"""Schemas for the penyetoran (per-shift cash deposit) module."""

from datetime import date, datetime
from decimal import Decimal
from typing import Literal, Optional

from pydantic import BaseModel, ConfigDict

from app.models.penyetoran import StatusPenyetoran, StatusPenyetoranBatch


# ---------------------------------------------------------------------------
# Daily summary building blocks
# ---------------------------------------------------------------------------

class ShiftProductSummary(BaseModel):
    """Aggregated sales numbers for one product inside one shift."""

    produk_id: int
    produk_nama: str
    produk_kode: str
    volume_digital: Decimal
    volume_manual: Decimal
    volume_final: Decimal  # sum of penjualan_nozzle.volume (based on primary_teller)
    nilai: Decimal
    harga_jual: Decimal    # representative price (from first nozzle row for that produk)


class ShiftSummary(BaseModel):
    """Aggregated summary for a single shift on the requested date."""

    laporan_shift_id: int
    shift_id: int
    shift_nama: str
    status: str
    products: list[ShiftProductSummary]
    total_volume_digital: Decimal
    total_volume_manual: Decimal
    total_volume_final: Decimal
    total_nilai: Decimal


class ProductTotal(BaseModel):
    """Cross-shift total for a single product."""

    produk_id: int
    produk_nama: str
    produk_kode: str
    volume_digital: Decimal
    volume_manual: Decimal
    volume_final: Decimal
    nilai: Decimal
    harga_jual: Decimal


class DailySummary(BaseModel):
    """Full daily summary used to pre-populate the penyetoran create form."""

    tanggal: date
    shifts: list[ShiftSummary]
    products_total: list[ProductTotal]
    grand_total_volume_digital: Decimal
    grand_total_volume_manual: Decimal
    grand_total_volume_final: Decimal
    grand_total_nilai: Decimal
    total_expenses: Decimal
    suggested_setor: Decimal  # grand_total_nilai - total_expenses
    existing_penyetoran_id: Optional[int] = None
    existing_penyetoran_status: Optional[str] = None


# ---------------------------------------------------------------------------
# CRUD request schemas
# ---------------------------------------------------------------------------

class PenyetoranUpdate(BaseModel):
    """Payload to update a DRAFT penyetoran (only catatan is editable)."""

    catatan: Optional[str] = None


class PenyetoranBatchCreate(BaseModel):
    """Payload to create a batch from a list of draft penyetoran IDs."""

    penyetoran_ids: list[int]
    tanggal_from: date
    tanggal_to: date
    catatan: Optional[str] = None


class BatchReviewRequest(BaseModel):
    """Payload for manager approval of a submitted batch."""

    action: Literal["approve"]
    catatan_review: Optional[str] = None


# ---------------------------------------------------------------------------
# Response schemas
# ---------------------------------------------------------------------------

class PenyetoranResponse(BaseModel):
    """Full response for a single penyetoran record."""

    model_config = ConfigDict(from_attributes=True)

    id: int
    spbu_id: int
    laporan_shift_id: int
    tanggal: date
    shift_id: int
    shift_nama: Optional[str]  # from laporan_shift.shift.nama
    jumlah_kas: Decimal
    jumlah_non_kas: Decimal
    total_penjualan: Decimal
    catatan: Optional[str]
    bukti_url: Optional[str]
    status: str
    batch_id: Optional[int]
    created_by_name: Optional[str]
    created_at: datetime
    updated_at: datetime


class PenyetoranBatchResponse(BaseModel):
    """Full response for a single PenyetoranBatch."""

    model_config = ConfigDict(from_attributes=True)

    id: int
    spbu_id: int
    tanggal_from: date
    tanggal_to: date
    total_amount: Decimal
    catatan: Optional[str]
    status: str
    submitted_by_name: Optional[str]
    submitted_at: Optional[datetime]
    reviewed_by_name: Optional[str]
    reviewed_at: Optional[datetime]
    catatan_review: Optional[str]
    items: list[PenyetoranResponse]
    created_at: datetime
    updated_at: datetime
