from __future__ import annotations

from datetime import date, datetime
from decimal import Decimal
from typing import Any
from uuid import UUID

from pydantic import BaseModel, ConfigDict, Field


class DebtBalanceItemSchema(BaseModel):
    model_config = ConfigDict(from_attributes=True)

    id: UUID | None = None
    company_id: UUID
    partner_id: UUID | None = None
    partner_code: str
    partner_name: str
    account_code: str
    debt_type: str
    fiscal_year: int = 2026
    period_type: str = "quarter"
    period_name: str = "Q1.2026"
    as_of_date: date = Field(default_factory=lambda: date(2026, 3, 31))
    opening_debit_vnd: Decimal = Field(default_factory=lambda: Decimal("0.0000"))
    opening_credit_vnd: Decimal = Field(default_factory=lambda: Decimal("0.0000"))
    period_debit_vnd: Decimal = Field(default_factory=lambda: Decimal("0.0000"))
    period_credit_vnd: Decimal = Field(default_factory=lambda: Decimal("0.0000"))
    closing_debit_vnd: Decimal = Field(default_factory=lambda: Decimal("0.0000"))
    closing_credit_vnd: Decimal = Field(default_factory=lambda: Decimal("0.0000"))
    net_closing_balance_vnd: Decimal = Field(default_factory=lambda: Decimal("0.0000"))
    source_file: str
    raw_metadata: dict[str, Any] = Field(default_factory=dict)
    created_at: datetime | None = None
    updated_at: datetime | None = None


class DebtReportSummary(BaseModel):
    total_partners: int
    total_opening_debit_vnd: Decimal
    total_opening_credit_vnd: Decimal
    total_period_debit_vnd: Decimal
    total_period_credit_vnd: Decimal
    total_closing_debit_vnd: Decimal
    total_closing_credit_vnd: Decimal
    total_net_closing_vnd: Decimal


class DebtLedgerResponse(BaseModel):
    account_code: str
    debt_type: str
    period_name: str
    as_of_date: date
    summary: DebtReportSummary
    items: list[DebtBalanceItemSchema]


class FixedAssetItemSchema(BaseModel):
    model_config = ConfigDict(from_attributes=True)

    id: UUID | None = None
    company_id: UUID
    asset_code: str
    asset_name: str
    asset_type: str
    department: str
    increase_date: date
    voucher_number: str | None = None
    depreciation_start_date: date
    useful_life_months: int
    remaining_life_months: int
    original_cost_vnd: Decimal = Field(default_factory=lambda: Decimal("0.0000"))
    depreciable_value_vnd: Decimal = Field(default_factory=lambda: Decimal("0.0000"))
    period_depreciation_vnd: Decimal = Field(default_factory=lambda: Decimal("0.0000"))
    accumulated_depreciation_vnd: Decimal = Field(default_factory=lambda: Decimal("0.0000"))
    net_book_value_vnd: Decimal = Field(default_factory=lambda: Decimal("0.0000"))
    monthly_depreciation_vnd: Decimal = Field(default_factory=lambda: Decimal("0.0000"))
    cost_account: str = "211"
    depreciation_account: str = "2141"
    fiscal_year: int = 2026
    period_name: str = "Q1.2026"
    as_of_date: date = Field(default_factory=lambda: date(2026, 3, 31))
    matched_equipment_id: UUID | None = None
    source_file: str
    raw_metadata: dict[str, Any] = Field(default_factory=dict)
    created_at: datetime | None = None
    updated_at: datetime | None = None


class FixedAssetSummary(BaseModel):
    total_assets: int
    total_original_cost_vnd: Decimal
    total_depreciable_value_vnd: Decimal
    total_period_depreciation_vnd: Decimal
    total_accumulated_depreciation_vnd: Decimal
    total_net_book_value_vnd: Decimal
    total_monthly_depreciation_vnd: Decimal


class FixedAssetResponse(BaseModel):
    period_name: str
    as_of_date: date
    summary: FixedAssetSummary
    items: list[FixedAssetItemSchema]


class ConsolidatedDebtOverview(BaseModel):
    as_of_date: date
    period_name: str
    receivables_summary: DebtReportSummary
    payables_summary: DebtReportSummary
    fixed_assets_summary: FixedAssetSummary
    net_working_capital_receivable_vnd: Decimal
    net_working_capital_payable_vnd: Decimal
    net_trade_debt_balance_vnd: Decimal
