"""Coach & Staff schemas — Phase 9.3."""
from __future__ import annotations

from datetime import date, datetime, time
from uuid import UUID

from pydantic import BaseModel, Field

from app.models.types import AssignmentKind, LifecycleStatus, StaffKind
from app.schemas.common import ORMBase


class StaffCertificateCreate(BaseModel):
    sports_center_id: UUID
    coach_id: UUID
    name: str = Field(max_length=255)
    issuer: str | None = Field(default=None, max_length=255)
    certificate_number: str | None = Field(default=None, max_length=100)
    issued_on: date | None = None
    expires_on: date | None = None
    file_ref: str | None = Field(default=None, max_length=255)
    status: LifecycleStatus = LifecycleStatus.ACTIVE
    notes: str | None = None


class StaffCertificateUpdate(BaseModel):
    name: str | None = Field(default=None, max_length=255)
    issuer: str | None = Field(default=None, max_length=255)
    certificate_number: str | None = Field(default=None, max_length=100)
    issued_on: date | None = None
    expires_on: date | None = None
    file_ref: str | None = Field(default=None, max_length=255)
    status: LifecycleStatus | None = None
    notes: str | None = None


class StaffCertificateRead(ORMBase):
    id: UUID
    tenant_id: UUID
    sports_center_id: UUID
    coach_id: UUID
    name: str
    issuer: str | None
    certificate_number: str | None
    issued_on: date | None
    expires_on: date | None
    file_ref: str | None
    status: LifecycleStatus
    notes: str | None
    is_deleted: bool
    created_by: str | None
    updated_by: str | None


class StaffSkillCreate(BaseModel):
    sports_center_id: UUID
    coach_id: UUID
    skill_code: str = Field(max_length=50)
    skill_name: str = Field(max_length=255)
    level: int | None = None
    notes: str | None = None


class StaffSkillUpdate(BaseModel):
    skill_name: str | None = Field(default=None, max_length=255)
    level: int | None = None
    notes: str | None = None


class StaffSkillRead(ORMBase):
    id: UUID
    tenant_id: UUID
    sports_center_id: UUID
    coach_id: UUID
    skill_code: str
    skill_name: str
    level: int | None
    notes: str | None
    is_deleted: bool
    created_by: str | None
    updated_by: str | None


class StaffWorkingHoursCreate(BaseModel):
    sports_center_id: UUID
    coach_id: UUID
    branch_id: UUID | None = None
    day_of_week: int = Field(ge=0, le=6)
    starts_at: time
    ends_at: time
    is_active: bool = True


class StaffWorkingHoursUpdate(BaseModel):
    branch_id: UUID | None = None
    day_of_week: int | None = Field(default=None, ge=0, le=6)
    starts_at: time | None = None
    ends_at: time | None = None
    is_active: bool | None = None


class StaffWorkingHoursRead(ORMBase):
    id: UUID
    tenant_id: UUID
    sports_center_id: UUID
    coach_id: UUID
    branch_id: UUID | None
    day_of_week: int
    starts_at: time
    ends_at: time
    is_active: bool
    is_deleted: bool
    created_by: str | None
    updated_by: str | None


class StaffAvailabilityCreate(BaseModel):
    sports_center_id: UUID
    coach_id: UUID
    starts_at: datetime
    ends_at: datetime
    is_available: bool = True
    reason: str | None = Field(default=None, max_length=255)
    metadata_json: dict | None = None


class StaffAvailabilityUpdate(BaseModel):
    starts_at: datetime | None = None
    ends_at: datetime | None = None
    is_available: bool | None = None
    reason: str | None = Field(default=None, max_length=255)
    metadata_json: dict | None = None


class StaffAvailabilityRead(ORMBase):
    id: UUID
    tenant_id: UUID
    sports_center_id: UUID
    coach_id: UUID
    starts_at: datetime
    ends_at: datetime
    is_available: bool
    reason: str | None
    metadata_json: dict | None
    is_deleted: bool
    created_by: str | None
    updated_by: str | None


class StaffAssignmentCreate(BaseModel):
    sports_center_id: UUID
    coach_id: UUID
    assignment_kind: AssignmentKind
    member_id: UUID | None = None
    sport_id: UUID | None = None
    program_ref: str | None = Field(default=None, max_length=100)
    starts_on: date | None = None
    ends_on: date | None = None
    status: LifecycleStatus = LifecycleStatus.ACTIVE
    notes: str | None = None


class StaffAssignmentUpdate(BaseModel):
    assignment_kind: AssignmentKind | None = None
    member_id: UUID | None = None
    sport_id: UUID | None = None
    program_ref: str | None = Field(default=None, max_length=100)
    starts_on: date | None = None
    ends_on: date | None = None
    status: LifecycleStatus | None = None
    notes: str | None = None


class StaffAssignmentRead(ORMBase):
    id: UUID
    tenant_id: UUID
    sports_center_id: UUID
    coach_id: UUID
    assignment_kind: AssignmentKind
    member_id: UUID | None
    sport_id: UUID | None
    program_ref: str | None
    starts_on: date | None
    ends_on: date | None
    status: LifecycleStatus
    notes: str | None
    is_deleted: bool
    created_by: str | None
    updated_by: str | None
