# #region DashboardSchemas [C:1] [TYPE Module] [SEMANTICS pydantic, dashboard, api, dto, git-status]
# @BRIEF DTO classes for the Dashboard Hub API.
# @LAYER Infrastructure
# @RELATION DEPENDS_ON -> [EXT:Library:pydantic]

from typing import Literal

from pydantic import BaseModel, ConfigDict, Field


# #region GitStatus [C:1] [TYPE DataClass]
# @BRIEF DTO for dashboard Git synchronization status.
class GitStatus(BaseModel):
    branch: str | None = None
    sync_status: str | None = Field(None, pattern="^OK|DIFF|NO_REPO|ERROR$")
    has_repo: bool | None = None
    has_changes_for_commit: bool | None = None


# #endregion GitStatus


# #region LastTask [C:2] [TYPE DataClass]
# @BRIEF DTO for the most recent background task associated with a dashboard.
class LastTask(BaseModel):
    task_id: str | None = None
    status: str | None = Field(
        None,
        pattern="^PENDING|RUNNING|SUCCESS|FAILED|ERROR|AWAITING_INPUT|WAITING_INPUT|AWAITING_MAPPING$",
    )
    validation_status: str | None = Field(None, pattern="^PASS|FAIL|WARN|UNKNOWN$")


# #endregion LastTask


# #region DashboardItem [C:1] [TYPE DataClass]
# @BRIEF DTO representing a single dashboard with projected metadata.
class DashboardItem(BaseModel):
    id: int
    title: str
    slug: str | None = None
    url: str | None = None
    last_modified: str | None = None
    created_by: str | None = None
    modified_by: str | None = None
    owners: list[str] | None = None
    git_status: GitStatus | None = None
    last_task: LastTask | None = None


# #endregion DashboardItem


# #region EffectiveProfileFilter [C:1] [TYPE DataClass]
# @BRIEF Metadata about applied profile filters for UI context.
class EffectiveProfileFilter(BaseModel):
    applied: bool
    source_page: Literal["dashboards_main", "other"] = "dashboards_main"
    override_show_all: bool = False
    username: str | None = None
    match_logic: Literal["owners_or_modified_by", "slug_only", "owners_or_modified_by+slug_only"] | None = None


# #endregion EffectiveProfileFilter


# #region DashboardsResponse [C:1] [TYPE DataClass]
# @BRIEF Envelope DTO for paginated dashboards list.
class DashboardsResponse(BaseModel):
    dashboards: list[DashboardItem]
    total: int
    page: int
    page_size: int
    total_pages: int
    effective_profile_filter: EffectiveProfileFilter | None = None


# #endregion DashboardsResponse


# #region DashboardChartItem [C:1] [TYPE DataClass]
# @BRIEF DTO for a chart linked to a dashboard.
class DashboardChartItem(BaseModel):
    id: int
    title: str
    viz_type: str | None = None
    dataset_id: int | None = None
    last_modified: str | None = None
    overview: str | None = None


# #endregion DashboardChartItem


# #region DashboardDatasetItem [C:1] [TYPE DataClass]
# @BRIEF DTO for a dataset associated with a dashboard.
class DashboardDatasetItem(BaseModel):
    model_config = ConfigDict(protected_namespaces=())
    id: int
    table_name: str
    schema: str | None = None
    database: str
    last_modified: str | None = None
    overview: str | None = None


# #endregion DashboardDatasetItem


# #region DashboardDetailResponse [C:1] [TYPE DataClass]
# @BRIEF Detailed dashboard metadata including children.
class DashboardDetailResponse(BaseModel):
    id: int
    title: str
    slug: str | None = None
    url: str | None = None
    description: str | None = None
    last_modified: str | None = None
    published: bool | None = None
    charts: list[DashboardChartItem]
    datasets: list[DashboardDatasetItem]
    chart_count: int
    dataset_count: int


# #endregion DashboardDetailResponse


# #region DashboardTaskHistoryItem [C:1] [TYPE DataClass]
# @BRIEF Individual history record entry.
class DashboardTaskHistoryItem(BaseModel):
    id: str
    plugin_id: str
    status: str
    validation_status: str | None = None
    started_at: str | None = None
    finished_at: str | None = None
    env_id: str | None = None
    summary: str | None = None


# #endregion DashboardTaskHistoryItem


# #region DashboardTaskHistoryResponse [C:1] [TYPE DataClass]
# @BRIEF Collection DTO for task history.
class DashboardTaskHistoryResponse(BaseModel):
    dashboard_id: int
    items: list[DashboardTaskHistoryItem]


# #endregion DashboardTaskHistoryResponse


# #region DatabaseMapping [C:2] [TYPE DataClass]
# @BRIEF DTO for cross-environment database ID mapping.
class DatabaseMapping(BaseModel):
    source_db: str
    target_db: str
    source_db_uuid: str | None = None
    target_db_uuid: str | None = None
    confidence: float


# #endregion DatabaseMapping


# #region DatabaseMappingsResponse [C:1] [TYPE DataClass]
# @BRIEF Wrapper for database mappings.
class DatabaseMappingsResponse(BaseModel):
    mappings: list[DatabaseMapping]


# #endregion DatabaseMappingsResponse


# #region MigrateRequest [C:1] [TYPE DataClass]
# @BRIEF DTO for dashboard migration requests.
class MigrateRequest(BaseModel):
    source_env_id: str = Field(..., description="Source environment ID")
    target_env_id: str = Field(..., description="Target environment ID")
    dashboard_ids: list[int] = Field(
        ..., description="List of dashboard IDs to migrate"
    )
    db_mappings: dict[str, str] | None = Field(
        None, description="Database mappings for migration"
    )
    replace_db_config: bool = Field(False, description="Replace database configuration")


# #endregion MigrateRequest


# #region TaskResponse [C:1] [TYPE DataClass]
# @BRIEF DTO for async task ID return.
class TaskResponse(BaseModel):
    task_id: str


# #endregion TaskResponse


# #region BackupRequest [C:1] [TYPE DataClass]
# @BRIEF DTO for dashboard backup requests.
class BackupRequest(BaseModel):
    env_id: str = Field(..., description="Environment ID")
    dashboard_ids: list[int] = Field(..., description="List of dashboard IDs to backup")
    schedule: str | None = Field(
        None, description="Cron schedule for recurring backups (e.g., '0 0 * * *')"
    )


# #endregion BackupRequest
# #endregion DashboardSchemas
