"""Member Management DTOs — Phase 9.2."""
from __future__ import annotations

from datetime import date, datetime
from uuid import UUID

from pydantic import BaseModel, Field

from app.models.types import (
    CardKind,
    CardStatus,
    DocumentKind,
    FamilyRelationship,
    MemberStatus,
    WaiverStatus,
)
from app.schemas.common import ORMBase


class MemberCreate(BaseModel):
    sports_center_id: UUID
    display_name: str = Field(max_length=255)
    member_number: str | None = Field(default=None, max_length=50)
    branch_id: UUID | None = None
    first_name: str | None = Field(default=None, max_length=100)
    last_name: str | None = Field(default=None, max_length=100)
    email: str | None = Field(default=None, max_length=255)
    mobile: str | None = Field(default=None, max_length=50)
    birth_date: date | None = None
    gender: str | None = Field(default=None, max_length=32)
    status: MemberStatus = MemberStatus.ACTIVE
    external_user_ref: str | None = Field(default=None, max_length=100)
    external_customer_ref: str | None = Field(default=None, max_length=100)
    external_crm_contact_ref: str | None = Field(default=None, max_length=100)
    preferred_language: str | None = Field(default=None, max_length=16)
    notes: str | None = None
    profile: dict | None = None
    custom_fields: dict | None = None


class MemberUpdate(BaseModel):
    display_name: str | None = Field(default=None, max_length=255)
    branch_id: UUID | None = None
    first_name: str | None = Field(default=None, max_length=100)
    last_name: str | None = Field(default=None, max_length=100)
    email: str | None = Field(default=None, max_length=255)
    mobile: str | None = Field(default=None, max_length=50)
    birth_date: date | None = None
    gender: str | None = Field(default=None, max_length=32)
    status: MemberStatus | None = None
    external_user_ref: str | None = Field(default=None, max_length=100)
    external_customer_ref: str | None = Field(default=None, max_length=100)
    external_crm_contact_ref: str | None = Field(default=None, max_length=100)
    preferred_language: str | None = Field(default=None, max_length=16)
    notes: str | None = None
    profile: dict | None = None
    custom_fields: dict | None = None
    version: int | None = None


class MemberRead(ORMBase):
    id: UUID
    tenant_id: UUID
    sports_center_id: UUID
    branch_id: UUID | None
    member_number: str
    display_name: str
    first_name: str | None
    last_name: str | None
    email: str | None
    mobile: str | None
    birth_date: date | None
    gender: str | None
    status: MemberStatus
    external_user_ref: str | None
    external_customer_ref: str | None
    external_crm_contact_ref: str | None
    preferred_language: str | None
    notes: str | None
    profile: dict | None
    custom_fields: dict | None
    version: int
    is_deleted: bool
    created_by: str | None
    updated_by: str | None
    created_at: datetime
    updated_at: datetime


class FamilyMemberCreate(BaseModel):
    sports_center_id: UUID
    member_id: UUID
    display_name: str = Field(max_length=255)
    relationship: FamilyRelationship = FamilyRelationship.OTHER
    related_member_id: UUID | None = None
    email: str | None = Field(default=None, max_length=255)
    mobile: str | None = Field(default=None, max_length=50)
    birth_date: date | None = None
    is_emergency_eligible: bool = False
    notes: str | None = None


class FamilyMemberUpdate(BaseModel):
    display_name: str | None = Field(default=None, max_length=255)
    relationship: FamilyRelationship | None = None
    related_member_id: UUID | None = None
    email: str | None = Field(default=None, max_length=255)
    mobile: str | None = Field(default=None, max_length=50)
    birth_date: date | None = None
    is_emergency_eligible: bool | None = None
    notes: str | None = None


class FamilyMemberRead(ORMBase):
    id: UUID
    tenant_id: UUID
    sports_center_id: UUID
    member_id: UUID
    related_member_id: UUID | None
    display_name: str
    relationship: FamilyRelationship
    email: str | None
    mobile: str | None
    birth_date: date | None
    is_emergency_eligible: bool
    notes: str | None
    is_deleted: bool
    created_by: str | None
    updated_by: str | None
    created_at: datetime
    updated_at: datetime


class EmergencyContactCreate(BaseModel):
    sports_center_id: UUID
    member_id: UUID
    display_name: str = Field(max_length=255)
    phone: str = Field(max_length=50)
    relationship: str | None = Field(default=None, max_length=64)
    alternate_phone: str | None = Field(default=None, max_length=50)
    email: str | None = Field(default=None, max_length=255)
    is_primary: bool = False
    notes: str | None = None


class EmergencyContactUpdate(BaseModel):
    display_name: str | None = Field(default=None, max_length=255)
    phone: str | None = Field(default=None, max_length=50)
    relationship: str | None = Field(default=None, max_length=64)
    alternate_phone: str | None = Field(default=None, max_length=50)
    email: str | None = Field(default=None, max_length=255)
    is_primary: bool | None = None
    notes: str | None = None


class EmergencyContactRead(ORMBase):
    id: UUID
    tenant_id: UUID
    sports_center_id: UUID
    member_id: UUID
    display_name: str
    relationship: str | None
    phone: str
    alternate_phone: str | None
    email: str | None
    is_primary: bool
    notes: str | None
    is_deleted: bool
    created_by: str | None
    updated_by: str | None
    created_at: datetime
    updated_at: datetime


class MedicalInformationUpsert(BaseModel):
    sports_center_id: UUID
    member_id: UUID
    blood_type: str | None = Field(default=None, max_length=16)
    allergies: list | None = None
    conditions: list | None = None
    medications: list | None = None
    clearance_status: str | None = Field(default=None, max_length=32)
    clearance_expires_on: date | None = None
    physician_name: str | None = Field(default=None, max_length=255)
    physician_phone: str | None = Field(default=None, max_length=50)
    notes: str | None = None
    document_file_ref: str | None = Field(default=None, max_length=255)
    is_confidential: bool = True


