"""
用户模型 — SQLModel

角色体系（不再改）:
  - zhipianren = 制片人 / 管理员 = 全部权限
  - zebian     = 责编 = 维护仪表盘 / 知识库 / 排期
  - biandao    = 编导 = 使用 TPS / 查看知识库 / 生成报题单 / 个人热点雷达
"""

from enum import Enum
from datetime import datetime, timezone

from sqlalchemy import Column
from sqlalchemy import DateTime as SADateTime
from sqlalchemy.sql import func as sa_func
from sqlmodel import Field, SQLModel


class UserRole(str, Enum):
    """用户角色枚举，值与数据库 CHECK 约束一致。"""
    zhipianren = "zhipianren"   # 制片人 / 管理员
    zebian = "zebian"           # 责编
    biandao = "biandao"         # 编导


class User(SQLModel, table=True):
    __tablename__ = "users"

    id: int | None = Field(default=None, primary_key=True)
    username: str = Field(max_length=50, unique=True, index=True)
    display_name: str = Field(max_length=100)
    password_hash: str = Field(max_length=128)
    role: UserRole = Field(default=UserRole.biandao, max_length=20)
    profile_text: str | None = Field(default=None)
    is_active: bool = Field(default=True)

    created_at: datetime | None = Field(
        default=None,
        sa_column=Column(SADateTime(timezone=True), nullable=False, server_default=sa_func.now()),
    )
    updated_at: datetime | None = Field(
        default=None,
        sa_column=Column(SADateTime(timezone=True), nullable=False, server_default=sa_func.now()),
    )
