# 宇之然内容创作平台 - 数据模型

from sqlalchemy import Column, Integer, String, DateTime, JSON, Boolean, func, Float
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.dialects.postgresql import UUID
import uuid

Base = declarative_base()

class User(Base):
    __tablename__ = "users"

    id = Column(Integer, primary_key=True, index=True)
    username = Column(String(50), unique=True, index=True, nullable=False)
    password_hash = Column(String(100), nullable=False)
    role = Column(String(20), default="user")  # user | admin
    created_at = Column(DateTime(timezone=True), server_default=func.now())
    last_login = Column(DateTime(timezone=True))

class Topic(Base):
    __tablename__ = "topics"

    id = Column(String(50), primary_key=True, index=True)
    title = Column(String(500), nullable=False)
    field = Column(String(100))
    priority_score = Column(Integer, default=0)
    status = Column(String(50), default="待处理")  # 待处理 | 待审查 | 待发布 | 已发布
    compliance_score = Column(Float, default=0.0)
    created_at = Column(DateTime(timezone=True), server_default=func.now())
    updated_at = Column(
        DateTime(timezone=True),
        server_default=func.now(),
        onupdate=func.now()
    )
    generated_at = Column(DateTime(timezone=True))  # 新增字段
    published_at = Column(DateTime(timezone=True))   # 新增字段
    platform_urls = Column(JSON)                    # {"zhihu": "url", ...}

class AuditLog(Base):
    __tablename__ = "audit_logs"

    id = Column(Integer, primary_key=True, index=True)
    username = Column(String(50), nullable=False)
    user_id = Column(Integer, index=True)
    action = Column(String(50), index=True)         # login, create_user, delete_user, publish
    resource_type = Column(String(50))              # user, topic, article
    resource_id = Column(Integer)
    details = Column(String(500))
    ip_address = Column(String(45))
    user_agent = Column(String(200))
    timestamp = Column(DateTime(timezone=True), server_default=func.now())

class GenerateTask(Base):
    __tablename__ = "generate_tasks"

    id = Column(Integer, primary_key=True, index=True)
    topic_id = Column(Integer, index=True)
    status = Column(String(20), default="pending")  # pending, processing, completed, failed
    result = Column(JSON)                           # 存储生成结果或错误信息
    created_by = Column(Integer)
    created_at = Column(DateTime(timezone=True), server_default=func.now())
    updated_at = Column(
        DateTime(timezone=True),
        server_default=func.now(),
        onupdate=func.now()
    )

class PublishRecord(Base):
    __tablename__ = "publish_records"

    id = Column(Integer, primary_key=True, index=True)
    topic_id = Column(Integer, index=True)
    platform = Column(String(50))                   # zhihu, wechat, xiaohongshu
    url = Column(String(500))
    status = Column(String(20), default="success")  # success, failed
    error_message = Column(String(500))
    created_by = Column(Integer)
    created_at = Column(DateTime(timezone=True), server_default=func.now())