"""Equipment 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.extensions import db
from shopdb.core.models import AssetType, AssetStatus

from .models import Equipment, EquipmentType
from .api import equipment_bp

logger = logging.getLogger(__name__)


class EquipmentPlugin(BasePlugin):
    """
    Equipment plugin - manages manufacturing equipment assets.

    Equipment includes CNCs, CMMs, lathes, grinders, EDMs, part markers, etc.
    Uses the new Asset architecture with Equipment 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', 'equipment'),
            version=self._manifest.get('version', '1.0.0'),
            description=self._manifest.get(
                'description',
                'Equipment management for manufacturing assets'
            ),
            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/equipment'),
        )

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

    def get_models(self) -> List[Type]:
        """Return list of SQLAlchemy model classes."""
        return [Equipment, EquipmentType]

    def init_app(self, app: Flask, db_instance) -> None:
        """Initialize plugin with Flask app."""
        logger.info(f"Equipment 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_asset_statuses()
            self._ensure_equipment_types()
            logger.info("Equipment plugin installed")

    def _ensure_asset_type(self) -> None:
        """Ensure equipment asset type exists."""
        existing = AssetType.query.filter_by(assettype='equipment').first()
        if not existing:
            at = AssetType(
                assettype='equipment',
                pluginname='equipment',
                tablename='equipment',
                description='Manufacturing equipment (CNCs, CMMs, lathes, etc.)',
                icon='cog'
            )
            db.session.add(at)
            logger.debug("Created asset type: equipment")
        db.session.commit()

    def _ensure_asset_statuses(self) -> None:
        """Ensure standard asset statuses exist."""
        statuses = [
            ('In Use', 'Asset is currently in use', '#28a745'),
            ('Spare', 'Spare/backup asset', '#17a2b8'),
            ('Retired', 'Asset has been retired', '#6c757d'),
            ('Maintenance', 'Asset is under maintenance', '#ffc107'),
            ('Decommissioned', 'Asset has been decommissioned', '#dc3545'),
        ]

        for name, description, color in statuses:
            existing = AssetStatus.query.filter_by(status=name).first()
            if not existing:
                s = AssetStatus(
                    status=name,
                    description=description,
                    color=color
                )
                db.session.add(s)
                logger.debug(f"Created asset status: {name}")

        db.session.commit()

    def _ensure_equipment_types(self) -> None:
        """Ensure basic equipment types exist."""
        equipment_types = [
            ('CNC', 'Computer Numerical Control machine', 'cnc'),
            ('CMM', 'Coordinate Measuring Machine', 'cmm'),
            ('Lathe', 'Lathe machine', 'lathe'),
            ('Grinder', 'Grinding machine', 'grinder'),
            ('EDM', 'Electrical Discharge Machine', 'edm'),
            ('Part Marker', 'Part marking/engraving equipment', 'marker'),
            ('Mill', 'Milling machine', 'mill'),
            ('Press', 'Press machine', 'press'),
            ('Robot', 'Industrial robot', 'robot'),
            ('Other', 'Other equipment type', 'cog'),
        ]

        for name, description, icon in equipment_types:
            existing = EquipmentType.query.filter_by(equipmenttype=name).first()
            if not existing:
                et = EquipmentType(
                    equipmenttype=name,
                    description=description,
                    icon=icon
                )
                db.session.add(et)
                logger.debug(f"Created equipment type: {name}")

        db.session.commit()

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

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

        @click.group('equipment')
        def equipmentcli():
            """Equipment plugin commands."""
            pass

        @equipmentcli.command('list-types')
        def list_types():
            """List all equipment types."""
            from flask import current_app

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

                click.echo('Equipment Types:')
                for t in types:
                    click.echo(f"  [{t.equipmenttypeid}] {t.equipmenttype}")

        @equipmentcli.command('stats')
        def stats():
            """Show equipment statistics."""
            from flask import current_app
            from shopdb.core.models import Asset

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

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

                # By type
                by_type = db.session.query(
                    EquipmentType.equipmenttype,
                    db.func.count(Equipment.equipmentid)
                ).join(Equipment, Equipment.equipmenttypeid == EquipmentType.equipmenttypeid
                ).join(Asset, Asset.assetid == Equipment.assetid
                ).filter(Asset.isactive == True
                ).group_by(EquipmentType.equipmenttype
                ).all()

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

        return [equipmentcli]

    def get_dashboard_widgets(self) -> List[Dict]:
        """Return dashboard widget definitions."""
        return [
            {
                'name': 'Equipment Status',
                'component': 'EquipmentStatusWidget',
                'endpoint': '/api/equipment/dashboard/summary',
                'size': 'medium',
                'position': 5,
            },
        ]

    def get_navigation_items(self) -> List[Dict]:
        """Return navigation menu items."""
        return [
            {
                'name': 'Equipment',
                'icon': 'cog',
                'route': '/machines',
                'position': 10,
            },
        ]
