"""GET /home/summary — the Studio's built-in Home dashboard, the landing page
for tenant admins and internal users after sign-in (Public Portal accounts
land on their portal instead). Every section beyond the personal ones is
None unless the caller's roles grant "view" on the module it summarizes, so
the page shows each person only what they could already open."""

import uuid
from datetime import date, datetime

from pydantic import BaseModel


class HomeNotification(BaseModel):
    id: uuid.UUID
    title: str
    message: str
    read: bool
    created_at: datetime


class HomeNotifications(BaseModel):
    unread: int
    recent: list[HomeNotification]


class HomeInstance(BaseModel):
    id: uuid.UUID
    workflow_id: uuid.UUID
    workflow_name: str
    current_step: str | None
    status: str
    started_at: datetime
    sla_due_at: datetime | None
    overdue: bool


class HomeMyWork(BaseModel):
    in_progress: int
    recent: list[HomeInstance]


class HomeWorkflows(BaseModel):
    in_progress: int
    overdue: int
    escalated: int
    completed_30d: int
    cancelled_30d: int
    started_30d: int
    recent: list[HomeInstance]


class HomeDayCount(BaseModel):
    day: date
    count: int


class HomeFormCount(BaseModel):
    form_id: uuid.UUID
    name: str
    count: int


class HomeSubmission(BaseModel):
    id: uuid.UUID
    form_id: uuid.UUID
    form_name: str
    submitted_at: datetime
    submitted_by_name: str | None
    from_portal: bool


class HomeSubmissions(BaseModel):
    total: int
    total_capped: bool = False  # there are more than total (record_sql.COUNT_CAP)
    last_7d: int
    prev_7d: int
    daily: list[HomeDayCount]  # the last 14 days, oldest first, zero-filled
    top_forms: list[HomeFormCount]  # last 30 days
    recent: list[HomeSubmission]


class HomeBuildCount(BaseModel):
    published: int
    draft: int


class HomeBuild(BaseModel):
    # None = the caller can't view that module.
    forms: HomeBuildCount | None = None
    workflows: HomeBuildCount | None = None
    pages: HomeBuildCount | None = None
    tasks: HomeBuildCount | None = None
    datasources: int | None = None
    datasource_records: int | None = None
    datasource_records_capped: bool = False  # there are more (record_sql.COUNT_CAP)
    dashboards: int | None = None
    scheduled_jobs_active: int | None = None


class HomePeople(BaseModel):
    internal_users: int
    portal_users: int
    vendors: int
    signed_in_7d: int
    inactive: int


class HomeActivity(BaseModel):
    action: str
    entity_type: str | None
    actor_name: str | None
    occurred_at: datetime


class HomeDashboardLink(BaseModel):
    id: uuid.UUID
    name: str


class HomeSummary(BaseModel):
    user_name: str
    tenant_name: str
    is_admin: bool
    generated_at: datetime
    notifications: HomeNotifications
    my_work: HomeMyWork
    workflows: HomeWorkflows | None
    submissions: HomeSubmissions | None
    build: HomeBuild
    people: HomePeople | None
    activity: list[HomeActivity] | None
    dashboards: list[HomeDashboardLink] | None
