"""
Configuration Module
配置模块

Central configuration management for the MixVideo V2 application.
"""

import os
from pathlib import Path
from typing import Dict, Any
try:
    from pydantic_settings import BaseSettings
    from pydantic import Field
except ImportError:
    # Fallback for older pydantic versions
    from pydantic import BaseSettings, Field

project_root = Path(__file__).parent.parent
class Settings(BaseSettings):
    """Application settings with environment variable support."""
    
    # Application Info
    app_name: str = "MixVideo V2"
    app_version: str = "2.0.0"
    debug: bool = Field(default=False, env="DEBUG")
    
    # Paths
    project_root: Path = project_root
    temp_dir: Path = Field(default_factory=lambda: project_root / "mixvideo" / "temp")
    cache_dir: Path = Field(default_factory=lambda: project_root / "mixvideo" / "cache")
    projects_dir: Path = Field(default_factory=lambda: project_root / "mixvideo"/"projects")
    
    # Video Processing
    max_video_resolution: str = "1920x1080"
    default_fps: int = 30
    default_video_codec: str = "libx264"
    default_audio_codec: str = "aac"
    # cloud flare
    cloudflare_api_key: str = "dlGquMNiAX-S7SV9pXne7YGfdH_fEgq3TfIGgNcQ"
    cloudflare_url: str = "https://api.cloudflare.com/client/v4/"
    cloudflare_account_id: str = "67720b647ff2b55cf37ba3ef9e677083"
    cloudflare_kv_id: str = "995d7eff088547d7a7d8fea53e56115b"
    # Audio Processing
    default_sample_rate: int = 44100
    default_audio_bitrate: str = "192k"
    
    # Performance
    max_workers: int = Field(default=4, env="MAX_WORKERS")
    chunk_size: int = 1024 * 1024  # 1MB chunks
    
    # Redis (for task queue)
    redis_host: str = Field(default="localhost", env="REDIS_HOST")
    redis_port: int = Field(default=6379, env="REDIS_PORT")
    redis_db: int = Field(default=0, env="REDIS_DB")
    
    # Logging
    log_level: str = Field(default="INFO", env="LOG_LEVEL")
    log_file: str = "mixvideo.log"
    
    # File Formats
    supported_video_formats: list = [".mp4", ".avi", ".mov", ".mkv", ".wmv", ".flv"]
    supported_audio_formats: list = [".mp3", ".wav", ".aac", ".flac", ".ogg"]
    supported_image_formats: list = [".jpg", ".jpeg", ".png", ".bmp", ".tiff"]
    
    class Config:
        env_file = ".env"
        env_file_encoding = "utf-8"
    
    def __init__(self, **kwargs):
        super().__init__(**kwargs)
        # Ensure directories exist
        self.temp_dir.mkdir(parents=True, exist_ok=True)
        self.cache_dir.mkdir(parents=True, exist_ok=True)
        self.projects_dir.mkdir(parents=True, exist_ok=True)


# Global settings instance
settings = Settings()


class Config:
    """Configuration helper class."""
    
    @staticmethod
    def get_temp_path(filename: str = None) -> Path:
        """Get temporary file path."""
        if filename:
            return settings.temp_dir / filename
        return settings.temp_dir
    
    @staticmethod
    def get_cache_path(filename: str = None) -> Path:
        """Get cache file path."""
        if filename:
            return settings.cache_dir / filename
        return settings.cache_dir
    
    @staticmethod
    def get_project_path(project_name: str = None) -> Path:
        """Get project directory path."""
        if project_name:
            return settings.projects_dir / project_name
        return settings.projects_dir
    
    @staticmethod
    def is_supported_video(file_path: str) -> bool:
        """Check if file is a supported video format."""
        return Path(file_path).suffix.lower() in settings.supported_video_formats
    
    @staticmethod
    def is_supported_audio(file_path: str) -> bool:
        """Check if file is a supported audio format."""
        return Path(file_path).suffix.lower() in settings.supported_audio_formats
    
    @staticmethod
    def is_supported_image(file_path: str) -> bool:
        """Check if file is a supported image format."""
        return Path(file_path).suffix.lower() in settings.supported_image_formats
