"""
CheckoutLineItem ORM model.

Pre-configured line items attached to a Checkout. When a payer submits
the checkout form, these items are copied onto the resulting PaymentRequest
line items. product_id is nullable — merchants may configure free-text items
not tied to a product catalog entry.
"""

from typing import Optional, TYPE_CHECKING

from sqlalchemy import Float, ForeignKey, Integer, Text
from sqlalchemy.orm import Mapped, mapped_column, relationship

from src.apps.base.models.base import Base

if TYPE_CHECKING:
    from typing import Any
    Checkout = Any
    Product = Any


class CheckoutLineItem(Base):
    """
    A line item pre-configured on a Checkout.

    product_id       — optional FK to products.id; NULL for free-text items.
    description      — display label shown to the payer.
    quantity         — default quantity (payer may override if merchant allows).
    unit_price       — price per unit in the checkout's currency.
    discount_amount  — flat discount applied to this line item.
    """

    __tablename__ = "checkout_line_items"

    id: Mapped[int] = mapped_column(Integer, primary_key=True, index=True, autoincrement=True)

    checkout_id: Mapped[int] = mapped_column(
        Integer, ForeignKey("checkouts.id"), nullable=False, index=True
    )

    product_id: Mapped[Optional[int]] = mapped_column(
        Integer, ForeignKey("products.id"), nullable=True, index=True
    )

    description: Mapped[Optional[str]] = mapped_column(Text, nullable=True)
    quantity: Mapped[int] = mapped_column(Integer, nullable=False, default=1)
    unit_price: Mapped[float] = mapped_column(Float, nullable=False, default=0.0)
    discount_amount: Mapped[Optional[float]] = mapped_column(Float, nullable=True, default=0.0)

    # Relationships
    checkout: Mapped["Checkout"] = relationship("Checkout", back_populates="line_items")
    product: Mapped[Optional["Product"]] = relationship("Product")
