"""Deployments domain - Pydantic schemas."""
from __future__ import annotations
from datetime import datetime
from typing import Optional
from uuid import UUID

from typing import Literal

from pydantic import BaseModel


class DeployCreate(BaseModel):
    revision_id: Optional[UUID] = None  # defaults to latest revision for the project
    environment: Literal["production", "staging", "preview"] = "production"


class DeployResponse(BaseModel):
    id: UUID
    project_id: UUID
    revision_id: UUID
    environment: str
    status: str
    deploy_url: Optional[str]
    provider: Optional[str]
    error_message: Optional[str]
    started_at: Optional[datetime]
    completed_at: Optional[datetime]
    created_at: datetime

    model_config = {"from_attributes": True}


class DomainCreate(BaseModel):
    domain: str
    environment: Literal["production", "preview"] = "production"


class DnsRecord(BaseModel):
    """A single DNS record the user must add at their registrar."""
    step: int
    purpose: str    # "verify" | "route"
    type: str       # "TXT" | "CNAME" | "A" | "ALIAS"
    host: str       # full hostname (e.g. "_p2hs-verify.www.example.com")
    value: str      # record value
    ttl: int = 300
    note: Optional[str] = None


class DnsInstructions(BaseModel):
    """Complete DNS setup instructions returned alongside a DomainResponse."""
    records: list[DnsRecord]
    is_apex: bool = False
    apex_note: Optional[str] = None
    propagation_note: str = "DNS changes can take 5 minutes to 24 hours to propagate globally."


class DomainResponse(BaseModel):
    id: UUID
    project_id: UUID
    domain: str
    environment: str
    verification_status: str
    verification_txt: Optional[str]
    is_primary: bool
    tls_status: Optional[str]
    created_at: datetime
    verified_at: Optional[datetime]
    dns_instructions: Optional[DnsInstructions] = None

    model_config = {"from_attributes": True}
