"""导航菜单模型 — 管理 Header / Footer 的菜单结构"""
from sqlalchemy import BigInteger, ForeignKey, String, Boolean, JSON
from sqlalchemy.orm import Mapped, mapped_column

from app.core.models.base import Base, TimestampMixin


class NavigationMenu(Base, TimestampMixin):
    """一个租户可有多个命名菜单，每个菜单绑定一个位置（header/footer）"""

    __tablename__ = "navigation_menus"
    __table_args__ = {
        "mysql_engine": "InnoDB",
        "mysql_charset": "utf8mb4",
        "mysql_collate": "utf8mb4_unicode_ci",
        "comment": "导航菜单",
    }

    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,
        index=True,
    )
    name: Mapped[str] = mapped_column(String(200), nullable=False, comment="菜单名称")
    location: Mapped[str] = mapped_column(
        String(50), nullable=False, index=True,
        comment="菜单位置：header | footer | header_secondary | …",
    )
    is_active: Mapped[bool] = mapped_column(Boolean, nullable=False, default=True)
    # JSON 数组，每个元素结构：
    # { id, label, url, type, target, icon?, bg_color?, children[] }
    items: Mapped[list | None] = mapped_column(JSON, nullable=True, default=list)
    # 菜单级别的元数据（Footer 品牌信息等）
    # { logo, brand_name, description, copyright }
    meta: Mapped[dict | None] = mapped_column(JSON, nullable=True, default=dict)

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