"""
Pydantic schemas for admin endpoints.
"""
from datetime import datetime
from typing import Optional, List
from pydantic import BaseModel, Field

from app.schemas.cv import PersonalInfo, ExperienceInput, FormationInput, LanguageInput


class AdminUser(BaseModel):
    """Single user entry in the admin users list."""
    id: str
    firstname: str
    lastname: str
    phone: Optional[str] = None
    profession: Optional[str] = None
    city: Optional[str] = None
    role: str
    email: Optional[str] = None
    email_confirmed_at: Optional[datetime] = None
    has_cv: bool
    created_at: datetime


class AdminUsersResponse(BaseModel):
    """Response shape for GET /admin/users."""
    users: List[AdminUser]
    total: int


class AdminStatsResponse(BaseModel):
    """Response shape for GET /admin/stats — counts for DashboardTab KPIs."""
    total_candidates:  int
    total_admins:      int
    total_cvs:         int
    total_active_30d:  int  # users with last_sign_in_at within the last 30 days


class OrphanCv(BaseModel):
    """
    A CV row whose owning user has been deleted (cvs.user_id IS NULL,
    set by the cvs_user_id_fkey ON DELETE SET NULL constraint).

    No join with profiles is possible — the owning profile row no longer
    exists. personal_info is kept (built at CV creation time from the
    profile that existed then) so an orphaned CV can still be identified
    visually. experiences/formations/skills/languages are now included
    (previously omitted to keep the list light) so the admin UI can open
    an orphaned CV in the same CVViewerModal used everywhere else in the
    app, instead of a second, data-incomplete preview component.

    personal_info/experiences/formations/languages reuse
    app.schemas.cv models (all CamelCaseModel) so nested keys serialize
    as camelCase on the wire, matching every other CV payload the
    frontend already consumes (CVOutput).
    """
    id:            str
    personal_info: Optional[PersonalInfo] = None
    profil:        str
    experiences:   List[ExperienceInput] = Field(default_factory=list)
    formations:    List[FormationInput] = Field(default_factory=list)
    skills:        List[str] = Field(default_factory=list)
    languages:     List[LanguageInput] = Field(default_factory=list)
    pdf_url:       Optional[str] = None
    created_at:    datetime
    updated_at:    datetime


class OrphanCvsResponse(BaseModel):
    """Response shape for GET /admin/cvs/orphans."""
    cvs:   List[OrphanCv]
    total: int
