# app/modules/hosting/schemas/hosted_site.py
"""Pydantic schemas for hosted site management."""

from datetime import datetime

from pydantic import BaseModel, Field, model_validator


class HostedSiteCreate(BaseModel):
    """Schema for creating a hosted site.

    Either merchant_id or prospect_id must be provided:
    - merchant_id: store is created under this merchant
    - prospect_id: a merchant is auto-created from prospect data
    """

    business_name: str = Field(..., max_length=255)
    merchant_id: int | None = None
    prospect_id: int | None = None
    contact_name: str | None = Field(None, max_length=255)
    contact_email: str | None = Field(None, max_length=255)
    contact_phone: str | None = Field(None, max_length=50)
    internal_notes: str | None = None

    @model_validator(mode="after")
    def require_merchant_or_prospect(self) -> "HostedSiteCreate":
        if not self.merchant_id and not self.prospect_id:
            raise ValueError("Either merchant_id or prospect_id is required")
        return self


class HostedSiteUpdate(BaseModel):
    """Schema for updating a hosted site."""

    business_name: str | None = Field(None, max_length=255)
    contact_name: str | None = Field(None, max_length=255)
    contact_email: str | None = Field(None, max_length=255)
    contact_phone: str | None = Field(None, max_length=50)
    internal_notes: str | None = None


class SendProposalRequest(BaseModel):
    """Schema for sending a proposal."""

    notes: str | None = None


class AcceptProposalRequest(BaseModel):
    """Schema for accepting a proposal."""

    merchant_id: int | None = None


class GoLiveRequest(BaseModel):
    """Schema for going live with a domain."""

    domain: str = Field(..., max_length=255)


class HostedSiteResponse(BaseModel):
    """Schema for hosted site response."""

    id: int
    store_id: int
    prospect_id: int | None = None
    status: str
    business_name: str
    contact_name: str | None = None
    contact_email: str | None = None
    contact_phone: str | None = None
    proposal_sent_at: datetime | None = None
    proposal_accepted_at: datetime | None = None
    went_live_at: datetime | None = None
    proposal_notes: str | None = None
    live_domain: str | None = None
    internal_notes: str | None = None
    created_at: datetime
    updated_at: datetime

    class Config:
        from_attributes = True


class HostedSiteDetailResponse(HostedSiteResponse):
    """Full hosted site detail with services."""

    client_services: list["ClientServiceResponse"] = []

    class Config:
        from_attributes = True


class HostedSiteListResponse(BaseModel):
    """Paginated hosted site list response."""

    items: list[HostedSiteResponse]
    total: int
    page: int
    per_page: int
    pages: int


class HostedSiteDeleteResponse(BaseModel):
    """Response for hosted site deletion."""

    message: str


# Forward references
from app.modules.hosting.schemas.client_service import (
    ClientServiceResponse,  # noqa: E402
)

HostedSiteDetailResponse.model_rebuild()
