from pathlib import Path

from pydantic_settings import BaseSettings, SettingsConfigDict

ENV_FILE = Path(__file__).resolve().parent.parent / ".env"


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

    database_url: str = "postgresql+psycopg://marketing:marketing@localhost:5432/leads_tracker"
    cors_origins: str = "http://localhost:5173,http://localhost:3000"

    openai_api_key: str = ""
    # Fast tool-calling model (gpt-4o-mini >> gpt-5-nano for voice plan latency)
    openai_model: str = "gpt-4o-mini"
    # Reasoning models (gpt-5*, o*) think before answering. "minimal" is by far
    # the fastest and is plenty for picking a tool. Ignored for non-reasoning models.
    openai_reasoning_effort: str = "minimal"
    # Speech-to-text: gpt-4o-mini-transcribe is faster + more accurate than whisper-1
    openai_stt_model: str = "gpt-4o-mini-transcribe"
    # ISO-639-1 hint ("en"); empty = auto-detect
    openai_stt_language: str = "en"
    openai_timeout_seconds: float = 30.0

    smtp_host: str = ""
    smtp_port: int = 587
    smtp_user: str = ""
    smtp_password: str = ""
    smtp_from: str = ""
    smtp_use_tls: bool = True

    # Gmail API + OAuth (preferred for multi-account sending)
    # Create an OAuth "Web application" client in Google Cloud Console.
    google_client_id: str = ""
    google_client_secret: str = ""
    # Optional path to downloaded client_secret_*.json (overrides id/secret above)
    google_client_secrets_file: str = ""
    google_redirect_uri: str = "http://localhost:8000/api/gmail/auth/callback"
    frontend_url: str = "http://localhost:5173"
    # auto = Gmail OAuth when account connected, else SMTP; gmail = OAuth only; smtp = SMTP only
    email_provider: str = "auto"

    # Inbound mail → auto-create Positive leads
    inbound_mail_enabled: bool = True
    inbound_mail_poll_seconds: int = 120
    # Use OpenAI to decide interested/positive replies (after hard spam/negative rules)
    inbound_mail_ai_enabled: bool = True

    @property
    def origins(self) -> list[str]:
        return [o.strip() for o in self.cors_origins.split(",") if o.strip()]

    @property
    def smtp_configured(self) -> bool:
        return bool(self.smtp_host and (self.smtp_from or self.smtp_user))

    @property
    def gmail_oauth_configured(self) -> bool:
        if self.google_client_secrets_file:
            return Path(self.google_client_secrets_file).is_file()
        return bool(self.google_client_id and self.google_client_secret)


settings = Settings()

# Offsets from date_added — matches sheet “follow up guide”
FOLLOW_UP_SCHEDULE = [
    {
        "number": 1,
        "day_label": "Day 1",
        "email_type": "Day 1 (Same day reply)",
        "offset_days": 0,
        "gap_label": "+0d",
        "goal": "Value Drop",
        "key_cta": "Deliver 3 quick store observations + soft CTA",
        "status_option": "Book a 15-min call?",
        "color": "#dcfce7",
    },
    {
        "number": 2,
        "day_label": "Day 3",
        "email_type": "Day 3",
        "offset_days": 3,
        "gap_label": "+3d",
        "goal": "Social Proof",
        "key_cta": "Share a relevant case study / portfolio example",
        "status_option": "See full case study →",
        "color": "#dbeafe",
    },
    {
        "number": 3,
        "day_label": "Day 7",
        "email_type": "Day 7",
        "offset_days": 7,
        "gap_label": "+7d",
        "goal": "Audit Teaser",
        "key_cta": "Send 1-2 more specific audit findings from their store",
        "status_option": "Want the full 20-point audit?",
        "color": "#fef9c3",
    },
    {
        "number": 4,
        "day_label": "Day 10",
        "email_type": "Day 10",
        "offset_days": 10,
        "gap_label": "+10d",
        "goal": "Objection Buster",
        "key_cta": "Address common hesitations (cost, time, ROI)",
        "status_option": "Would a no-cost audit help?",
        "color": "#ede9fe",
    },
    {
        "number": 5,
        "day_label": "Day 14",
        "email_type": "Day 14",
        "offset_days": 14,
        "gap_label": "+14d",
        "goal": "Value Reminder",
        "key_cta": "Reinforce revenue impact, share a quick win stat",
        "status_option": "15 mins this week?",
        "color": "#ffedd5",
    },
    {
        "number": 6,
        "day_label": "Day 21",
        "email_type": "Day 21",
        "offset_days": 21,
        "gap_label": "+21d",
        "goal": "Last Attempt",
        "key_cta": "Soft close — 'still interested?' angle",
        "status_option": "Just reply YES/NO",
        "color": "#ffedd5",
    },
    {
        "number": 7,
        "day_label": "Day 30",
        "email_type": "Day 30",
        "offset_days": 30,
        "gap_label": "+30d",
        "goal": "Break-Up / Re-Engage",
        "key_cta": "Friendly farewell + leave door open for Q3",
        "status_option": "Remove or re-engage?",
        "color": "#e0e7ff",
    },
]

FOLLOW_UP_GUIDE = {spec["number"]: spec for spec in FOLLOW_UP_SCHEDULE}

PIPELINE_STAGES = [
    "New Lead",
    "Needs to send",
    "Potential lead",
    "Warm lead",
    "Positive lead",
    "Follow-up Required",
    "Future follow up",
    "Proposal Sent",
    "Call Scheduled",
    "Won",
    "Lost",
]

NOTE_QUICK_TAGS = [
    "Called - Left Voicemail",
    "Call Scheduled",
    "Needs to send",
    "Warm lead",
    "Positive lead",
    "Future follow up",
    "Follow-up Required",
]

FOLLOW_UP_STATUSES = ["pending", "done", "skipped"]
