"""
Blueprint factory - Railpack-first routing with E2B dry-run support.

Architecture:
- Railpack-first: Use Railway's buildpack for all project detection
- E2B sandbox: Optional dry-run builds in isolated environments
- Fallback chain: Railpack → Error (no other blueprints needed)

Environment Variables:
- E2B_API_KEY: API key for E2B sandbox (optional, for --dry-run)
"""

import os
from pathlib import Path
from typing import Optional

from .base import Blueprint, BuildPlan
from .railpack import RailpackBlueprint


class BlueprintFactory:
    """
    Factory for creating and selecting blueprints.
    
    Implements Railpack-first routing:
    1. RailpackBlueprint - Universal detection for all frameworks
    2. No fallbacks needed - Railpack handles everything
    """
    
    def __init__(self):
        # Single blueprint - Railpack handles all cases
        self._blueprint = RailpackBlueprint()
    
    def detect_framework(self, project_path: Path) -> tuple[str, BuildPlan]:
        """
        Detect framework and generate build plan for a project.
        
        Args:
            project_path: Path to the project directory
            
        Returns:
            Tuple of (framework_name, build_plan)
            
        Raises:
            RuntimeError: If detection fails
        """
        plan = self._blueprint.generate_plan(project_path)
        return plan.framework, plan
    
    def get_build_plan(self, project_path: Path) -> BuildPlan:
        """
        Get the build plan for a project.
        
        Args:
            project_path: Path to the project directory
            
        Returns:
            BuildPlan with all build/deployment information
        """
        return self._blueprint.generate_plan(project_path)
    
    def generate_dockerfile(self, project_path: Path) -> str:
        """
        Generate a Dockerfile for the project.
        
        Args:
            project_path: Path to the project directory
            
        Returns:
            Generated Dockerfile content
        """
        return self._blueprint.generate_dockerfile(project_path)


# Global factory instance
_factory: Optional[BlueprintFactory] = None


def get_factory() -> BlueprintFactory:
    """Get the global blueprint factory instance."""
    global _factory
    if _factory is None:
        _factory = BlueprintFactory()
    return _factory


def get_blueprint_for_framework(framework: str) -> Blueprint:
    """
    Get the appropriate blueprint for a framework.
    
    Note: Since we use Railpack-first, this always returns RailpackBlueprint.
    The framework parameter is kept for API compatibility.
    
    Args:
        framework: Framework name (for compatibility, not used)
        
    Returns:
        RailpackBlueprint instance
    """
    return RailpackBlueprint()


def detect_project(project_path: Path) -> tuple[str, BuildPlan]:
    """
    Convenience function to detect framework and get build plan.
    
    Args:
        project_path: Path to the project directory
        
    Returns:
        Tuple of (framework_name, build_plan)
    """
    return get_factory().detect_framework(project_path)
