"""Centralised configuration from environment variables."""

from __future__ import annotations

from pydantic_settings import BaseSettings


class Settings(BaseSettings):
    # dgred CRM
    dgred_api_key: str = ""
    dgred_base_url: str = "https://app.dgred.com/api/v1"

    # eBay
    ebay_client_id: str = ""
    ebay_client_secret: str = ""
    ebay_oauth_token: str = ""  # Legacy: used as fallback if no refresh token
    ebay_redirect_uri: str = "Denis_Kuchur-DenisKuc-agents-shbppb"
    ebay_token_path: str = "/var/lib/agent-samochodowy/ebay_tokens.json"

    # Allegro
    allegro_client_id: str = ""
    allegro_client_secret: str = ""
    allegro_oauth_token: str = ""

    # Apify
    apify_token: str = ""
    apify_actor_id: str = ""
    # Apify — authenticated scraper (cookies-based, for private groups)
    apify_actor_id_auth: str = "whoareyouanas/facebook-group-scraper"
    fb_cookies: str = ""  # JSON array of FB session cookies; empty = skip scraping
    fb_proxy_url: str = ""  # residential proxy URL for authenticated scraper

    # LLM
    llm_api_key: str = ""
    llm_provider: str = "openai"  # "openai" or "anthropic"
    llm_model: str = "gpt-4o-mini"

    # Pipeline
    dry_run: bool = True
    log_level: str = "INFO"
    db_path: str = "data/catalog.db"
    confidence_high: float = 0.85
    confidence_low: float = 0.50
    post_max_age_hours: int = 48

    # Discovery funnel
    n_groups_per_cycle: int = 6

    # SendGrid notifications (operator only)
    sendgrid_api_key: str = ""
    notify_email_from: str = "sys@airminal.com"
    notify_email_to: str = ""  # comma-separated
    notify_max_per_cycle: int = 10

    # Airminal billing
    airminal_billing_key: str = ""
    airminal_billing_url: str = "https://app.airminal.com/pages/api/api_billing.php"
    billing_mode: str = "shadow"  # shadow | live
    tokens_per_scan: float = 0.5
    tokens_per_lead_67: float = 5.0
    tokens_per_lead_68: float = 2.0
    tokens_per_lead_69: float = 0.0
    billing_max_tokens_per_cycle: float = 200.0

    # Circuit breaker
    max_leads_per_cycle: int = 20
    max_errors_per_cycle: int = 5

    # Account guard (anti-ban)
    warmup_until: str = ""  # ISO date (YYYY-MM-DD); empty = no warmup, use n_groups_per_cycle
    max_daily_scans: int = 30

    model_config = {"env_file": ".env", "env_file_encoding": "utf-8", "extra": "ignore"}
