"""Posting DTOs."""
from __future__ import annotations

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

from pydantic import BaseModel, Field

from app.models.types import VoucherStatus
from app.schemas.common import ORMBase


class VoucherLineCreate(BaseModel):
    account_id: UUID
    debit: Decimal = Decimal("0")
    credit: Decimal = Decimal("0")
    description: str | None = None
    cost_center_id: UUID | None = None
    project_id: UUID | None = None


class VoucherCreate(BaseModel):
    fiscal_period_id: UUID
    voucher_number: str | None = Field(default=None, max_length=50)
    voucher_date: date
    description: str | None = None
    reference_number: str | None = None
    source_module: str | None = None
    lines: list[VoucherLineCreate] = Field(min_length=2)


class VoucherUpdate(BaseModel):
    voucher_date: date | None = None
    description: str | None = None
    reference_number: str | None = None
    lines: list[VoucherLineCreate] | None = Field(default=None, min_length=2)


class VoucherLineRead(ORMBase):
    id: UUID
    line_number: int
    account_id: UUID
    debit: Decimal
    credit: Decimal
    description: str | None
    cost_center_id: UUID | None = None
    project_id: UUID | None = None


class VoucherRead(ORMBase):
    id: UUID
    tenant_id: UUID
    fiscal_period_id: UUID
    voucher_number: str
    voucher_date: date
    status: VoucherStatus
    description: str | None
    total_debit: Decimal
    total_credit: Decimal
    posted_at: datetime | None
    lines: list[VoucherLineRead] = []


class PostRequest(BaseModel):
    source_module: str | None = None


class ReverseRequest(BaseModel):
    reversal_date: date | None = None


class AuditLogRead(ORMBase):
    id: UUID
    tenant_id: UUID
    operation: str
    actor_user_id: str
    voucher_number: str | None
    resource_type: str
    resource_id: str
    created_at: datetime
