# backend/app/plugins/tax/models.py
from decimal import Decimal
from sqlalchemy import BigInteger, String, Integer, DECIMAL, ForeignKey, UniqueConstraint, Index
from sqlalchemy.dialects.mysql import TINYINT
from sqlalchemy.orm import Mapped, mapped_column, relationship
from app.core.models.base import Base, TimestampMixin, TenantMixin


class TaxClass(Base, TenantMixin, TimestampMixin):
    __tablename__ = "tax_classes"
    __table_args__ = (
        UniqueConstraint("tenant_id", "name", name="uk_tax_classes_tenant_name"),
        {
            "mysql_engine": "InnoDB",
            "mysql_charset": "utf8mb4",
            "mysql_collate": "utf8mb4_unicode_ci",
        },
    )

    id: Mapped[int] = mapped_column(BigInteger, primary_key=True, autoincrement=True)
    name: Mapped[str] = mapped_column(String(100), nullable=False, comment="税种名称")
    description: Mapped[str | None] = mapped_column(String(255), nullable=True, comment="描述")
    is_default: Mapped[int] = mapped_column(TINYINT(1), nullable=False, default=0, comment="是否默认税种")
    sort_order: Mapped[int] = mapped_column(Integer, nullable=False, default=0, comment="排序")

    rates = relationship("TaxRate", back_populates="tax_class", lazy="noload", cascade="all, delete-orphan")


class TaxRate(Base, TenantMixin, TimestampMixin):
    __tablename__ = "tax_rates"
    __table_args__ = (
        Index("ix_tax_rates_lookup", "tenant_id", "tax_class_id", "country", "province"),
        {
            "mysql_engine": "InnoDB",
            "mysql_charset": "utf8mb4",
            "mysql_collate": "utf8mb4_unicode_ci",
        },
    )

    id: Mapped[int] = mapped_column(BigInteger, primary_key=True, autoincrement=True)
    tax_class_id: Mapped[int] = mapped_column(
        BigInteger, ForeignKey("tax_classes.id", ondelete="CASCADE"), nullable=False
    )
    name: Mapped[str] = mapped_column(String(100), nullable=False, comment="税率名称，如 NZ GST")
    country: Mapped[str] = mapped_column(String(2), nullable=False, comment="ISO 3166-1 alpha-2")
    province: Mapped[str | None] = mapped_column(String(100), nullable=True, comment="省/州，NULL=全国")
    rate: Mapped[Decimal] = mapped_column(DECIMAL(6, 4), nullable=False, comment="税率，如 0.1500")
    priority: Mapped[int] = mapped_column(Integer, nullable=False, default=1, comment="优先级")
    compound: Mapped[int] = mapped_column(TINYINT(1), nullable=False, default=0, comment="是否复合税")

    tax_class = relationship("TaxClass", back_populates="rates", lazy="noload")


class TaxSettings(Base, TenantMixin, TimestampMixin):
    __tablename__ = "tax_settings"
    __table_args__ = (
        UniqueConstraint("tenant_id", name="uk_tax_settings_tenant"),
        {
            "mysql_engine": "InnoDB",
            "mysql_charset": "utf8mb4",
            "mysql_collate": "utf8mb4_unicode_ci",
        },
    )

    id: Mapped[int] = mapped_column(BigInteger, primary_key=True, autoincrement=True)
    prices_include_tax: Mapped[int] = mapped_column(TINYINT(1), nullable=False, default=0, comment="标价含税")
    tax_shipping: Mapped[int] = mapped_column(TINYINT(1), nullable=False, default=0, comment="运费征税")
    shipping_tax_class_id: Mapped[int | None] = mapped_column(
        BigInteger, ForeignKey("tax_classes.id", ondelete="SET NULL"), nullable=True
    )
    display_prices_in_shop: Mapped[str] = mapped_column(String(10), nullable=False, default="excl")
    display_prices_in_cart: Mapped[str] = mapped_column(String(10), nullable=False, default="excl")
    tax_registration_no: Mapped[str | None] = mapped_column(String(100), nullable=True, comment="税号")
    rounding_mode: Mapped[str] = mapped_column(String(10), nullable=False, default="line", comment="line/total")
