from pydantic_settings import BaseSettings, SettingsConfigDict


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

    DATABASE_URL: str
    DATABASE_URL_SYNC: str
    SECRET_KEY: str
    ADMIN_EMAIL: str = "admin@local"
    LOG_LEVEL: str = "INFO"
    SESSION_COOKIE_NAME: str = "judicialsession"
    SESSION_LIFETIME_SECONDS: int = 60 * 60 * 24 * 7
    SCRAPER_USER_AGENTS_FILE: str | None = None
    SCRAPE_MIN_DELAY_S: float = 0.3
    SCRAPE_MAX_DELAY_S: float = 1.2
    SCRAPE_CONCURRENCY: int = 8
    FUZZY_MATCH_THRESHOLD: float = 0.82

    # SMTP (for email alerts). If SMTP_HOST is empty, email alerts are skipped.
    SMTP_HOST: str = ""
    SMTP_PORT: int = 587
    SMTP_USER: str = ""
    SMTP_PASSWORD: str = ""
    SMTP_FROM: str = ""
    SMTP_STARTTLS: bool = True
    # O mail.segunor.pt serve na 465 com TLS implícito, não STARTTLS. Com a
    # flag a false o handshake nunca chega a acontecer e o envio falha.
    SMTP_SSL_IMPLICIT: bool = False

    # Webhook delivery
    WEBHOOK_TIMEOUT_S: float = 8.0
    WEBHOOK_RETRIES: int = 3

    # DRE — uses official files.diariodarepublica.pt RSS feeds (Série I + II)
    DRE_ENABLED: bool = True

    # Registo comercial (IRN). O que importa é o **total** de pedidos por
    # segundo, não quantos trabalhadores há: cada acto são três idas e voltas de
    # ~90 ms em série, portanto um trabalhador sozinho passa o tempo à espera e
    # dois ou três chegam ao tecto. O tecto é uma escolha de cortesia — o portal
    # aguenta mais, medido a ~11 pedidos/s — e existe para não sermos nós a
    # degradar um serviço público.
    REGISTRY_RATE_LIMIT: float = 12.0
    REGISTRY_WORKERS: int = 3
    # Metade do ritmo em horário de trabalho, que é quando o portal do IRN tem
    # gente a usá-lo. Fora dessa janela corre ao tecto.
    REGISTRY_DAYTIME_FACTOR: float = 0.5
    REGISTRY_DAYTIME_START_HOUR: int = 8
    REGISTRY_DAYTIME_END_HOUR: int = 20
    # Profundidade máxima da expansão por sócios que são empresas. Cada hop
    # multiplica o número de entidades; 3 já cobre grupo-mãe, irmãs e netas.
    REGISTRY_MAX_DEPTH: int = 3


settings = Settings()
