from datetime import datetime

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

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


class UserOAuth(Base, TimestampMixin, SoftDeleteMixin):
    __tablename__ = "user_oauth"

    id: Mapped[str] = mapped_column(
        String(32), primary_key=True, comment="主键"
    )
    account_id: Mapped[str] = mapped_column(
        String(64), nullable=False, index=True, comment="授权账户id"
    )
    account_name: Mapped[str] = mapped_column(
        String(128), nullable=False, comment="授权账户name"
    )
    account_role: Mapped[str | None] = mapped_column(
        String(64), nullable=True, comment="授权账户角色"
    )
    account_username: Mapped[str | None] = mapped_column(
        String(128), nullable=True, comment="授权账户登录账号"
    )
    account_userid: Mapped[str | None] = mapped_column(
        String(128), nullable=True, comment="授权账户登录userid，同一个用户不同的授权账户token不一样"
    )
    user_id: Mapped[str] = mapped_column(
        String(32), nullable=False, index=True,
        comment="用户id"
    )
    open_type: Mapped[int] = mapped_column(
        Integer, nullable=False,
        comment="开户方式（1=千川，2=广告，3=本地推，4=星图，5=快手代理商，6=巨量星图，7=巨量服务单，8=腾讯服务单，9=腾讯营销K2，10=腾讯营销K3）"
    )
    port_type: Mapped[int] = mapped_column(
        Integer, nullable=False, 
        comment="平台端口（1=巨量，2=磁力，3=巨量星图，4=服务单，5=腾讯）"
    )
    appid: Mapped[str | None] = mapped_column(
        String(64), nullable=True, comment="授权应用id"
    )
    access_token: Mapped[str | None] = mapped_column(
        Text, nullable=True, comment="授权token"
    )
    access_token_expired: Mapped[datetime | None] = mapped_column(
        DateTime(timezone=True), nullable=True, 
        comment="token过期时间"
    )
    refresh_token: Mapped[str | None] = mapped_column(
        Text, nullable=True, comment="授权刷新token"
    )
    refresh_token_expired: Mapped[datetime | None] = mapped_column(
        DateTime(timezone=True), nullable=True,
        comment="刷新token过期时间"
    )
    material_auth_status: Mapped[bool] = mapped_column(
        Boolean, default=False, comment="是否敏感物料授权（true=是，false=否）"
    )