"""Analytics DTOs — Phase 11.10."""
from datetime import datetime
from uuid import UUID

from pydantic import BaseModel, ConfigDict, Field

from app.models.types import AnalyticsReportStatus, AnalyticsSnapshotStatus
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 AnalyticsReportDefinitionCreate(_Create):
    workspace_id: UUID
    site_id: UUID | None = None
    code: str = Field(min_length=1, max_length=50)
    name: str = Field(min_length=1, max_length=255)
    metric_keys: list[str]
    filters: dict | None = None
    status: AnalyticsReportStatus = AnalyticsReportStatus.DRAFT


class AnalyticsReportDefinitionUpdate(_Update):
    name: str | None = Field(default=None, min_length=1, max_length=255)
    metric_keys: list[str] | None = None
    filters: dict | None = None
    status: AnalyticsReportStatus | None = None


class AnalyticsReportDefinitionRead(ORMBase):
    id: UUID
    tenant_id: UUID
    workspace_id: UUID
    site_id: UUID | None
    code: str
    name: str
    metric_keys: list[str]
    filters: dict | None
    status: AnalyticsReportStatus
    metadata_json: dict | None
    version: int
    is_deleted: bool
    created_by: str | None
    updated_by: str | None
    created_at: datetime
    updated_at: datetime


class AnalyticsReportDefinitionListResponse(BaseModel):
    items: list[AnalyticsReportDefinitionRead]
    total: int
    page: int
    page_size: int


class AnalyticsSnapshotRefresh(BaseModel):
    model_config = _FORBID
    report_id: UUID
    workspace_id: UUID | None = None
    site_id: UUID | None = None
    period_start: str = Field(min_length=1, max_length=32)
    period_end: str = Field(min_length=1, max_length=32)


class AnalyticsSnapshotCreate(BaseModel):
    model_config = _FORBID
    report_id: UUID
    workspace_id: UUID | None = None
    site_id: UUID | None = None
    period_start: str | None = Field(default=None, max_length=32)
    period_end: str | None = Field(default=None, max_length=32)
    metrics: dict
    status: AnalyticsSnapshotStatus = AnalyticsSnapshotStatus.ACTIVE


class AnalyticsSnapshotUpdate(BaseModel):
    model_config = _FORBID
    period_start: str | None = Field(default=None, max_length=32)
    period_end: str | None = Field(default=None, max_length=32)
    metrics: dict | None = None
    status: AnalyticsSnapshotStatus | None = None
    version: int = Field(ge=1)


class AnalyticsSnapshotRead(ORMBase):
    id: UUID
    tenant_id: UUID
    report_id: UUID
    workspace_id: UUID | None
    site_id: UUID | None
    period_start: str | None
    period_end: str | None
    metrics: dict
    status: AnalyticsSnapshotStatus
    version: int
    is_deleted: bool
    created_by: str | None
    updated_by: str | None
    created_at: datetime
    updated_at: datetime


class AnalyticsSnapshotListResponse(BaseModel):
    items: list[AnalyticsSnapshotRead]
    total: int
    page: int
    page_size: int
