"""AI content hook DTOs — Phase 11.10."""
from datetime import datetime
from uuid import UUID

from pydantic import BaseModel, ConfigDict, Field

from app.models.types import (
    AiContentDispatchStatus,
    AiContentHookStatus,
    AiContentTaskKind,
)
from app.schemas.common import ORMBase

_FORBID = ConfigDict(extra="forbid")


class _Create(BaseModel):
    model_config = _FORBID
    metadata_json: dict | None = None


class _Update(BaseModel):
    model_config = _FORBID
    metadata_json: dict | None = None
    version: int = Field(ge=1)


class AiContentHookRegistrationCreate(_Create):
    workspace_id: UUID
    site_id: UUID | None = None
    page_id: UUID | None = None
    code: str = Field(min_length=1, max_length=50)
    name: str = Field(min_length=1, max_length=255)
    task_kind: AiContentTaskKind
    external_ai_ref: str | None = Field(default=None, max_length=255)
    status: AiContentHookStatus = AiContentHookStatus.DRAFT
    config_json: dict | None = None


class AiContentHookRegistrationUpdate(_Update):
    name: str | None = Field(default=None, min_length=1, max_length=255)
    task_kind: AiContentTaskKind | None = None
    external_ai_ref: str | None = Field(default=None, max_length=255)
    status: AiContentHookStatus | None = None
    config_json: dict | None = None


class AiContentHookRegistrationRead(ORMBase):
    id: UUID
    tenant_id: UUID
    workspace_id: UUID
    site_id: UUID | None
    page_id: UUID | None
    code: str
    name: str
    task_kind: AiContentTaskKind
    external_ai_ref: str | None
    status: AiContentHookStatus
    config_json: 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 AiContentHookRegistrationListResponse(BaseModel):
    items: list[AiContentHookRegistrationRead]
    total: int
    page: int
    page_size: int


class AiContentDispatchCreate(BaseModel):
    model_config = _FORBID
    registration_id: UUID
    workspace_id: UUID
    site_id: UUID | None = None
    page_id: UUID | None = None
    task: str = Field(min_length=1, max_length=100)
    context_ref: str | None = Field(default=None, max_length=255)
    payload_json: dict | None = None


class AiContentDispatchUpdate(BaseModel):
    model_config = _FORBID
    task: str | None = Field(default=None, min_length=1, max_length=100)
    context_ref: str | None = Field(default=None, max_length=255)
    payload_json: dict | None = None
    status: AiContentDispatchStatus | None = None
    version: int = Field(ge=1)


class AiContentDispatchRead(ORMBase):
    id: UUID
    tenant_id: UUID
    registration_id: UUID
    workspace_id: UUID
    site_id: UUID | None
    page_id: UUID | None
    task: str
    context_ref: str | None
    payload_json: dict | None
    status: AiContentDispatchStatus
    response_json: dict | None
    generated_content_ref: str | None
    version: int
    is_deleted: bool
    created_by: str | None
    updated_by: str | None
    created_at: datetime
    updated_at: datetime


class AiContentDispatchListResponse(BaseModel):
    items: list[AiContentDispatchRead]
    total: int
    page: int
    page_size: int
