from __future__ import annotations

from datetime import date, datetime

from sqlalchemy import Date, DateTime, Float, ForeignKey, Integer, String, Text, UniqueConstraint
from sqlalchemy.orm import Mapped, mapped_column, relationship

from app.db.session import Base


class MixCalculatorSession(Base):
    __tablename__ = "mix_calculator_sessions"
    __table_args__ = (UniqueConstraint("tenant_id", "session_number", name="uq_mix_calculator_session_number"),)

    id: Mapped[int] = mapped_column(primary_key=True)
    tenant_id: Mapped[str] = mapped_column(String(64), default="default", index=True)
    session_number: Mapped[str] = mapped_column(String(32), index=True)
    client_name: Mapped[str] = mapped_column(String(255), index=True)
    product_id: Mapped[int] = mapped_column(ForeignKey("products.id"), index=True)
    product_name: Mapped[str] = mapped_column(String(255))
    mix_id: Mapped[int] = mapped_column(ForeignKey("mixes.id"), index=True)
    mix_name: Mapped[str] = mapped_column(String(255))
    mix_date: Mapped[date] = mapped_column(Date, index=True)
    batch_size_kg: Mapped[float] = mapped_column(Float)
    total_bags: Mapped[float] = mapped_column(Float)
    total_kg: Mapped[float] = mapped_column(Float)
    product_unit_of_measure: Mapped[str] = mapped_column(String(64))
    product_unit_size_kg: Mapped[float] = mapped_column(Float)
    prepared_by_user_id: Mapped[int | None] = mapped_column(ForeignKey("client_users.id"), nullable=True, index=True)
    prepared_by_name: Mapped[str] = mapped_column(String(255))
    created_by: Mapped[str] = mapped_column(String(255))
    status: Mapped[str] = mapped_column(String(32), default="saved")
    notes: Mapped[str | None] = mapped_column(Text, nullable=True)
    created_at: Mapped[datetime] = mapped_column(DateTime, default=datetime.utcnow)
    updated_at: Mapped[datetime] = mapped_column(DateTime, default=datetime.utcnow, onupdate=datetime.utcnow)

    lines: Mapped[list["MixCalculatorSessionLine"]] = relationship(
        back_populates="session",
        cascade="all, delete-orphan",
        order_by="MixCalculatorSessionLine.sort_order",
    )


class MixCalculatorSessionLine(Base):
    __tablename__ = "mix_calculator_session_lines"

    id: Mapped[int] = mapped_column(primary_key=True)
    tenant_id: Mapped[str] = mapped_column(String(64), default="default", index=True)
    session_id: Mapped[int] = mapped_column(ForeignKey("mix_calculator_sessions.id"), index=True)
    raw_material_id: Mapped[int | None] = mapped_column(nullable=True)
    raw_material_name: Mapped[str] = mapped_column(String(255))
    required_kg: Mapped[float] = mapped_column(Float)
    mix_percentage: Mapped[float] = mapped_column(Float)
    unit: Mapped[str] = mapped_column(String(64))
    # Snapshot of the ingredient's rounding setting at save time.
    rounding_decimals: Mapped[int] = mapped_column(Integer, default=2)
    sort_order: Mapped[int] = mapped_column(Integer, default=0)

    session: Mapped[MixCalculatorSession] = relationship(back_populates="lines")
