from sqlalchemy import Column, Integer, String, Float, DateTime, Text, Boolean, ForeignKey, func
from app.database import Base


class QcTask(Base):
    """检验任务"""
    __tablename__ = "qc_task"
    id = Column(Integer, primary_key=True, index=True)
    task_code = Column(String(50), unique=True, nullable=False, index=True)
    coil_no = Column(String(30), nullable=True, index=True)
    task_type = Column(String(30), nullable=True)  # e.g. incoming/process/final
    scheme_name = Column(String(100), nullable=True)
    status = Column(Integer, default=0)  # 0=待检验 1=检验中 2=待审核 3=完成
    inspect_user = Column(String(50), nullable=True)
    inspect_time = Column(DateTime, nullable=True)
    audit_user = Column(String(50), nullable=True)
    audit_time = Column(DateTime, nullable=True)
    result = Column(String(20), nullable=True)  # qualified/unqualified
    remark = Column(Text, nullable=True)
    del_flag = Column(Integer, default=0)
    created_at = Column(DateTime, server_default=func.now())
    updated_at = Column(DateTime, server_default=func.now(), onupdate=func.now())


class QcTaskItem(Base):
    """检验项目"""
    __tablename__ = "qc_task_item"
    id = Column(Integer, primary_key=True, index=True)
    task_id = Column(Integer, ForeignKey("qc_task.id"), nullable=False, index=True)
    item_name = Column(String(100), nullable=False)
    item_type = Column(String(20), nullable=True)  # quantitative/qualitative
    standard_value = Column(Float, nullable=True)
    upper_limit = Column(Float, nullable=True)
    lower_limit = Column(Float, nullable=True)
    unit = Column(String(20), nullable=True)
    inspect_value = Column(String(50), nullable=True)
    is_qualified = Column(Integer, nullable=True)  # 1=qualified 0=unqualified
    judge_result = Column(String(100), nullable=True)
    inspect_user = Column(String(50), nullable=True)
    inspect_time = Column(DateTime, nullable=True)
    created_at = Column(DateTime, server_default=func.now())


class QcDefect(Base):
    """钢卷缺陷记录"""
    __tablename__ = "qc_defect"
    id = Column(Integer, primary_key=True, index=True)
    coil_no = Column(String(30), nullable=True, index=True)
    seq_no = Column(Integer, nullable=True, comment="序号")
    defect_desc = Column(String(200), nullable=True, comment="缺陷描述")
    start_position = Column(Float, nullable=True, comment="开始位置")
    end_position = Column(Float, nullable=True, comment="结束位置")
    length_val = Column(Float, nullable=True, comment="长度")
    upper_surface = Column(Boolean, default=False, comment="上板面")
    lower_surface = Column(Boolean, default=False, comment="下板面")
    side_op = Column(Boolean, default=False, comment="操作侧")
    side_middle = Column(Boolean, default=False, comment="中间")
    side_drive = Column(Boolean, default=False, comment="驱动侧")
    is_main = Column(Boolean, default=False, comment="主缺陷")
    inherit_source = Column(String(30), nullable=True, comment="继承来源卷号")
    image_url = Column(Text, nullable=True, comment="缺陷图片(URL或base64)")

    # 兼容旧字段
    production_line = Column(String(50), nullable=True)
    position = Column(String(50), nullable=True)
    plate_surface = Column(String(20), nullable=True)
    defect_code = Column(String(30), nullable=True)
    defect_type = Column(String(50), nullable=True, index=True)
    defect_rate = Column(Float, nullable=True)
    defect_weight = Column(Float, nullable=True)
    degree = Column(String(20), nullable=True)  # light/medium/serious
    judge_level = Column(String(20), nullable=True)
    judge_by = Column(String(50), nullable=True)
    judge_time = Column(DateTime, nullable=True)
    main_mark = Column(Integer, nullable=True)
    whole_coil_mark = Column(Integer, nullable=True)
    remark = Column(Text, nullable=True)
    del_flag = Column(Integer, default=0)
    created_at = Column(DateTime, server_default=func.now())
    updated_at = Column(DateTime, server_default=func.now(), onupdate=func.now())
