from __future__ import annotations

from datetime import datetime

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

from app.db.session import Base


class ProductCostItem(Base):
    __tablename__ = "product_cost_items"
    __table_args__ = (UniqueConstraint("tenant_id", "item_id", name="uq_product_cost_item_tenant_item"),)

    id: Mapped[int] = mapped_column(primary_key=True)
    tenant_id: Mapped[str] = mapped_column(String(64), default="default", index=True)
    client_category: Mapped[str] = mapped_column(String(255), index=True)
    item_id: Mapped[str | None] = mapped_column(String(128), nullable=True, index=True)
    product_name: Mapped[str] = mapped_column(String(255), index=True)
    mix_product_name: Mapped[str] = mapped_column(String(255), index=True)
    unit_type: Mapped[str] = mapped_column(String(32), default="Standard")
    own_bag: Mapped[str | None] = mapped_column(String(32), nullable=True)
    unit_kg: Mapped[float | None] = mapped_column(Float, nullable=True)
    items_per_pallet: Mapped[int | None] = mapped_column(Integer, nullable=True)
    bagging_process: Mapped[str | None] = mapped_column(String(128), nullable=True)
    manual_distributor_margin: Mapped[float | None] = mapped_column(Float, nullable=True)
    manual_wholesale_margin: Mapped[float | None] = mapped_column(Float, nullable=True)

    cleaned_product_cost_per_kg: Mapped[float | None] = mapped_column(Float, nullable=True)
    grading_cost_per_kg: Mapped[float | None] = mapped_column(Float, nullable=True)
    bagging_cost_per_kg: Mapped[float | None] = mapped_column(Float, nullable=True)
    cracking_cost_per_kg: Mapped[float | None] = mapped_column(Float, nullable=True)
    bag_cost_per_unit: Mapped[float | None] = mapped_column(Float, nullable=True)
    freight_cost_per_unit: Mapped[float | None] = mapped_column(Float, nullable=True)
    finished_product_delivered_cost: Mapped[float | None] = mapped_column(Float, nullable=True)
    distributor_price: Mapped[float | None] = mapped_column(Float, nullable=True)
    wholesale_price: Mapped[float | None] = mapped_column(Float, nullable=True)
    warnings: 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)


class ProductCostBaseInput(Base):
    __tablename__ = "product_cost_base_inputs"

    id: Mapped[int] = mapped_column(primary_key=True)
    tenant_id: Mapped[str] = mapped_column(String(64), default="default", unique=True, index=True)
    grading_per_tonne: Mapped[float] = mapped_column(Float, default=0.0)
    grading_per_kg: Mapped[float] = mapped_column(Float, default=0.0)
    cracking_per_tonne: Mapped[float] = mapped_column(Float, default=0.0)
    cracking_per_kg: Mapped[float] = mapped_column(Float, default=0.0)
    updated_at: Mapped[datetime] = mapped_column(DateTime, default=datetime.utcnow, onupdate=datetime.utcnow)


class ProductCostProcessInput(Base):
    __tablename__ = "product_cost_process_inputs"
    __table_args__ = (UniqueConstraint("tenant_id", "process_name", name="uq_product_cost_process_tenant_name"),)

    id: Mapped[int] = mapped_column(primary_key=True)
    tenant_id: Mapped[str] = mapped_column(String(64), default="default", index=True)
    process_name: Mapped[str] = mapped_column(String(128), index=True)
    cost_per_kg: Mapped[float] = mapped_column(Float, default=0.0)
    updated_at: Mapped[datetime] = mapped_column(DateTime, default=datetime.utcnow, onupdate=datetime.utcnow)


class ProductCostClientInput(Base):
    __tablename__ = "product_cost_client_inputs"
    __table_args__ = (UniqueConstraint("tenant_id", "client_category", name="uq_product_cost_client_tenant_name"),)

    id: Mapped[int] = mapped_column(primary_key=True)
    tenant_id: Mapped[str] = mapped_column(String(64), default="default", index=True)
    client_category: Mapped[str] = mapped_column(String(255), index=True)
    distributor_margin: Mapped[float | None] = mapped_column(Float, nullable=True)
    wholesale_margin: Mapped[float | None] = mapped_column(Float, nullable=True)
    updated_at: Mapped[datetime] = mapped_column(DateTime, default=datetime.utcnow, onupdate=datetime.utcnow)


class ProductCostBagInput(Base):
    __tablename__ = "product_cost_bag_inputs"
    __table_args__ = (UniqueConstraint("tenant_id", "input_key", name="uq_product_cost_bag_tenant_key"),)

    id: Mapped[int] = mapped_column(primary_key=True)
    tenant_id: Mapped[str] = mapped_column(String(64), default="default", index=True)
    input_key: Mapped[str] = mapped_column(String(64), index=True)
    label: Mapped[str] = mapped_column(String(128))
    cost: Mapped[float] = mapped_column(Float, default=0.0)
    updated_at: Mapped[datetime] = mapped_column(DateTime, default=datetime.utcnow, onupdate=datetime.utcnow)


class ProductCostFreightInput(Base):
    __tablename__ = "product_cost_freight_inputs"
    __table_args__ = (UniqueConstraint("tenant_id", "input_key", name="uq_product_cost_freight_tenant_key"),)

    id: Mapped[int] = mapped_column(primary_key=True)
    tenant_id: Mapped[str] = mapped_column(String(64), default="default", index=True)
    input_key: Mapped[str] = mapped_column(String(64), index=True)
    label: Mapped[str] = mapped_column(String(128))
    cost: Mapped[float] = mapped_column(Float, default=0.0)
    updated_at: Mapped[datetime] = mapped_column(DateTime, default=datetime.utcnow, onupdate=datetime.utcnow)
