from sqlalchemy import Boolean, ForeignKey, Integer, String, Text
from sqlalchemy.orm import Mapped, mapped_column
from sqlalchemy.types import JSON

from app.models.base import BaseModel


class ProductGroup(BaseModel):
    """分类下的产品组。一个组在前台呈现为一个 SKU 选择卡片集合。

    例如「云服务器」分类下的「香港 CN2」「美国 CUVIP」「日本软银」等线路组。
    parent_id 非空时为子组，前台先选顶级组再选子组。
    """

    __tablename__ = "product_groups"

    category_id: Mapped[int] = mapped_column(
        ForeignKey("product_categories.id", ondelete="CASCADE"), nullable=False, index=True
    )
    parent_id: Mapped[int | None] = mapped_column(
        ForeignKey("product_groups.id", ondelete="CASCADE"), nullable=True, index=True, comment="顶级组为 NULL，子组指向父组"
    )
    slug: Mapped[str] = mapped_column(String(64), nullable=False)
    name: Mapped[str] = mapped_column(String(100), nullable=False)
    headline: Mapped[str | None] = mapped_column(String(200), nullable=True, comment="一句话卖点")
    description: Mapped[str | None] = mapped_column(Text, nullable=True)
    icon: Mapped[str | None] = mapped_column(String(64), nullable=True)
    network_type: Mapped[str | None] = mapped_column(
        String(32), nullable=True, comment="网络线路类型：cn2_gia / bgp / 香港优化 等，前端筛选用"
    )
    sort_order: Mapped[int] = mapped_column(Integer, default=0, nullable=False)
    requires_real_name: Mapped[bool] = mapped_column(Boolean, default=False, nullable=False)
    area: Mapped[int | None] = mapped_column(Integer, nullable=True, comment="魔方云区域ID")
    node: Mapped[int | None] = mapped_column(Integer, nullable=True, comment="魔方云节点ID")
    os_options: Mapped[list | None] = mapped_column(JSON, nullable=True, comment="OS选项列表 [{label, value}]")
