"""Network plugin main class."""

import json
import logging
from pathlib import Path
from typing import List, Dict, Optional, Type

from flask import Flask, Blueprint
import click

from shopdb.plugins.base import BasePlugin, PluginMeta
from shopdb.api import db, AssetType

from .models import NetworkDevice, NetworkDeviceType, Subnet, VLAN
from .api import network_bp

logger = logging.getLogger(__name__)


class NetworkPlugin(BasePlugin):
    """
    Network plugin - manages network device assets.

    Network devices include switches, routers, access points, cameras, IDFs, etc.
    Uses the new Asset architecture with NetworkDevice extension table.
    """

    def __init__(self):
        self._manifest = self._load_manifest()

    def _load_manifest(self) -> Dict:
        """Load plugin manifest from JSON file."""
        manifestpath = Path(__file__).parent / 'manifest.json'
        if manifestpath.exists():
            with open(manifestpath, 'r') as f:
                return json.load(f)
        return {}

    @property
    def meta(self) -> PluginMeta:
        """Return plugin metadata."""
        return PluginMeta(
            name=self._manifest.get('name', 'network'),
            version=self._manifest.get('version', '1.0.0'),
            description=self._manifest.get(
                'description',
                'Network device management for switches, APs, and cameras'
            ),
            author=self._manifest.get('author', 'ShopDB Team'),
            dependencies=self._manifest.get('dependencies', []),
            core_version=self._manifest.get('core_version', '>=1.0.0'),
            api_prefix=self._manifest.get('api_prefix', '/api/network'),
        )

    def get_blueprint(self) -> Optional[Blueprint]:
        """Return Flask Blueprint with API routes."""
        return network_bp

    def get_models(self) -> List[Type]:
        """Return list of SQLAlchemy model classes."""
        return [NetworkDevice, NetworkDeviceType, Subnet, VLAN]

    def init_app(self, app: Flask, db_instance) -> None:
        """Initialize plugin with Flask app."""
        logger.info(f"Network plugin initialized (v{self.meta.version})")

    def on_install(self, app: Flask) -> None:
        """Called when plugin is installed."""
        with app.app_context():
            self._ensure_asset_type()
            self._ensure_network_device_types()
            logger.info("Network plugin installed")

    def _ensure_asset_type(self) -> None:
        """Ensure network_device asset type exists."""
        existing = AssetType.query.filter_by(assettype='network_device').first()
        if not existing:
            at = AssetType(
                assettype='network_device',
                pluginname='network',
                tablename='networkdevices',
                description='Network infrastructure devices (switches, APs, cameras, etc.)',
                icon='network-wired'
            )
            db.session.add(at)
            logger.debug("Created asset type: network_device")
        db.session.commit()

    def _ensure_network_device_types(self) -> None:
        """Ensure basic network device types exist."""
        device_types = [
            ('Switch', 'Network switch', 'network-wired'),
            ('Router', 'Network router', 'router'),
            ('Access Point', 'Wireless access point', 'wifi'),
            ('Firewall', 'Network firewall', 'shield'),
            ('Camera', 'IP camera', 'video'),
            ('IDF', 'Intermediate Distribution Frame/closet', 'box'),
            ('MDF', 'Main Distribution Frame', 'building'),
            ('Patch Panel', 'Patch panel', 'th'),
            ('UPS', 'Uninterruptible power supply', 'battery'),
            ('Other', 'Other network device', 'network-wired'),
        ]

        for name, description, icon in device_types:
            existing = NetworkDeviceType.query.filter_by(networkdevicetype=name).first()
            if not existing:
                ndt = NetworkDeviceType(
                    networkdevicetype=name,
                    description=description,
                    icon=icon
                )
                db.session.add(ndt)
                logger.debug(f"Created network device type: {name}")

        db.session.commit()

    def on_uninstall(self, app: Flask) -> None:
        """Called when plugin is uninstalled."""
        logger.info("Network plugin uninstalled")

    def get_cli_commands(self) -> List:
        """Return CLI commands for this plugin."""

        @click.group('network')
        def networkcli():
            """Network plugin commands."""
            pass

        @networkcli.command('list-types')
        def list_types():
            """List all network device types."""
            from flask import current_app

            with current_app.app_context():
                types = NetworkDeviceType.query.filter_by(isactive=True).all()
                if not types:
                    click.echo('No network device types found.')
                    return

                click.echo('Network Device Types:')
                for t in types:
                    click.echo(f"  [{t.networkdevicetypeid}] {t.networkdevicetype}")

        @networkcli.command('stats')
        def stats():
            """Show network device statistics."""
            from flask import current_app
            from shopdb.api import Asset

            with current_app.app_context():
                total = db.session.query(NetworkDevice).join(Asset).filter(
                    Asset.isactive == True
                ).count()

                click.echo(f"Total active network devices: {total}")

                # By type
                by_type = db.session.query(
                    NetworkDeviceType.networkdevicetype,
                    db.func.count(NetworkDevice.networkdeviceid)
                ).join(NetworkDevice, NetworkDevice.networkdevicetypeid == NetworkDeviceType.networkdevicetypeid
                ).join(Asset, Asset.assetid == NetworkDevice.assetid
                ).filter(Asset.isactive == True
                ).group_by(NetworkDeviceType.networkdevicetype
                ).all()

                if by_type:
                    click.echo("\nBy Type:")
                    for t, c in by_type:
                        click.echo(f"  {t}: {c}")

        @networkcli.command('find')
        @click.argument('hostname')
        def find_by_hostname(hostname):
            """Find a network device by hostname."""
            from flask import current_app

            with current_app.app_context():
                netdev = NetworkDevice.query.filter(
                    NetworkDevice.hostname.ilike(f'%{hostname}%')
                ).first()

                if not netdev:
                    click.echo(f'No network device found matching hostname: {hostname}')
                    return

                click.echo(f'Found: {netdev.hostname}')
                click.echo(f'  Asset: {netdev.asset.assetnumber}')
                click.echo(f'  Type: {netdev.networkdevicetype.networkdevicetype if netdev.networkdevicetype else "N/A"}')
                click.echo(f'  Firmware: {netdev.firmwareversion or "N/A"}')
                click.echo(f'  PoE: {"Yes" if netdev.ispoe else "No"}')

        return [networkcli]

    # NO dashboard card, deliberately. This plugin declared one for months
    # naming a component nobody wrote, so nothing rendered. Converting it would
    # have meant inventing a card: network devices carry no live status - there
    # is no polling, no reachability check, nothing that can be WRONG - so the
    # only card possible is a count of how many exist, which is the kind of
    # always-true number this dashboard exists to get away from. If reachability
    # is ever collected, that is the card.

    def get_navigation_items(self) -> List[Dict]:
        """Return navigation menu items."""
        return [
            {
                'name': 'Network',
                'icon': 'network-wired',
                'route': '/network',
                'position': 18,
            },
        ]

    def get_permissions(self) -> List:
        """Return the RBAC permissions this plugin owns."""
        return [
            ('network.view', 'View network devices', 'network'),
            ('network.create', 'Create network devices', 'network'),
            ('network.edit', 'Edit network devices', 'network'),
            ('network.delete', 'Delete network devices', 'network'),
        ]
