"""店铺设置模型"""
from decimal import Decimal

from sqlalchemy import BigInteger, ForeignKey, String, Text, DECIMAL, JSON, UniqueConstraint, Integer
from sqlalchemy.orm import Mapped, mapped_column, relationship

from app.core.models.base import Base, TimestampMixin


class TenantSettings(Base, TimestampMixin):
    """店铺设置 — 一个租户只有一条记录"""

    __tablename__ = "tenant_settings"
    __table_args__ = (
        UniqueConstraint("tenant_id", name="uk_tenant_settings_tenant"),
        {
            "mysql_engine": "InnoDB",
            "mysql_charset": "utf8mb4",
            "mysql_collate": "utf8mb4_unicode_ci",
        },
    )

    id: Mapped[int] = mapped_column(BigInteger, primary_key=True, autoincrement=True)
    tenant_id: Mapped[int] = mapped_column(
        BigInteger,
        ForeignKey("tenants.id", ondelete="CASCADE"),
        nullable=False,
        unique=True,
        comment="租户 ID",
    )

    # 商店信息
    store_name: Mapped[str] = mapped_column(String(200), nullable=False, default="My Store")
    store_phone: Mapped[str | None] = mapped_column(String(50), nullable=True)
    store_address: Mapped[str | None] = mapped_column(String(500), nullable=True)
    store_description: Mapped[str | None] = mapped_column(Text, nullable=True)

    # 物流
    default_shipping_fee: Mapped[Decimal] = mapped_column(DECIMAL(10, 2), nullable=False, default=Decimal("10.00"))
    free_shipping_threshold: Mapped[Decimal] = mapped_column(DECIMAL(10, 2), nullable=False, default=Decimal("99.00"))

    # 通知
    notify_email: Mapped[str | None] = mapped_column(String(255), nullable=True)

    # SMTP 邮件配置
    smtp_host: Mapped[str | None] = mapped_column(String(255), nullable=True, default="")
    smtp_port: Mapped[int] = mapped_column(Integer, nullable=False, default=587)
    smtp_user: Mapped[str | None] = mapped_column(String(255), nullable=True, default="")
    smtp_password: Mapped[str | None] = mapped_column(String(255), nullable=True, default="")
    smtp_from_name: Mapped[str | None] = mapped_column(String(100), nullable=True, default="SME Store")
    smtp_from_email: Mapped[str | None] = mapped_column(String(255), nullable=True, default="")
    smtp_use_tls: Mapped[int] = mapped_column(Integer, nullable=False, default=1)  # 0=off, 1=on
    smtp_enabled: Mapped[int] = mapped_column(Integer, nullable=False, default=0)  # 0=off, 1=on

    # 支付开关（JSON，方便扩展）
    payment_methods: Mapped[dict | None] = mapped_column(JSON, nullable=True)

    # 库存
    allow_oversell_global: Mapped[int] = mapped_column(Integer, nullable=False, default=0, comment="全局允许超卖 0=否 1=是")

    # 时区（IANA tz database 名，例如 "Pacific/Auckland"）。默认 UTC。
    timezone: Mapped[str] = mapped_column(String(64), nullable=False, default="UTC", comment="IANA 时区，默认 UTC")

    # 预留扩展
    extra: Mapped[dict | None] = mapped_column(JSON, nullable=True)

    # ── 关联 ─────────────────────────────────────────────────────
    # tenant = relationship("Tenant", back_populates="settings", lazy="noload")  # 注:需Tenant侧也有backref

    def __repr__(self) -> str:
        return f"<TenantSettings tenant_id={self.tenant_id} store={self.store_name!r}>"
