"""Setup script to ensure spaCy models are properly installed."""

import subprocess
import sys
import os
from typing import Optional


def install_spacy_model(model_name: str = "en_core_web_lg") -> bool:
    """Install spaCy model if not already available."""
    try:
        import spacy

        # Check if model is already installed
        if spacy.util.is_package(model_name):
            print(f"✅ spaCy model '{model_name}' is already installed")
            return True

        print(f"📥 Installing spaCy model '{model_name}'...")

        # Try direct wheel installation first (best for corporate networks)
        wheel_urls = {
            "en_core_web_lg": "https://github.com/explosion/spacy-models/releases/download/en_core_web_lg-3.8.0/en_core_web_lg-3.8.0-py3-none-any.whl",
            "en_core_web_md": "https://github.com/explosion/spacy-models/releases/download/en_core_web_md-3.8.0/en_core_web_md-3.8.0-py3-none-any.whl",
            "en_core_web_sm": "https://github.com/explosion/spacy-models/releases/download/en_core_web_sm-3.8.0/en_core_web_sm-3.8.0-py3-none-any.whl"
        }

        if model_name in wheel_urls:
            try:
                result = subprocess.run([
                    sys.executable, "-m", "pip", "install", wheel_urls[model_name]
                ], capture_output=True, text=True, check=True)
                print(f"✅ Successfully installed {model_name} via pip")
                return True
            except subprocess.CalledProcessError as e:
                print(f"❌ Failed to install {model_name}: {e.stderr}")
                print(f"   Error details: {e.stderr}")
                return False
        else:
            print(f"❌ No wheel URL available for model: {model_name}")
            return False

    except ImportError:
        print("❌ spaCy is not installed. Please install spaCy first.")
        return False
    except Exception as e:
        print(f"❌ Unexpected error installing {model_name}: {e}")
        return False


def setup_models() -> None:
    """Setup all required spaCy models."""
    print("🔧 Setting up spaCy models for simple-anonymizer...")

    # Install the default model
    success = install_spacy_model("en_core_web_lg")

    if success:
        print("✅ spaCy model setup completed successfully!")
    else:
        print("⚠️  spaCy model setup failed. You may need to install manually:")
        print("   pip install https://github.com/explosion/spacy-models/releases/download/en_core_web_lg-3.8.0/en_core_web_lg-3.8.0-py3-none-any.whl")


if __name__ == "__main__":
    setup_models()
