from pydantic import BaseModel
from typing import Optional, List
from datetime import datetime


class TabEntryOut(BaseModel):
    id: int
    tab_id: int
    order_id: Optional[int] = None
    order_item_id: Optional[int] = None
    amount: float
    description: Optional[str] = None
    created_by_id: int
    created_by_name: Optional[str] = None
    created_at: datetime

    model_config = {"from_attributes": True}


class TabPaymentOut(BaseModel):
    id: int
    tab_id: int
    amount: float
    payment_method: Optional[str] = None
    received_by_id: int
    received_by_name: Optional[str] = None
    notes: Optional[str] = None
    created_at: datetime

    model_config = {"from_attributes": True}


class TabOut(BaseModel):
    id: int
    customer_id: int
    customer_name: Optional[str] = None
    customer_phone: Optional[str] = None
    status: str
    opened_at: datetime
    closed_at: Optional[datetime] = None
    closed_by_id: Optional[int] = None
    notes: Optional[str] = None
    balance: float = 0.0          # computed: sum(entries) - sum(payments)
    total_charged: float = 0.0
    total_paid: float = 0.0
    entries: List[TabEntryOut] = []
    payments: List[TabPaymentOut] = []

    model_config = {"from_attributes": True}


class TabPayRequest(BaseModel):
    amount: float
    payment_method: Optional[str] = None
    notes: Optional[str] = None


class TabForgiveRequest(BaseModel):
    reason: Optional[str] = None


class TabCloseRequest(BaseModel):
    notes: Optional[str] = None
