"""
Pydantic models for Trial Balance (Balanță de Verificare)
Maps to Oracle VBAL VIEW (exists in each company schema)
"""
from pydantic import BaseModel, Field
from typing import Optional, List
from decimal import Decimal

class TrialBalanceItem(BaseModel):
    """
    Individual trial balance record from VBAL VIEW
    Real structure from Oracle:
    - CONT: account number
    - DENUMIRE: account description
    - PRECDEB/PRECCRED: previous balance debit/credit
    - RULDEB/RULCRED: monthly movement debit/credit
    - SOLDDEB/SOLDCRED: final balance debit/credit
    """
    cont: str = Field(description="Număr cont contabil (CONT)")
    denumire: Optional[str] = Field(default="", description="Denumire cont (DENUMIRE)")
    sold_precedent_debit: Decimal = Field(description="Sold precedent debit (PRECDEB)", decimal_places=2)
    sold_precedent_credit: Decimal = Field(description="Sold precedent credit (PRECCRED)", decimal_places=2)
    rulaj_lunar_debit: Decimal = Field(description="Rulaj lunar debit (RULDEB)", decimal_places=2)
    rulaj_lunar_credit: Decimal = Field(description="Rulaj lunar credit (RULCRED)", decimal_places=2)
    sold_final_debit: Decimal = Field(description="Sold final debit (SOLDDEB)", decimal_places=2)
    sold_final_credit: Decimal = Field(description="Sold final credit (SOLDCRED)", decimal_places=2)

    class Config:
        from_attributes = True


class TrialBalanceFilters(BaseModel):
    """
    Filters applied to trial balance data
    """
    luna: int = Field(description="Luna (1-12)")
    an: int = Field(description="An")
    cont_filter: Optional[str] = Field(default=None, description="Filtru număr cont (partial match)")
    denumire_filter: Optional[str] = Field(default=None, description="Filtru denumire cont (partial match, case-insensitive)")


class TrialBalancePagination(BaseModel):
    """
    Pagination metadata
    """
    total_items: int = Field(description="Total number of items")
    total_pages: int = Field(description="Total number of pages")
    current_page: int = Field(description="Current page number")
    page_size: int = Field(description="Items per page")


class TrialBalanceTotals(BaseModel):
    """
    Totals for all 6 columns from all filtered records (not just current page)
    """
    total_sold_precedent_debit: Decimal = Decimal('0.00')
    total_sold_precedent_credit: Decimal = Decimal('0.00')
    total_rulaj_lunar_debit: Decimal = Decimal('0.00')
    total_rulaj_lunar_credit: Decimal = Decimal('0.00')
    total_sold_final_debit: Decimal = Decimal('0.00')
    total_sold_final_credit: Decimal = Decimal('0.00')


class TrialBalanceResponse(BaseModel):
    """
    Complete response for trial balance endpoint
    """
    success: bool = Field(default=True, description="Request success status")
    data: dict = Field(description="Trial balance data with items, pagination, and filters")

    class Config:
        json_schema_extra = {
            "example": {
                "success": True,
                "data": {
                    "items": [
                        {
                            "cont": "4111",
                            "dcont": "Furnizori interni",
                            "sold_precedent_debit": 0.00,
                            "sold_precedent_credit": 15000.00,
                            "rulaj_lunar_debit": 5000.00,
                            "rulaj_lunar_credit": 8000.00,
                            "sold_final_debit": 0.00,
                            "sold_final_credit": 18000.00
                        }
                    ],
                    "pagination": {
                        "total_items": 150,
                        "total_pages": 3,
                        "current_page": 1,
                        "page_size": 50
                    },
                    "filters_applied": {
                        "luna": 11,
                        "an": 2025,
                        "cont_filter": None,
                        "denumire_filter": "furnizori"
                    }
                }
            }
        }
