"""Billing schemas — Invoice + InvoiceLine Pydantic models."""
from __future__ import annotations
import uuid
from datetime import date, datetime
from decimal import Decimal
from pydantic import BaseModel, computed_field


class InvoiceLineCreate(BaseModel):
    order_line_id: uuid.UUID | None = None
    description: str
    quantity: int = 1
    unit_price: Decimal | None = None


class InvoiceLineOut(BaseModel):
    id: uuid.UUID
    invoice_id: uuid.UUID
    order_line_id: uuid.UUID | None
    description: str
    quantity: int
    unit_price: Decimal | None
    total: Decimal | None

    model_config = {"from_attributes": True}


class InvoiceCreate(BaseModel):
    order_line_ids: list[uuid.UUID] = []
    notes: str | None = None
    issued_at: date | None = None
    due_at: date | None = None
    vat_rate: Decimal = Decimal("0.19")
    currency: str = "EUR"


class InvoiceStatusUpdate(BaseModel):
    status: str  # draft|sent|paid|cancelled


class InvoiceOut(BaseModel):
    id: uuid.UUID
    tenant_id: uuid.UUID | None
    invoice_number: str
    status: str
    issued_at: date | None
    due_at: date | None
    total_net: Decimal | None
    total_vat: Decimal | None
    vat_rate: Decimal
    currency: str
    notes: str | None
    pdf_key: str | None
    created_at: datetime
    lines: list[InvoiceLineOut] = []

    @computed_field  # type: ignore[misc]
    @property
    def pdf_url(self) -> str | None:
        if self.pdf_key:
            return f"/api/billing/invoices/{self.id}/pdf"
        return None

    model_config = {"from_attributes": True}
