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 Customer(Base):
    __tablename__ = "md_customer"

    id: Mapped[int] = mapped_column(BigInteger, primary_key=True)
    customer_code: Mapped[str] = mapped_column(String(50))
    customer_name: Mapped[str] = mapped_column(String(200))
    short_name: Mapped[str | None] = mapped_column(String(100), nullable=True)
    contact_name: Mapped[str | None] = mapped_column(String(100), nullable=True)
    contact_phone: Mapped[str | None] = mapped_column(String(50), nullable=True)
    address: Mapped[str | None] = mapped_column(String(255), nullable=True)
    tax_no: Mapped[str | None] = mapped_column(String(100), nullable=True)
    credit_days: Mapped[int] = mapped_column(server_default=text("0"))
    status: Mapped[str] = mapped_column(String(32))
    created_at: Mapped[object] = mapped_column(DateTime)
    updated_at: Mapped[object] = mapped_column(DateTime)


class SalesOrder(Base):
    __tablename__ = "so_sales_order"

    id: Mapped[int] = mapped_column(BigInteger, primary_key=True)
    order_no: Mapped[str] = mapped_column(String(50))
    customer_id: Mapped[int] = mapped_column(ForeignKey("md_customer.id"))
    order_date: Mapped[object] = mapped_column(Date)
    promised_date: Mapped[object | None] = mapped_column(Date, nullable=True)
    sales_employee_id: Mapped[int | None] = mapped_column(BigInteger, nullable=True)
    delivery_address: Mapped[str | None] = mapped_column(String(255), nullable=True)
    tax_rate: Mapped[float] = mapped_column(DECIMAL(8, 4), server_default=text("0"))
    total_amount: Mapped[float] = mapped_column(DECIMAL(18, 2), server_default=text("0"))
    status: Mapped[str] = mapped_column(String(32))
    created_at: Mapped[object] = mapped_column(DateTime)
    updated_at: Mapped[object] = mapped_column(DateTime)


class SalesOrderItem(Base):
    __tablename__ = "so_sales_order_item"

    id: Mapped[int] = mapped_column(BigInteger, primary_key=True)
    sales_order_id: Mapped[int] = mapped_column(ForeignKey("so_sales_order.id"))
    line_no: Mapped[int] = mapped_column(Integer)
    product_item_id: Mapped[int] = mapped_column(ForeignKey("md_item.id"))
    customer_part_no: Mapped[str | None] = mapped_column(String(100), nullable=True)
    order_qty: Mapped[float] = mapped_column(DECIMAL(18, 6), server_default=text("0"))
    delivered_qty: Mapped[float] = mapped_column(DECIMAL(18, 6), server_default=text("0"))
    unit_price: Mapped[float] = mapped_column(DECIMAL(18, 4), server_default=text("0"))
    line_amount: Mapped[float] = mapped_column(DECIMAL(18, 2), server_default=text("0"))
    promised_date: Mapped[object | None] = mapped_column(Date, nullable=True)
    status: Mapped[str] = mapped_column(String(32))
    created_at: Mapped[object] = mapped_column(DateTime)
    updated_at: Mapped[object] = mapped_column(DateTime)
