"""Competition schemas — Phase 9.7."""
from __future__ import annotations

from datetime import date, datetime
from uuid import UUID

from pydantic import BaseModel, Field

from app.models.types import (
    CompetitionKind,
    CompetitionStatus,
    MatchStatus,
    MedalKind,
    RegistrationStatus,
    ResultOutcome,
)
from app.schemas.common import ORMBase


class CompetitionCreate(BaseModel):
    sports_center_id: UUID
    code: str = Field(max_length=50)
    name: str = Field(max_length=255)
    description: str | None = None
    competition_kind: CompetitionKind = CompetitionKind.TOURNAMENT
    sport_id: UUID | None = None
    facility_id: UUID | None = None
    status: CompetitionStatus = CompetitionStatus.DRAFT
    starts_on: date | None = None
    ends_on: date | None = None
    max_teams: int | None = None
    rules: dict | None = None
    metadata_json: dict | None = None


class CompetitionUpdate(BaseModel):
    name: str | None = Field(default=None, max_length=255)
    description: str | None = None
    competition_kind: CompetitionKind | None = None
    sport_id: UUID | None = None
    facility_id: UUID | None = None
    status: CompetitionStatus | None = None
    starts_on: date | None = None
    ends_on: date | None = None
    max_teams: int | None = None
    rules: dict | None = None
    metadata_json: dict | None = None
    version: int | None = None


class CompetitionRead(ORMBase):
    id: UUID
    tenant_id: UUID
    sports_center_id: UUID
    code: str
    name: str
    description: str | None
    competition_kind: CompetitionKind
    sport_id: UUID | None
    facility_id: UUID | None
    status: CompetitionStatus
    starts_on: date | None
    ends_on: date | None
    max_teams: int | None
    rules: dict | None
    metadata_json: dict | None
    version: int
    is_deleted: bool


class CompetitionTeamCreate(BaseModel):
    sports_center_id: UUID
    competition_id: UUID
    code: str = Field(max_length=50)
    name: str = Field(max_length=255)
    captain_member_id: UUID | None = None
    group_id: UUID | None = None
    metadata_json: dict | None = None


class CompetitionTeamRead(ORMBase):
    id: UUID
    tenant_id: UUID
    sports_center_id: UUID
    competition_id: UUID
    code: str
    name: str
    captain_member_id: UUID | None
    group_id: UUID | None
    metadata_json: dict | None
    is_deleted: bool


class CompetitionGroupCreate(BaseModel):
    sports_center_id: UUID
    competition_id: UUID
    code: str = Field(max_length=50)
    name: str = Field(max_length=255)
    sort_order: int = 0
    metadata_json: dict | None = None


class CompetitionGroupRead(ORMBase):
    id: UUID
    tenant_id: UUID
    sports_center_id: UUID
    competition_id: UUID
    code: str
    name: str
    sort_order: int
    metadata_json: dict | None
    is_deleted: bool


class CompetitionRegistrationCreate(BaseModel):
    sports_center_id: UUID
    competition_id: UUID
    member_id: UUID
    team_id: UUID | None = None
    notes: str | None = None


class CompetitionRegistrationConfirm(BaseModel):
    pass


class CompetitionRegistrationRead(ORMBase):
    id: UUID
    tenant_id: UUID
    sports_center_id: UUID
    competition_id: UUID
    member_id: UUID
    team_id: UUID | None
    status: RegistrationStatus
    registered_at: datetime
    notes: str | None
    is_deleted: bool


class CompetitionMatchCreate(BaseModel):
    sports_center_id: UUID
    competition_id: UUID
    code: str = Field(max_length=50)
    group_id: UUID | None = None
    home_team_id: UUID | None = None
    away_team_id: UUID | None = None
    facility_id: UUID | None = None
    scheduled_at: datetime | None = None
    round_number: int | None = None
    metadata_json: dict | None = None


class CompetitionMatchRead(ORMBase):
    id: UUID
    tenant_id: UUID
    sports_center_id: UUID
    competition_id: UUID
    group_id: UUID | None
    code: str
    home_team_id: UUID | None
    away_team_id: UUID | None
    facility_id: UUID | None
    scheduled_at: datetime | None
    status: MatchStatus
    round_number: int | None
    metadata_json: dict | None
    version: int
    is_deleted: bool


class CompetitionResultCreate(BaseModel):
    sports_center_id: UUID
    competition_id: UUID
    match_id: UUID
    home_score: float | None = None
    away_score: float | None = None
    winner_team_id: UUID | None = None
    outcome: ResultOutcome
    notes: str | None = None


class CompetitionResultRead(ORMBase):
    id: UUID
    tenant_id: UUID
    sports_center_id: UUID
    competition_id: UUID
    match_id: UUID
    home_score: float | None
    away_score: float | None
    winner_team_id: UUID | None
    outcome: ResultOutcome
    recorded_at: datetime
    notes: str | None
    is_deleted: bool


class CompetitionRankingRead(ORMBase):
    id: UUID
    tenant_id: UUID
    sports_center_id: UUID
    competition_id: UUID
    team_id: UUID | None
    member_id: UUID | None
    rank: int
    points: float
    wins: int
    losses: int
    draws: int
    metadata_json: dict | None
    is_deleted: bool


class CompetitionMedalCreate(BaseModel):
    sports_center_id: UUID
    competition_id: UUID
    member_id: UUID
    team_id: UUID | None = None
    medal_kind: MedalKind
    notes: str | None = None


class CompetitionMedalRead(ORMBase):
    id: UUID
    tenant_id: UUID
    sports_center_id: UUID
    competition_id: UUID
    member_id: UUID
    team_id: UUID | None
    medal_kind: MedalKind
    awarded_at: datetime
    notes: str | None
    is_deleted: bool


class CompetitionJudgeCreate(BaseModel):
    sports_center_id: UUID
    competition_id: UUID
    coach_id: UUID
    role: str = Field(max_length=100)
    match_id: UUID | None = None
    notes: str | None = None


class CompetitionJudgeRead(ORMBase):
    id: UUID
    tenant_id: UUID
    sports_center_id: UUID
    competition_id: UUID
    coach_id: UUID
    role: str
    match_id: UUID | None
    notes: str | None
    is_deleted: bool


class CompetitionCertificateCreate(BaseModel):
    sports_center_id: UUID
    competition_id: UUID
    member_id: UUID
    title: str = Field(max_length=255)
    file_ref: str | None = Field(default=None, max_length=255)
    notes: str | None = None


class CompetitionCertificateRead(ORMBase):
    id: UUID
    tenant_id: UUID
    sports_center_id: UUID
    competition_id: UUID
    member_id: UUID
    title: str
    file_ref: str | None
    issued_at: datetime
    notes: str | None
    is_deleted: bool
