from __future__ import annotations

from sqlalchemy import BigInteger, Date, DateTime, DECIMAL, ForeignKey, Integer, String, text
from sqlalchemy.orm import Mapped, mapped_column

from app.models.base import Base


class Unit(Base):
    __tablename__ = "md_unit"

    id: Mapped[int] = mapped_column(BigInteger, primary_key=True)
    unit_code: Mapped[str] = mapped_column(String(50))
    unit_name: Mapped[str] = mapped_column(String(50))
    precision_digits: Mapped[int] = mapped_column(Integer, server_default=text("3"))
    created_at: Mapped[object] = mapped_column(DateTime)
    updated_at: Mapped[object] = mapped_column(DateTime)


class Item(Base):
    __tablename__ = "md_item"

    id: Mapped[int] = mapped_column(BigInteger, primary_key=True)
    item_code: Mapped[str] = mapped_column(String(50))
    item_name: Mapped[str] = mapped_column(String(200))
    item_type: Mapped[str] = mapped_column(String(32))
    specification: Mapped[str | None] = mapped_column(String(200), nullable=True)
    material_grade: Mapped[str | None] = mapped_column(String(100), nullable=True)
    unit_weight_kg: Mapped[float] = mapped_column(DECIMAL(18, 6), server_default=text("0"))
    sales_unit_id: Mapped[int | None] = mapped_column(ForeignKey("md_unit.id"), nullable=True)
    stock_unit_id: Mapped[int | None] = mapped_column(ForeignKey("md_unit.id"), nullable=True)
    purchase_unit_id: Mapped[int | None] = mapped_column(ForeignKey("md_unit.id"), nullable=True)
    safety_stock_weight_kg: Mapped[float] = mapped_column(DECIMAL(18, 6), server_default=text("0"))
    scrap_sale_price: Mapped[float] = mapped_column(DECIMAL(18, 4), server_default=text("0"))
    status: Mapped[str] = mapped_column(String(32))
    remark: Mapped[str | None] = mapped_column(String(255), nullable=True)
    created_at: Mapped[object] = mapped_column(DateTime)
    updated_at: Mapped[object] = mapped_column(DateTime)


class Material(Base):
    __tablename__ = "md_material"

    id: Mapped[int] = mapped_column(BigInteger, primary_key=True)
    item_id: Mapped[int] = mapped_column(ForeignKey("md_item.id"))
    default_supplier_id: Mapped[int | None] = mapped_column(BigInteger, nullable=True)
    purchase_calc_mode: Mapped[str] = mapped_column(String(32))
    min_purchase_qty: Mapped[float] = mapped_column(DECIMAL(18, 6), server_default=text("0"))
    purchase_multiple_qty: Mapped[float] = mapped_column(DECIMAL(18, 6), server_default=text("0"))
    purchase_multiple_weight_kg: Mapped[float] = mapped_column(DECIMAL(18, 6), server_default=text("0"))
    lead_time_days: Mapped[int] = mapped_column(server_default=text("0"))
    quality_rule: Mapped[str | None] = mapped_column(String(255), nullable=True)
    created_at: Mapped[object] = mapped_column(DateTime)
    updated_at: Mapped[object] = mapped_column(DateTime)


class Product(Base):
    __tablename__ = "md_product"

    id: Mapped[int] = mapped_column(BigInteger, primary_key=True)
    item_id: Mapped[int] = mapped_column(ForeignKey("md_item.id"))
    drawing_no: Mapped[str | None] = mapped_column(String(100), nullable=True)
    product_family: Mapped[str | None] = mapped_column(String(100), nullable=True)
    version_no: Mapped[str] = mapped_column(String(50))
    standard_output_qty: Mapped[float] = mapped_column(DECIMAL(18, 6), server_default=text("0"))
    allowed_scrap_rate: Mapped[float] = mapped_column(DECIMAL(8, 4), server_default=text("0"))
    quality_standard: Mapped[str | None] = mapped_column(String(255), nullable=True)
    created_at: Mapped[object] = mapped_column(DateTime)
    updated_at: Mapped[object] = mapped_column(DateTime)


