"""
Shadcn CLI wrapper for component management
"""

import subprocess
from pathlib import Path
from typing import List, Optional, Dict, Any


class ShadcnWrapper:
    """
    Wrapper for shadcn/ui CLI
    Manages shadcn component installation and configuration
    """

    def __init__(self, shadcn_path: str = "npx shadcn-ui@latest"):
        """
        Initialize Shadcn wrapper

        Args:
            shadcn_path: Path to shadcn executable (default: npx shadcn-ui@latest)
        """
        self.shadcn_path = shadcn_path

    def add_component(
        self,
        component_name: str,
        project_path: Path,
        overwrite: bool = False
    ) -> Dict[str, Any]:
        """
        Add a shadcn component to project

        Args:
            component_name: Component to add (e.g., 'button', 'card')
            project_path: Project root directory
            overwrite: Whether to overwrite existing components

        Returns:
            Dict with installation result
        """
        cmd = [
            "npx", "shadcn-ui@latest", "add", component_name,
            "--yes"  # Auto-confirm
        ]

        if overwrite:
            cmd.append("--overwrite")

        result = subprocess.run(
            cmd,
            cwd=project_path,
            capture_output=True,
            text=True
        )

        return {
            "success": result.returncode == 0,
            "component": component_name,
            "stdout": result.stdout,
            "stderr": result.stderr
        }

    def init_shadcn(self, project_path: Path) -> Dict[str, Any]:
        """
        Initialize shadcn in a project

        Args:
            project_path: Project root directory

        Returns:
            Dict with initialization result
        """
        cmd = ["npx", "shadcn-ui@latest", "init", "--yes"]

        result = subprocess.run(
            cmd,
            cwd=project_path,
            capture_output=True,
            text=True
        )

        return {
            "success": result.returncode == 0,
            "stdout": result.stdout,
            "stderr": result.stderr
        }

    def list_available_components(self) -> List[str]:
        """
        List available shadcn components

        Returns:
            List of component names
        """
        # Hardcoded list of common shadcn components
        # In a real implementation, this would query the shadcn registry
        return [
            "accordion", "alert", "alert-dialog", "aspect-ratio",
            "avatar", "badge", "button", "calendar", "card",
            "checkbox", "collapsible", "command", "context-menu",
            "dialog", "dropdown-menu", "form", "hover-card",
            "input", "label", "menubar", "navigation-menu",
            "popover", "progress", "radio-group", "scroll-area",
            "select", "separator", "sheet", "skeleton", "slider",
            "switch", "table", "tabs", "textarea", "toast",
            "toggle", "tooltip"
        ]


# Alias for backward compatibility
ShadcnTool = ShadcnWrapper
