"""Configuration management for LinkedIn Workflow."""
from typing import Optional
from pydantic_settings import BaseSettings, SettingsConfigDict
from pydantic import Field, AliasChoices
from pathlib import Path


class Settings(BaseSettings):
    """Application settings loaded from environment variables."""
    # API Keys
    openai_api_key: str
    perplexity_api_key: str
    apify_api_key: str
    azure_claude_api_key: str = ""
    azure_claude_endpoint: str = ""
    azure_claude_base_url: str = ""
    azure_claude_api_version: str = "2024-10-21"
    azure_claude_deployment: str = ""
    azure_claude_model: str = "claude-sonnet-4.6"

    # Supabase
    supabase_url: str
    supabase_key: str
    supabase_service_role_key: str = ""  # Required for admin operations like deleting users

    # Apify
    apify_actor_id: str = "apimaestro~linkedin-profile-posts"

    # Web Interface
    web_password: str = ""
    session_secret: str = ""

    # Development
    debug: bool = False
    log_level: str = "INFO"

    # Email Settings
    smtp_host: str = ""
    smtp_port: int = 587
    smtp_user: str = ""
    smtp_password: str = ""
    smtp_from_name: str = "LinkedIn Post System"
    email_default_recipient: str = ""

    # Writer Features (can be toggled to disable new features)
    writer_multi_draft_enabled: bool = True  # Generate multiple drafts and select best
    writer_multi_draft_count: int = 3  # Number of drafts to generate (2-5)
    writer_semantic_matching_enabled: bool = True  # Use semantically similar example posts
    writer_learn_from_feedback: bool = True  # Learn from recurring critic feedback
    writer_feedback_history_count: int = 10  # Number of past posts to analyze for patterns

    # Quality Refiner Features (NEW - post-processing improvements)
    quality_refiner_enabled: bool = True  # Enable automatic quality refinement
    quality_smart_revision_enabled: bool = True  # Enable smart revision if auto-refiner not enough
    quality_min_acceptable_score: int = 80  # Minimum score to accept (even if not perfect)
    quality_target_score: int = 85  # Target score to achieve
    quality_max_smart_revisions: int = 1  # Max LLM revisions (0 = disabled, 1 = safe)

    # User Frontend (LinkedIn OAuth via Supabase)
    user_frontend_enabled: bool = True  # Enable user frontend with LinkedIn OAuth
    supabase_redirect_url: str = ""  # OAuth Callback URL (e.g., https://linkedin.onyva.dev/auth/callback)

    # LinkedIn API (Custom OAuth for auto-posting)
    linkedin_client_id: str = ""
    linkedin_client_secret: str = ""
    linkedin_redirect_uri: str = ""  # e.g., https://yourdomain.com/settings/linkedin/callback

    # Token Encryption
    encryption_key: str = ""  # Generate with: python -c "from cryptography.fernet import Fernet; print(Fernet.generate_key().decode())"

    # NVIDIA (free model for insights summary)
    nvidia_api_key: str = ""

    # MOCO Integration
    moco_api_key: str = ""    # Token für Authorization-Header
    moco_domain: str = ""     # Subdomain: {domain}.mocoapp.com

    # Redis
    redis_url: str = "redis://redis:6379/0"
    scheduler_enabled: bool = False  # True only on dedicated scheduler container
    insights_manual_refresh_limit_enabled: bool = Field(
        default=True,
        validation_alias=AliasChoices(
            "INSIGHTS_MANUAL_REFRESH_LIMIT_ENABLED",
            "NSIGHTS_MANUAL_REFRESH_LIMIT_ENABLED"
        ),
    )

    # YouTube (optional)
    youtube_cookies: str = ""  # Raw Cookie header value for transcript fetching
    transcriptapi_key: str = ""  # TranscriptAPI.com key

    # Telegram Bot (experimental)
    telegram_enabled: bool = False
    telegram_bot_token: str = ""
    telegram_bot_username: str = ""       # e.g. "MyLinkedInBot" (without @)
    telegram_webhook_secret: str = ""     # Random string to validate incoming webhooks
    telegram_webhook_url: str = ""        # Base URL of the app, e.g. https://app.example.com

    # Microsoft Teams Bot
    teams_enabled: bool = False
    microsoft_app_id: str = ""
    microsoft_app_secret: str = ""
    microsoft_app_tenant_id: str = "botframework.com"  # "botframework.com" for multi-tenant, tenant ID for single-tenant

    model_config = SettingsConfigDict(
        env_file=".env",
        env_file_encoding="utf-8",
        case_sensitive=False,
        extra="ignore"
    )


# Global settings instance
settings = Settings()

# API pricing per 1M tokens (input, output)
API_PRICING = {
    "gpt-4o": {"input": 2.50, "output": 10.00},
    "gpt-4o-mini": {"input": 0.15, "output": 0.60},
    "claude-sonnet-4.6": {"input": 3.00, "output": 15.00},
    "sonar": {"input": 1.00, "output": 1.00},
}


def estimate_cost(model: str, prompt_tokens: int, completion_tokens: int) -> float:
    """Estimate cost in USD for an API call."""
    normalized_model = (model or "").lower()

    if normalized_model not in API_PRICING:
        if "claude" in normalized_model and "sonnet" in normalized_model:
            normalized_model = "claude-sonnet-4.6"
        elif "gpt-4o-mini" in normalized_model:
            normalized_model = "gpt-4o-mini"
        elif "gpt-4o" in normalized_model:
            normalized_model = "gpt-4o"

    pricing = API_PRICING.get(normalized_model, {"input": 1.00, "output": 1.00})
    input_cost = (prompt_tokens / 1_000_000) * pricing["input"]
    output_cost = (completion_tokens / 1_000_000) * pricing["output"]
    return input_cost + output_cost
