"""Consumer connector, widget & notify DTOs — Phase 11.9."""
from datetime import datetime
from uuid import UUID

from pydantic import BaseModel, ConfigDict, Field

from app.models.types import (
    ConnectorDispatchDirection,
    ConnectorDispatchStatus,
    ConsumerConnectorKind,
    ConsumerConnectorStatus,
    NotifyDispatchStatus,
    WidgetDefinitionStatus,
    WidgetInstanceStatus,
    WidgetKind,
)
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 _Read(ORMBase):
    id: UUID
    tenant_id: UUID
    metadata_json: dict | None
    version: int
    is_deleted: bool
    created_by: str | None
    updated_by: str | None
    created_at: datetime
    updated_at: datetime


class ConsumerConnectorRegistrationCreate(_Create):
    workspace_id: UUID
    site_id: UUID | None = None
    page_id: UUID | None = None
    kind: ConsumerConnectorKind
    code: str = Field(min_length=1, max_length=50)
    name: str = Field(min_length=1, max_length=255)
    status: ConsumerConnectorStatus = ConsumerConnectorStatus.DRAFT
    external_service_ref: str | None = Field(default=None, max_length=255)
    entity_ref: str | None = Field(default=None, max_length=255)
    config_json: dict | None = None


class ConsumerConnectorRegistrationUpdate(_Update):
    name: str | None = Field(default=None, min_length=1, max_length=255)
    status: ConsumerConnectorStatus | None = None
    external_service_ref: str | None = Field(default=None, max_length=255)
    entity_ref: str | None = Field(default=None, max_length=255)
    config_json: dict | None = None


class ConsumerConnectorRegistrationRead(_Read):
    workspace_id: UUID
    site_id: UUID | None
    page_id: UUID | None
    kind: ConsumerConnectorKind
    code: str
    name: str
    status: ConsumerConnectorStatus
    external_service_ref: str | None
    entity_ref: str | None
    config_json: dict | None


class ConsumerConnectorRegistrationListResponse(BaseModel):
    items: list[ConsumerConnectorRegistrationRead]
    total: int
    page: int
    page_size: int


class ConsumerConnectorDispatchCreate(BaseModel):
    model_config = _FORBID
    registration_id: UUID
    workspace_id: UUID
    site_id: UUID | None = None
    page_id: UUID | None = None
    direction: ConnectorDispatchDirection
    event_type: str = Field(min_length=1, max_length=100)
    payload_ref: str | None = Field(default=None, max_length=255)
    notes: str | None = None


class ConsumerConnectorDispatchUpdate(BaseModel):
    model_config = _FORBID
    status: ConnectorDispatchStatus | None = None
    response_json: dict | None = None
    notes: str | None = None
    version: int = Field(ge=1)


class ConsumerConnectorDispatchRead(ORMBase):
    id: UUID
    tenant_id: UUID
    registration_id: UUID
    workspace_id: UUID
    site_id: UUID | None
    page_id: UUID | None
    direction: ConnectorDispatchDirection
    event_type: str
    payload_ref: str | None
    status: ConnectorDispatchStatus
    response_json: dict | None
    notes: str | None
    version: int
    is_deleted: bool
    created_by: str | None
    updated_by: str | None
    created_at: datetime
    updated_at: datetime


class ConsumerConnectorDispatchListResponse(BaseModel):
    items: list[ConsumerConnectorDispatchRead]
    total: int
    page: int
    page_size: int


class WidgetDefinitionCreate(_Create):
    workspace_id: UUID | None = None
    code: str = Field(min_length=1, max_length=50)
    widget_kind: WidgetKind
    name: str = Field(min_length=1, max_length=255)
    description: str | None = None
    status: WidgetDefinitionStatus = WidgetDefinitionStatus.DRAFT
    connector_kind: ConsumerConnectorKind | None = None
    default_config_json: dict | None = None
    permission_prefixes: list[str] | None = None


class WidgetDefinitionUpdate(_Update):
    name: str | None = Field(default=None, min_length=1, max_length=255)
    description: str | None = None
    status: WidgetDefinitionStatus | None = None
    connector_kind: ConsumerConnectorKind | None = None
    default_config_json: dict | None = None
    permission_prefixes: list[str] | None = None


class WidgetDefinitionRead(_Read):
    workspace_id: UUID | None
    code: str
    widget_kind: WidgetKind
    name: str
    description: str | None
    status: WidgetDefinitionStatus
    connector_kind: ConsumerConnectorKind | None
    default_config_json: dict | None
    permission_prefixes: list | None


class WidgetDefinitionListResponse(BaseModel):
    items: list[WidgetDefinitionRead]
    total: int
    page: int
    page_size: int


class WidgetInstanceCreate(_Create):
    workspace_id: UUID
    site_id: UUID
    page_id: UUID | None = None
    widget_definition_id: UUID
    widget_kind: WidgetKind
    code: str = Field(min_length=1, max_length=50)
    name: str = Field(min_length=1, max_length=255)
    status: WidgetInstanceStatus = WidgetInstanceStatus.DRAFT
    connector_registration_id: UUID | None = None
    entity_ref: str | None = Field(default=None, max_length=255)
    embed_ref: str = Field(min_length=1, max_length=255)
    config_json: dict | None = None


class WidgetInstanceUpdate(_Update):
    name: str | None = Field(default=None, min_length=1, max_length=255)
    status: WidgetInstanceStatus | None = None
    connector_registration_id: UUID | None = None
    entity_ref: str | None = Field(default=None, max_length=255)
    config_json: dict | None = None


class WidgetInstanceRead(_Read):
    workspace_id: UUID
    site_id: UUID
    page_id: UUID | None
    widget_definition_id: UUID
    widget_kind: WidgetKind
    code: str
    name: str
    status: WidgetInstanceStatus
    connector_registration_id: UUID | None
    entity_ref: str | None
    embed_ref: str
    config_json: dict | None


class WidgetInstanceListResponse(BaseModel):
    items: list[WidgetInstanceRead]
    total: int
    page: int
    page_size: int


class NotifyDispatchCreate(_Create):
    workspace_id: UUID
    site_id: UUID | None = None
    page_id: UUID | None = None
    widget_instance_id: UUID | None = None
    connector_registration_id: UUID | None = None
    channel: str = Field(min_length=1, max_length=50)
    template_key: str = Field(min_length=1, max_length=100)
    to_ref: str | None = Field(default=None, max_length=255)
    payload_json: dict | None = None


class NotifyDispatchUpdate(_Update):
    status: NotifyDispatchStatus | None = None
    communication_request_ref: str | None = Field(default=None, max_length=255)
    response_json: dict | None = None


class NotifyDispatchRead(_Read):
    workspace_id: UUID
    site_id: UUID | None
    page_id: UUID | None
    widget_instance_id: UUID | None
    connector_registration_id: UUID | None
    channel: str
    template_key: str
    communication_request_ref: str | None
    to_ref: str | None
    payload_json: dict | None
    status: NotifyDispatchStatus
    response_json: dict | None


class NotifyDispatchListResponse(BaseModel):
    items: list[NotifyDispatchRead]
    total: int
    page: int
    page_size: int
