import uuid
from datetime import date, datetime, time
from decimal import Decimal

from pydantic import BaseModel, Field

from app.models.time_entry import EntrySource, EntryStatus


class TimeEntryOut(BaseModel):
    model_config = {"from_attributes": True}

    id: uuid.UUID
    user_id: uuid.UUID
    date: date
    start_time: time
    end_time: time | None
    break_minutes: int
    break_start: time | None
    project_id: uuid.UUID | None
    note: str | None
    status: EntryStatus
    source: EntrySource
    approved_by: uuid.UUID | None
    correction_note: str | None
    worked_hours: float | None
    created_at: datetime
    updated_at: datetime


class TimeEntryWithWarnings(BaseModel):
    entry: TimeEntryOut
    warnings: list[str] = []


class StampInRequest(BaseModel):
    source: EntrySource = EntrySource.WEB
    project_id: uuid.UUID | None = None
    note: str | None = None


class StampOutRequest(BaseModel):
    note: str | None = None


class ManualEntryCreate(BaseModel):
    user_id: uuid.UUID | None = None  # MANAGER/HR können für andere setzen
    date: date
    start_time: time
    end_time: time
    break_minutes: int = Field(0, ge=0, le=600)
    project_id: uuid.UUID | None = None
    note: str | None = None
    source: EntrySource = EntrySource.MANUAL


class TimeEntryUpdate(BaseModel):
    start_time: time | None = None
    end_time: time | None = None
    break_minutes: int | None = Field(None, ge=0, le=600)
    project_id: uuid.UUID | None = None
    note: str | None = None
    correction_note: str | None = None


class RejectRequest(BaseModel):
    rejection_note: str | None = None


class TimeEntryListResponse(BaseModel):
    total: int
    items: list[TimeEntryOut]


class WorkScheduleOut(BaseModel):
    model_config = {"from_attributes": True}

    id: uuid.UUID
    company_id: uuid.UUID
    name: str
    mon_h: Decimal
    tue_h: Decimal
    wed_h: Decimal
    thu_h: Decimal
    fri_h: Decimal
    sat_h: Decimal
    sun_h: Decimal
    valid_from: date


class WorkScheduleCreate(BaseModel):
    name: str = Field(min_length=1, max_length=255)
    mon_h: Decimal = Field(Decimal("8.00"), ge=0, le=24)
    tue_h: Decimal = Field(Decimal("8.00"), ge=0, le=24)
    wed_h: Decimal = Field(Decimal("8.00"), ge=0, le=24)
    thu_h: Decimal = Field(Decimal("8.00"), ge=0, le=24)
    fri_h: Decimal = Field(Decimal("8.00"), ge=0, le=24)
    sat_h: Decimal = Field(Decimal("0.00"), ge=0, le=24)
    sun_h: Decimal = Field(Decimal("0.00"), ge=0, le=24)
    valid_from: date


class BalanceResponse(BaseModel):
    user_id: uuid.UUID
    period_start: date
    period_end: date
    total_hours_worked: float
    expected_hours: float
    overtime_hours: float
    approved_entries: int
    pending_entries: int