class MedicalInformationRead(ORMBase):
    id: UUID
    tenant_id: UUID
    sports_center_id: UUID
    member_id: UUID
    blood_type: str | None
    allergies: list | None
    conditions: list | None
    medications: list | None
    clearance_status: str | None
    clearance_expires_on: date | None
    physician_name: str | None
    physician_phone: str | None
    notes: str | None
    document_file_ref: str | None
    is_confidential: bool
    is_deleted: bool
    created_by: str | None
    updated_by: str | None
    created_at: datetime
    updated_at: datetime


class MembershipCardCreate(BaseModel):
    sports_center_id: UUID
    member_id: UUID
    membership_id: UUID | None = None
    kind: CardKind = CardKind.QR
    card_number: str | None = Field(default=None, max_length=64)
    status: CardStatus = CardStatus.ACTIVE
    issued_on: date | None = None
    expires_on: date | None = None
    metadata_json: dict | None = None


class MembershipCardUpdate(BaseModel):
    status: CardStatus | None = None
    membership_id: UUID | None = None
    expires_on: date | None = None
    metadata_json: dict | None = None


class MembershipCardRead(ORMBase):
    id: UUID
    tenant_id: UUID
    sports_center_id: UUID
    member_id: UUID
    membership_id: UUID | None
    card_number: str
    kind: CardKind
    status: CardStatus
    qr_payload: str | None
    barcode_payload: str | None
    issued_on: date | None
    expires_on: date | None
    metadata_json: dict | None
    is_deleted: bool
    created_by: str | None
    updated_by: str | None
    created_at: datetime
    updated_at: datetime


class DigitalMembershipCreate(BaseModel):
    sports_center_id: UUID
    member_id: UUID
    membership_id: UUID | None = None
    pass_code: str | None = Field(default=None, max_length=64)
    status: CardStatus = CardStatus.ACTIVE
    token_ref: str | None = Field(default=None, max_length=255)
    deep_link: str | None = Field(default=None, max_length=512)
    expires_at: datetime | None = None
    metadata_json: dict | None = None


class DigitalMembershipUpdate(BaseModel):
    status: CardStatus | None = None
    membership_id: UUID | None = None
    token_ref: str | None = Field(default=None, max_length=255)
    deep_link: str | None = Field(default=None, max_length=512)
    expires_at: datetime | None = None
    metadata_json: dict | None = None


class DigitalMembershipRead(ORMBase):
    id: UUID
    tenant_id: UUID
    sports_center_id: UUID
    member_id: UUID
    membership_id: UUID | None
    pass_code: str
    status: CardStatus
    token_ref: str | None
    deep_link: str | None
    issued_at: datetime | None
    expires_at: datetime | None
    metadata_json: dict | None
    is_deleted: bool
    created_by: str | None
    updated_by: str | None
    created_at: datetime
    updated_at: datetime


class WaiverCreate(BaseModel):
    sports_center_id: UUID
    member_id: UUID
    title: str = Field(max_length=255)
    membership_id: UUID | None = None
    waiver_version: str = Field(default="1", max_length=32)
    status: WaiverStatus = WaiverStatus.PENDING
    expires_on: date | None = None
    document_file_ref: str | None = Field(default=None, max_length=255)
    notes: str | None = None


class WaiverSignRequest(BaseModel):
    signature_ref: str | None = Field(default=None, max_length=255)
    document_file_ref: str | None = Field(default=None, max_length=255)


class WaiverUpdate(BaseModel):
    title: str | None = Field(default=None, max_length=255)
    waiver_version: str | None = Field(default=None, max_length=32)
    status: WaiverStatus | None = None
    expires_on: date | None = None
    document_file_ref: str | None = Field(default=None, max_length=255)
    notes: str | None = None


class WaiverRead(ORMBase):
    id: UUID
    tenant_id: UUID
    sports_center_id: UUID
    member_id: UUID
    membership_id: UUID | None
    title: str
    waiver_version: str
    status: WaiverStatus
    signed_at: datetime | None
    expires_on: date | None
    document_file_ref: str | None
    signature_ref: str | None
    notes: str | None
    is_deleted: bool
    created_by: str | None
    updated_by: str | None
    created_at: datetime
    updated_at: datetime


class MemberDocumentCreate(BaseModel):
    sports_center_id: UUID
    member_id: UUID
    title: str = Field(max_length=255)
    file_ref: str = Field(max_length=255)
    kind: DocumentKind = DocumentKind.OTHER
    mime_type: str | None = Field(default=None, max_length=128)
    size_bytes: int | None = None
    notes: str | None = None
    metadata_json: dict | None = None


class MemberDocumentUpdate(BaseModel):
    title: str | None = Field(default=None, max_length=255)
    kind: DocumentKind | None = None
    notes: str | None = None
    metadata_json: dict | None = None


class MemberDocumentRead(ORMBase):
    id: UUID
    tenant_id: UUID
    sports_center_id: UUID
    member_id: UUID
    title: str
    kind: DocumentKind
    file_ref: str
    mime_type: str | None
    size_bytes: int | None
    notes: str | None
    metadata_json: dict | None
    is_deleted: bool
    created_by: str | None
    updated_by: str | None
    created_at: datetime
    updated_at: datetime


class MembershipAssignMemberRequest(BaseModel):
    member_id: UUID
    version: int | None = None


class MembershipFreezeRequest(BaseModel):
    freeze_starts_on: date | None = None
    freeze_ends_on: date | None = None
    version: int | None = None


class MembershipStatusChangeRequest(BaseModel):
    version: int | None = None
