from sqlalchemy import Column, Integer, String, DateTime, Float, UniqueConstraint, BigInteger
from sqlalchemy.sql import func
from app.models import Base
from app.config import TABLE_PREFIX

class DitRecord(Base):
    __tablename__ = f"{TABLE_PREFIX}DIT_Records"
    __table_args__ = (
        UniqueConstraint('op_id', 'pn', name='uix_dit_op_pn'),
    )

    id = Column(Integer, primary_key=True, index=True)
    op_id = Column(String(255), index=True, nullable=False)  # 移除 unique，因為同一 op_id 可有多個 pn
    op_name = Column(String(255), nullable=True) # Opportunity Name
    erp_account = Column(String(100), index=True) # AQ 欄
    customer = Column(String(255), nullable=False, index=True)
    customer_normalized = Column(String(255), index=True)
    pn = Column(String(100), nullable=False, index=True)
    eau = Column(BigInteger, default=0)
    stage = Column(String(50))
    date = Column(String(20))
    created_at = Column(DateTime(timezone=True), server_default=func.now())
    updated_at = Column(DateTime(timezone=True), onupdate=func.now())
