"""可视化页面模型 — GrapeJS 编辑产出存储"""
from sqlalchemy import BigInteger, String, Text, Integer, Index
from sqlalchemy.dialects.mysql import TINYINT, MEDIUMTEXT
from sqlalchemy.orm import Mapped, mapped_column
from app.core.models.base import Base, TimestampMixin, TenantMixin


class Page(Base, TenantMixin, TimestampMixin):
    """由 GrapeJS 编辑器生成的自定义页面"""

    __tablename__ = "pages"
    __table_args__ = (
        Index("ix_pages_tenant_slug", "tenant_id", "slug", unique=True),
        Index("ix_pages_status", "status"),
        {
            "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="页面标题")
    slug:         Mapped[str]      = mapped_column(String(200), nullable=False, comment="URL slug，如 home / about")
    description:  Mapped[str|None] = mapped_column(String(500), nullable=True, comment="页面描述（SEO）")
    # GrapeJS 产出
    content_html: Mapped[str|None] = mapped_column(MEDIUMTEXT, nullable=True, comment="GrapeJS 输出 HTML")
    content_css:  Mapped[str|None] = mapped_column(MEDIUMTEXT, nullable=True, comment="GrapeJS 输出 CSS")
    content_json: Mapped[str|None] = mapped_column(MEDIUMTEXT, nullable=True, comment="GrapeJS 组件 JSON（用于再次编辑）")
    status:       Mapped[str]      = mapped_column(String(20), nullable=False, default="draft", comment="draft/published")
    sort_order:   Mapped[int]      = mapped_column(Integer, nullable=False, default=0)

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