"""Clinic management DTOs — Phase 13.3."""
from __future__ import annotations

from datetime import datetime
from uuid import UUID

from pydantic import BaseModel, ConfigDict, Field

from app.models.types import ClinicPolicyKind, LifecycleStatus
from app.schemas.common import ORMBase

_FORBID = ConfigDict(extra="forbid")


class ClinicServiceCreate(BaseModel):
    model_config = _FORBID
    clinic_id: UUID
    department_id: UUID | None = None
    code: str = Field(max_length=50)
    name: str = Field(max_length=255)
    description: str | None = None
    duration_minutes: int = Field(default=30, ge=5, le=480)
    pricing_ref: str | None = Field(default=None, max_length=100)
    status: LifecycleStatus = LifecycleStatus.ACTIVE
    metadata_json: dict | None = None


class ClinicServiceRead(ORMBase):
    id: UUID
    tenant_id: UUID
    clinic_id: UUID
    department_id: UUID | None
    code: str
    name: str
    description: str | None
    duration_minutes: int
    pricing_ref: str | None
    status: LifecycleStatus
    metadata_json: dict | None
    version: int
    is_deleted: bool
    created_at: datetime
    updated_at: datetime


class StaffAssignmentCreate(BaseModel):
    model_config = _FORBID
    clinic_id: UUID
    department_id: UUID | None = None
    doctor_id: UUID
    code: str = Field(max_length=50)
    role_code: str = Field(max_length=50)
    is_primary: bool = False
    metadata_json: dict | None = None


class StaffAssignmentRead(ORMBase):
    id: UUID
    tenant_id: UUID
    clinic_id: UUID
    department_id: UUID | None
    doctor_id: UUID
    code: str
    role_code: str
    is_primary: bool
    metadata_json: dict | None
    is_deleted: bool
    created_at: datetime
    updated_at: datetime


class ClinicPolicyCreate(BaseModel):
    model_config = _FORBID
    clinic_id: UUID
    code: str = Field(max_length=50)
    name: str = Field(max_length=255)
    kind: ClinicPolicyKind
    booking_lead_time_hours: int = Field(default=24, ge=0, le=720)
    cancellation_window_hours: int = Field(default=12, ge=0, le=720)
    privacy_flags: dict | None = None
    rules: dict | None = None
    status: LifecycleStatus = LifecycleStatus.ACTIVE


class ClinicPolicyRead(ORMBase):
    id: UUID
    tenant_id: UUID
    clinic_id: UUID
    code: str
    name: str
    kind: ClinicPolicyKind
    booking_lead_time_hours: int
    cancellation_window_hours: int
    privacy_flags: dict | None
    rules: dict | None
    status: LifecycleStatus
    version: int
    is_deleted: bool
    created_at: datetime
    updated_at: datetime


class OperatingHoursPolicyCreate(BaseModel):
    model_config = _FORBID
    clinic_id: UUID
    branch_id: UUID | None = None
    code: str = Field(max_length=50)
    name: str = Field(max_length=255)
    weekly_hours: dict
    exceptions: dict | None = None
    timezone: str = Field(default="Asia/Tehran", max_length=64)


class OperatingHoursPolicyRead(ORMBase):
    id: UUID
    tenant_id: UUID
    clinic_id: UUID
    branch_id: UUID | None
    code: str
    name: str
    weekly_hours: dict
    exceptions: dict | None
    timezone: str
    is_deleted: bool
    created_at: datetime
    updated_at: datetime
