"""Sites & page resource DTOs — Phase 11.1."""
from __future__ import annotations

from datetime import datetime
from uuid import UUID

from pydantic import BaseModel, ConfigDict, Field

from app.models.types import DomainKind, PageType, PublishStatus
from app.schemas.common import ORMBase

_FORBID = ConfigDict(extra="forbid")


class ExperienceSiteCreate(BaseModel):
    model_config = _FORBID

    workspace_id: UUID
    code: str = Field(max_length=50)
    name: str = Field(max_length=255)
    description: str | None = None
    publish_status: PublishStatus = PublishStatus.DRAFT
    default_locale: str = Field(default="fa", max_length=16)
    default_direction: str = Field(default="rtl", max_length=3, pattern="^(rtl|ltr)$")
    primary_domain: str | None = Field(default=None, max_length=255)
    settings: dict | None = None
    metadata_json: dict | None = None


class ExperienceSiteUpdate(BaseModel):
    model_config = _FORBID

    name: str | None = Field(default=None, max_length=255)
    description: str | None = None
    default_locale: str | None = Field(default=None, max_length=16)
    default_direction: str | None = Field(
        default=None, max_length=3, pattern="^(rtl|ltr)$"
    )
    primary_domain: str | None = Field(default=None, max_length=255)
    home_page_id: UUID | None = None
    settings: dict | None = None
    metadata_json: dict | None = None
    version: int = Field(ge=1)


class ExperienceSiteRead(ORMBase):
    id: UUID
    tenant_id: UUID
    workspace_id: UUID
    code: str
    name: str
    description: str | None
    publish_status: PublishStatus
    default_locale: str
    default_direction: str
    primary_domain: str | None
    home_page_id: UUID | None
    published_at: datetime | None
    archived_at: datetime | None
    settings: dict | None
    metadata_json: dict | None
    version: int
    is_deleted: bool
    created_by: str | None
    updated_by: str | None
    created_at: datetime
    updated_at: datetime


class ExperiencePageCreate(BaseModel):
    model_config = _FORBID

    workspace_id: UUID
    site_id: UUID
    code: str = Field(max_length=50)
    slug: str = Field(max_length=200)
    title: str = Field(max_length=255)
    summary: str | None = None
    page_type: PageType = PageType.LANDING
    publish_status: PublishStatus = PublishStatus.DRAFT
    locale: str = Field(default="fa", max_length=16)
    text_direction: str = Field(default="rtl", max_length=3, pattern="^(rtl|ltr)$")
    sort_order: int = Field(default=0, ge=0)
    is_home: bool = False
    content_shell: dict | None = None
    seo_shell: dict | None = None
    external_refs: dict | None = None
    metadata_json: dict | None = None


class ExperiencePageUpdate(BaseModel):
    model_config = _FORBID

    slug: str | None = Field(default=None, max_length=200)
    title: str | None = Field(default=None, max_length=255)
    summary: str | None = None
    page_type: PageType | None = None
    locale: str | None = Field(default=None, max_length=16)
    text_direction: str | None = Field(
        default=None, max_length=3, pattern="^(rtl|ltr)$"
    )
    sort_order: int | None = Field(default=None, ge=0)
    is_home: bool | None = None
    content_shell: dict | None = None
    seo_shell: dict | None = None
    external_refs: dict | None = None
    metadata_json: dict | None = None
    version: int = Field(ge=1)


class ExperiencePageRead(ORMBase):
    id: UUID
    tenant_id: UUID
    workspace_id: UUID
    site_id: UUID
    code: str
    slug: str
    title: str
    summary: str | None
    page_type: PageType
    publish_status: PublishStatus
    locale: str
    text_direction: str
    sort_order: int
    is_home: bool
    published_at: datetime | None
    archived_at: datetime | None
    content_shell: dict | None
    seo_shell: dict | None
    external_refs: dict | None
    metadata_json: dict | None
    version: int
    is_deleted: bool
    created_by: str | None
    updated_by: str | None
    created_at: datetime
    updated_at: datetime


class ExperienceSiteDomainCreate(BaseModel):
    model_config = _FORBID

    workspace_id: UUID
    site_id: UUID
    host: str = Field(max_length=255)
    domain_kind: DomainKind = DomainKind.SUBDOMAIN
    is_primary: bool = False
    path_prefix: str | None = Field(default=None, max_length=100)
    metadata_json: dict | None = None


class ExperienceSiteDomainUpdate(BaseModel):
    model_config = _FORBID

    host: str | None = Field(default=None, max_length=255)
    domain_kind: DomainKind | None = None
    is_primary: bool | None = None
    is_verified: bool | None = None
    path_prefix: str | None = Field(default=None, max_length=100)
    metadata_json: dict | None = None


class ExperienceSiteDomainRead(ORMBase):
    id: UUID
    tenant_id: UUID
    workspace_id: UUID
    site_id: UUID
    host: str
    domain_kind: DomainKind
    is_primary: bool
    is_verified: bool
    verification_token: str | None
    path_prefix: str | None
    metadata_json: dict | None
    is_deleted: bool
    created_by: str | None
    updated_by: str | None
    created_at: datetime
    updated_at: datetime
