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


class QualityRecord(Base):
    """质量检验记录"""
    __tablename__ = "quality_records"

    id                    = Column(Integer, primary_key=True, index=True)
    coil_no               = Column(String(30), nullable=False, index=True, comment="卷号")
    production_record_id  = Column(Integer, ForeignKey("production_records.id"), nullable=True, comment="关联生产实绩")

    # 实测规格
    thickness_actual      = Column(Float, nullable=True, comment="实测厚度 mm")
    width_actual          = Column(Float, nullable=True, comment="实测宽度 mm")
    flatness              = Column(Float, nullable=True, comment="平直度 IU")
    crown                 = Column(Float, nullable=True, comment="凸度 μm")

    # 表面缺陷
    surface_defect_type   = Column(String(50), nullable=True, comment="表面缺陷类型")
    defect_length_m       = Column(Float, nullable=True, comment="缺陷长度 m")
    defect_position       = Column(String(50), nullable=True, comment="缺陷位置")

    # 质量模型评分
    pi_score              = Column(Float, nullable=True, comment="酸洗指数评分 0-100")
    surface_score         = Column(Float, nullable=True, comment="表面质量评分 0-100")
    overall_grade         = Column(String(5), nullable=True, comment="综合等级 A1/A2/B1/B2/C")

    # 残酸 / 粗糙度
    acid_residual         = Column(Float, nullable=True, comment="残酸量 g/m²")
    roughness_ra          = Column(Float, nullable=True, comment="粗糙度 Ra μm")

    # 检验信息
    inspector             = Column(String(50), nullable=True, comment="检验员")
    inspect_time          = Column(DateTime, nullable=True, comment="检验时间")
    is_passed             = Column(Boolean, default=True, comment="是否合格")

    created_at            = Column(DateTime, server_default=func.now())
