"""
数据库模型定义
"""
from sqlalchemy import Column, Integer, String, Float, DateTime, ForeignKey, Boolean
from sqlalchemy.orm import relationship
from datetime import datetime
from backend.database import Base
class Student(Base):
    """学生表"""
    __tablename__ = "students"
    
    id = Column(Integer, primary_key=True, index=True, autoincrement=True)
    student_id = Column(String(50), unique=True, index=True, nullable=False, comment="学号")
    name = Column(String(100), nullable=False, comment="姓名")
    major = Column(String(100), comment="专业")
    total_score = Column(Float, default=0.0, comment="总积分")
    random_rollcall_count = Column(Integer, default=0, comment="随机点名次数")
    order_rollcall_count = Column(Integer, default=0, comment="顺序点名次数")
    transfer_right_count = Column(Integer, default=0, comment="转移权次数")
    created_at = Column(DateTime, default=datetime.now, comment="创建时间")
    updated_at = Column(DateTime, default=datetime.now, onupdate=datetime.now, comment="更新时间")
    
    # 关系
    rollcall_records = relationship("RollCallRecord", back_populates="student")
    score_records = relationship("ScoreRecord", back_populates="student")

class RollCallRecord(Base):
    """点名记录表"""
    __tablename__ = "rollcall_records"
    
    id = Column(Integer, primary_key=True, index=True, autoincrement=True)
    student_id = Column(Integer, ForeignKey("students.id"), nullable=False, comment="学生ID")
    rollcall_type = Column(String(20), nullable=False, comment="点名类型：random/order")
    is_present = Column(Boolean, default=False, comment="是否到达")
    question_repeat_score = Column(Float, default=0.0, comment="重复问题得分")
    answer_score = Column(Float, default=0.0, comment="回答问题得分")
    attendance_score = Column(Float, default=0.0, comment="到达得分")
    total_score_change = Column(Float, default=0.0, comment="本次积分变化")
    event_type = Column(String(50), comment="随机事件类型")
    created_at = Column(DateTime, default=datetime.now, comment="创建时间")
    
    # 关系
    student = relationship("Student", back_populates="rollcall_records")

class ScoreRecord(Base):
    """积分记录表"""
    __tablename__ = "score_records"
    
    id = Column(Integer, primary_key=True, index=True, autoincrement=True)
    student_id = Column(Integer, ForeignKey("students.id"), nullable=False, comment="学生ID")
    score_change = Column(Float, nullable=False, comment="积分变化")
    reason = Column(String(200), comment="积分变化原因")
    created_at = Column(DateTime, default=datetime.now, comment="创建时间")
    
    # 关系
    student = relationship("Student", back_populates="score_records")

