import enum
from sqlalchemy import Column, Integer, String, Float, DateTime, Text, Enum, func
from app.database import Base


class L3Status(str, enum.Enum):
    pending   = "pending"
    sent      = "sent"
    confirmed = "confirmed"
    cancelled = "cancelled"


class L2Status(str, enum.Enum):
    pending    = "pending"
    processing = "processing"
    done       = "done"


class PDIRecord(Base):
    """PDI (Process Data Input) – per-coil process order from L3."""
    __tablename__ = "pdi_records"

    id               = Column(Integer, primary_key=True, index=True)
    coil_no          = Column(String(30), nullable=False, index=True, comment="卷号")
    order_no         = Column(String(30), nullable=True, comment="订单号")
    customer         = Column(String(100), nullable=True, comment="客户名称")
    steel_grade      = Column(String(30), nullable=True, comment="钢种")

    # 几何规格
    thickness        = Column(Float, nullable=True, comment="来料厚度 mm")
    width            = Column(Float, nullable=True, comment="来料宽度 mm")
    target_thickness = Column(Float, nullable=True, comment="目标厚度 mm")
    target_width     = Column(Float, nullable=True, comment="目标宽度 mm")

    # 力学性能
    yield_strength   = Column(Float, nullable=True, comment="屈服强度 MPa")
    tensile_strength = Column(Float, nullable=True, comment="抗拉强度 MPa")
    elongation       = Column(Float, nullable=True, comment="延伸率 %")

    # 卷重/尺寸
    coil_weight      = Column(Float, nullable=True, comment="卷重 kg")
    inner_diameter   = Column(Float, nullable=True, comment="内径 mm")
    outer_diameter   = Column(Float, nullable=True, comment="外径 mm")

    # 工艺
    process_route    = Column(String(100), nullable=True, comment="工艺路径 e.g. P1+P2+P3+P4+P5+P6")
    priority         = Column(Integer, default=3, comment="优先级 1-5")

    # 状态
    l3_status        = Column(Enum(L3Status), default=L3Status.pending, comment="L3状态")
    l2_status        = Column(Enum(L2Status), default=L2Status.pending, comment="L2状态")

    # 时间戳
    send_time        = Column(DateTime, nullable=True, comment="下发时间")
    confirm_time     = Column(DateTime, nullable=True, comment="确认时间")

    remark           = Column(Text, nullable=True, comment="备注")
    created_at       = Column(DateTime, server_default=func.now())
    updated_at       = Column(DateTime, server_default=func.now(), onupdate=func.now())
