import os
from bfabric import BfabricAuth
from pydantic_settings import BaseSettings, SettingsConfigDict
from pydantic import EmailStr, Field
from typing import Optional, List, Dict

class Settings(BaseSettings):
    model_config = SettingsConfigDict(
        env_file=os.getenv("BFABRIC_WEBAPP_ENV", ".env"),
        extra="ignore",
    )

    REDIS_HOST: str = "localhost"
    REDIS_PORT: int = 6379
    
    REDIS_USERNAME: Optional[str] = None
    REDIS_PASSWORD: str = "CHANGE_ME_REDIS_PASSWORD_32CHR"

    # Power user credentials per B-Fabric instance (service accounts)
    POWER_USER_CREDENTIALS: Dict[str, BfabricAuth] = Field(
        default={
            "https://fgcz-bfabric.uzh.ch/bfabric": {
                "login": "gfeeder",
                "password": "CHANGE_ME_POWER_USER_PASSWORD_32"
            },
            "https://fgcz-bfabric-test.uzh.ch/bfabric": {
                "login": "CHANGE_ME_POWER_USER_LOGIN",
                "password": "CHANGE_ME_POWER_USER_PASSWORD_32"
            }
        },
        description="Power user credentials mapped to each B-Fabric instance base URL"
    )

    HOST: str = "127.0.0.1"
    PORT: int = 8050

    APP_TITLE: str = "B-Fabric App"
    SCRIPT_NAME: Optional[str] = None

    DEV: bool = False
    DEBUG: bool = False

    GUNICORN_WORKERS: int = 4
    GUNICORN_THREADS: int = 2

    DEVELOPER_EMAIL_ADDRESS: EmailStr = "claudio.cannizzaro@fgcz.uzh.ch"
    BUG_REPORT_EMAIL_ADDRESS: EmailStr = "gwtools@fgcz.system"

    #Run main pipeline config (only FGCZ specific)
    GSTORE_REMOTE_PATH: str = "/path/to/remote/gstore"
    SCRATCH_PATH: str = "/scratch/folder"
    TRX_LOGIN: str = "trxcopy@fgcz-server.uzh.ch" 
    TRX_SSH_KEY: str = "/home/user/.ssh/your_ssh_key"
    URL: str = "https:/fgcz/dummy/url"

    # Which service id to use for the charge 
    SERVICE_ID: int = 0

    # Which dataset template id to use for dataset creation
    DATASET_TEMPLATE_ID: int = 0

    # B-Fabric Configuration
    VALIDATION_INSTANCE_URL: str = "https://fgcz-bfabric-test.uzh.ch/bfabric"

    # Whitelist of supported B-Fabric instances (for security validation)
    SUPPORTED_BFABRIC_INSTANCES: List[str] = Field(
        default=[
            "https://fgcz-bfabric.uzh.ch/bfabric",
            "https://fgcz-bfabric-test.uzh.ch/bfabric"
        ],
        description="List of allowed B-Fabric base URLs. Tokens from other instances will be rejected."
    )

    # Session Configuration (Flask-Session)
    SECRET_KEY: str = Field(
        default="CHANGE_ME_SECRET_KEY_32_CHARS_MIN",
        description="Secret key for session signing. MUST be changed in production."
    )
    SESSION_TYPE: str = Field(
        default="filesystem",
        description="Session storage backend: 'filesystem' or 'redis'"
    )
    SESSION_FILE_DIR: str = Field(
        default="/tmp/flask_sessions",
        description="Directory for filesystem sessions (only used if SESSION_TYPE='filesystem')"
    )
    SESSION_PERMANENT: bool = True
    SESSION_USE_SIGNER: bool = True
    SESSION_KEY_PREFIX: str = "bfabric_webapp_session:"
    SESSION_COOKIE_SECURE: bool = Field(
        default=False,
        description="Set True in production with HTTPS"
    )
    SESSION_COOKIE_HTTPONLY: bool = True
    SESSION_COOKIE_SAMESITE: str = "Lax"
    PERMANENT_SESSION_LIFETIME: int = Field(
        default=86400,
        description="Session lifetime in seconds since last activity (default: 24 hours). Session extends on each activity."
    )

# Instantiate settings
settings = Settings()