class Warehouse(Base):
    __tablename__ = "wh_warehouse"

    id: Mapped[int] = mapped_column(BigInteger, primary_key=True)
    warehouse_code: Mapped[str] = mapped_column(String(50))
    warehouse_name: Mapped[str] = mapped_column(String(100))
    warehouse_type: Mapped[str] = mapped_column(String(32))
    manager_employee_id: Mapped[int | None] = mapped_column(BigInteger, nullable=True)
    status: Mapped[str] = mapped_column(String(32))
    remark: Mapped[str | None] = mapped_column(String(255), nullable=True)
    created_at: Mapped[object] = mapped_column(DateTime)
    updated_at: Mapped[object] = mapped_column(DateTime)


class Bom(Base):
    __tablename__ = "md_bom"

    id: Mapped[int] = mapped_column(BigInteger, primary_key=True)
    bom_code: Mapped[str] = mapped_column(String(50))
    product_item_id: Mapped[int] = mapped_column(ForeignKey("md_item.id"))
    version_no: Mapped[str] = mapped_column(String(50))
    is_default: Mapped[int] = mapped_column(Integer)
    status: Mapped[str] = mapped_column(String(32))
    effective_date: Mapped[object | None] = mapped_column(Date, nullable=True)
    expire_date: Mapped[object | None] = mapped_column(Date, nullable=True)
    remark: Mapped[str | None] = mapped_column(String(255), nullable=True)
    created_at: Mapped[object] = mapped_column(DateTime)
    updated_at: Mapped[object] = mapped_column(DateTime)


class BomItem(Base):
    __tablename__ = "md_bom_item"

    id: Mapped[int] = mapped_column(BigInteger, primary_key=True)
    bom_id: Mapped[int] = mapped_column(ForeignKey("md_bom.id"))
    seq_no: Mapped[int] = mapped_column(Integer)
    material_item_id: Mapped[int] = mapped_column(ForeignKey("md_item.id"))
    usage_type: Mapped[str] = mapped_column(String(32))
    qty_per_piece: Mapped[float] = mapped_column(DECIMAL(18, 6), server_default=text("0"))
    net_weight_per_piece_kg: Mapped[float] = mapped_column(DECIMAL(18, 6), server_default=text("0"))
    gross_weight_per_piece_kg: Mapped[float] = mapped_column(DECIMAL(18, 6), server_default=text("0"))
    loss_rate: Mapped[float] = mapped_column(DECIMAL(8, 4), server_default=text("0"))
    issue_over_tolerance_rate: Mapped[float] = mapped_column(DECIMAL(8, 4), server_default=text("0"))
    remark: Mapped[str | None] = mapped_column(String(255), nullable=True)
    created_at: Mapped[object] = mapped_column(DateTime)
    updated_at: Mapped[object] = mapped_column(DateTime)


class StockBalance(Base):
    __tablename__ = "wh_stock_balance"

    id: Mapped[int] = mapped_column(BigInteger, primary_key=True)
    item_id: Mapped[int] = mapped_column(ForeignKey("md_item.id"))
    warehouse_id: Mapped[int] = mapped_column(ForeignKey("wh_warehouse.id"))
    location_id: Mapped[int | None] = mapped_column(BigInteger, nullable=True)
    qty_on_hand: Mapped[float] = mapped_column(DECIMAL(18, 6), server_default=text("0"))
    weight_on_hand_kg: Mapped[float] = mapped_column(DECIMAL(18, 6), server_default=text("0"))
    qty_available: Mapped[float] = mapped_column(DECIMAL(18, 6), server_default=text("0"))
    weight_available_kg: Mapped[float] = mapped_column(DECIMAL(18, 6), server_default=text("0"))
    qty_allocated: Mapped[float] = mapped_column(DECIMAL(18, 6), server_default=text("0"))
    weight_allocated_kg: Mapped[float] = mapped_column(DECIMAL(18, 6), server_default=text("0"))
    avg_unit_cost: Mapped[float] = mapped_column(DECIMAL(18, 4), server_default=text("0"))
    created_at: Mapped[object] = mapped_column(DateTime)
    updated_at: Mapped[object] = mapped_column(DateTime)
