"""Tangki Produk History — tracks which product was in a tank over time."""

from __future__ import annotations

from datetime import date
from typing import TYPE_CHECKING

from sqlalchemy import Date, ForeignKey, Index
from sqlalchemy.orm import Mapped, mapped_column, relationship

from app.models.base import Base, TimestampMixin

if TYPE_CHECKING:
    from app.models.product import Produk
    from app.models.spbu import Tangki


class TangkiProdukHistory(Base, TimestampMixin):
    """One period of a product assignment to a tank."""

    __tablename__ = "tangki_produk_history"

    __table_args__ = (
        Index("ix_tangki_produk_history_tangki_id", "tangki_id"),
    )

    id: Mapped[int] = mapped_column(primary_key=True)
    tangki_id: Mapped[int] = mapped_column(
        ForeignKey("master_spbu_tangki.id", ondelete="CASCADE"), nullable=False
    )
    produk_id: Mapped[int] = mapped_column(
        ForeignKey("master_produk.id", ondelete="CASCADE"), nullable=False
    )
    berlaku_mulai: Mapped[date] = mapped_column(Date, nullable=False)
    berlaku_sampai: Mapped[date | None] = mapped_column(Date, nullable=True)

    # Relationships
    tangki: Mapped["Tangki"] = relationship(foreign_keys=[tangki_id])
    produk: Mapped["Produk"] = relationship(foreign_keys=[produk_id])
