from datetime import datetime
from typing import Optional

from sqlalchemy import BigInteger, String, Integer, Index
from sqlalchemy.dialects.mysql import DATETIME as MYSQL_DATETIME, TINYINT
from sqlalchemy.orm import Mapped, mapped_column
from app.core.models.base import Base, TimestampMixin, TenantMixin


class StoreBanner(Base, TenantMixin, TimestampMixin):
    """商城首页 Banner（后台可配置）"""

    __tablename__ = "store_banners"
    __table_args__ = (
        Index("ix_banners_tenant", "tenant_id"),
        Index("ix_banners_active", "is_active"),
        Index("ix_banners_sort", "sort_order"),
        {
            "mysql_engine": "InnoDB",
            "mysql_charset": "utf8mb4",
            "mysql_collate": "utf8mb4_unicode_ci",
        },
    )

    id:             Mapped[int]           = mapped_column(BigInteger, primary_key=True, autoincrement=True)
    title:          Mapped[str]           = mapped_column(String(200), nullable=False, comment="Banner 标题")
    title_en:       Mapped[Optional[str]] = mapped_column(String(200), nullable=True, comment="Banner title (EN)")
    subtitle:       Mapped[Optional[str]] = mapped_column(String(500), nullable=True, comment="副标题")
    subtitle_en:    Mapped[Optional[str]] = mapped_column(String(500), nullable=True, comment="Subtitle (EN)")
    button_text:    Mapped[Optional[str]] = mapped_column(String(100), nullable=True, comment="按钮文字")
    button_text_en: Mapped[Optional[str]] = mapped_column(String(100), nullable=True, comment="Button text (EN)")
    image_url:      Mapped[str]           = mapped_column(String(500), nullable=False, comment="图片 URL")
    link_url:       Mapped[Optional[str]] = mapped_column(String(500), nullable=True, comment="点击跳转链接")
    sort_order:     Mapped[int]           = mapped_column(Integer, nullable=False, default=0)
    is_active:      Mapped[int]           = mapped_column(TINYINT(1), nullable=False, default=1)
    start_at:       Mapped[Optional[datetime]] = mapped_column(MYSQL_DATETIME(fsp=3), nullable=True)
    end_at:         Mapped[Optional[datetime]] = mapped_column(MYSQL_DATETIME(fsp=3), nullable=True)

    def __repr__(self) -> str:
        return f"<StoreBanner id={self.id} title={self.title!r}>"
