# backend/src/models/project.py
from sqlalchemy import Column, Integer, String, Text, Date, Numeric, DateTime, Enum, ForeignKey
from sqlalchemy.sql import func
from sqlalchemy.orm import relationship
from config.database import Base


class Project(Base):
    __tablename__ = "projects"

    id = Column(Integer, primary_key=True, autoincrement=True, comment="项目ID")

    # 基础信息
    project_no = Column(String(50), unique=True, nullable=False, index=True, comment="合同编号")
    power_contract_no = Column(String(100), comment="供电局项目合同编号")
    name = Column(String(200), nullable=False, comment="项目名称")
    subitem_count = Column(Integer, default=0, comment="子项个数")
    subitem_code = Column(String(50), comment="子项编码")
    total_investment = Column(Numeric(15, 2), comment="项目总投资（万元）")
    contract_amount = Column(Numeric(15, 2), comment="中标合同金额（万元）")
    warranty_ratio = Column(Numeric(5, 2), comment="质保金比例")
    settlement_amount = Column(Numeric(15, 2), comment="结算金额（万元）")
    total_cost_estimated = Column(Numeric(15, 2), comment="总成本测算")
    voltage_level = Column(String(50), comment="工程电压等级")
    engineering_type = Column(String(50), index=True, comment="工程类别")
    owner_unit = Column(String(200), index=True, comment="业主单位")
    owner_contact = Column(String(200), comment="业主联系人及电话")
    bidding_type = Column(String(50), comment="中标形式")
    signing_date = Column(Date, index=True, comment="签订日期")
    start_date = Column(Date, index=True, comment="开工日期")
    planned_end_date = Column(Date, index=True, comment="计划竣工日期")
    actual_end_date = Column(Date, index=True, comment="实际竣工日期")

    # 质保信息
    warranty_amount = Column(Numeric(15, 2), default=0, comment="质保金（万元）")
    warranty_expiry_date = Column(Date, index=True, comment="质保期截止日")
    actual_warranty_refund_date = Column(Date, comment="实际退质保金日期")

    # 项目信息
    project_department = Column(String(100), index=True, comment="所属项目部")
    project_leader = Column(String(200), comment="项目负责人及电话")
    payment_method = Column(Text, comment="工程款拨付方式")

    # 成本控制
    total_cost_control = Column(Numeric(15, 2), comment="总体成本（控制）")
    is_adjusted = Column(Enum("是", "否", name="is_adjusted"), default="否", index=True, comment="是否调整")
    labor_cost_control = Column(Numeric(15, 2), comment="人工成本（控制）")
    labor_cost_planned = Column(Numeric(15, 2), comment="农民工工资（计划）")
    labor_cost_paid = Column(Numeric(15, 2), comment="农民工工资（实付）")
    material_cost_control = Column(Numeric(15, 2), comment="乙供材料费（控制）")
    material_cost_payable = Column(Numeric(15, 2), comment="应付材料费")
    material_cost_actual = Column(Numeric(15, 2), comment="实际发生材料费")
    material_cost_paid = Column(Numeric(15, 2), comment="实际支付材料费")
    other_cost_control = Column(Numeric(15, 2), comment="其他费用（控制）")
    other_cost_payable = Column(Numeric(15, 2), comment="应付其他费")
    other_cost_actual = Column(Numeric(15, 2), comment="实际其他费用")

    # 财务信息
    tax_amount = Column(Numeric(15, 2), comment="税金")
    profit = Column(Numeric(15, 2), comment="利润（万元）")
    actual_profit = Column(Numeric(15, 2), comment="实际利润（万元）")
    cost_settlement_amount = Column(Numeric(15, 2), comment="成本结算金额（万元）")
    cumulative_progress = Column(Numeric(5, 2), comment="累计进度")
    receivable_amount = Column(Numeric(15, 2), comment="应收款（完成进度款）")
    invoice_amount = Column(Numeric(15, 2), comment="开票金额（万元）")
    actual_receipt_amount = Column(Numeric(15, 2), comment="实际收款金额（万元）")
    receipt_completion_rate = Column(Numeric(5, 2), comment="实际收款完成率")
    payable_amount = Column(Numeric(15, 2), comment="应付款金额（万元）")
    actual_payment_amount = Column(Numeric(15, 2), comment="实际付款金额（万元）")
    unpaid_amount = Column(Numeric(15, 2), comment="未收款（万元）")
    payment_completion_rate = Column(Numeric(5, 2), comment="实际付款完成率")

    # 民工工资
    labor_debt_amount = Column(Numeric(15, 2), comment="民工工资清欠金额（万元）")

    # 结算信息
    settlement_cost_amount = Column(Numeric(15, 2), comment="结算后成本测算金额")
    settlement_labor_cost = Column(Numeric(15, 2), comment="结算人工费")
    settlement_material_cost = Column(Numeric(15, 2), comment="结算材料费")
    settlement_other_cost = Column(Numeric(15, 2), comment="结算其他费")

    # 统计信息
    due_settlement_count = Column(Integer, comment="到期应结算项目个数")
    unsettlement_count = Column(Integer, comment="到期未完成结算个数")

    # 其他
    problems = Column(Text, comment="存在的问题")
    suggestions = Column(Text, comment="建议措施")
    remarks = Column(Text, comment="备注")

    # 系统字段
    created_by = Column(Integer, ForeignKey("users.id", ondelete="RESTRICT"), nullable=False, index=True, comment="创建人ID")
    created_at = Column(
        DateTime,
        server_default=func.now(),
        comment="创建时间",
    )
    updated_at = Column(
        DateTime,
        server_default=func.now(),
        onupdate=func.now(),
        comment="更新时间",
    )

    creator = relationship("User", back_populates="created_projects")
