module.exports = (sequelize, DataTypes) => {
  const SubcontractDC = sequelize.define('SubcontractDC', {
    id: {
      type: DataTypes.INTEGER,
      primaryKey: true,
      autoIncrement: true
    },
    company_id: {
      type: DataTypes.INTEGER,
      allowNull: false,
      references: { model: 'Company', key: 'id' }
    },
    plant_id: {
      type: DataTypes.INTEGER,
      allowNull: false,
      references: { model: 'Plant', key: 'id' }
    },
    fiscal_year_id: {
      type: DataTypes.INTEGER,
      allowNull: false,
      references: { model: 'FiscalYear', key: 'id' }
    },
    dc_number: {
      type: DataTypes.STRING(50),
      allowNull: false
    },
    dc_date: {
      type: DataTypes.DATEONLY,
      allowNull: false
    },
    vendor_id: {
      type: DataTypes.INTEGER,
      allowNull: false,
      references: { model: 'BusinessPartner', key: 'id' }
    },
    operation_type: {
      type: DataTypes.STRING(120),
      allowNull: false
    },
    purpose: {
      type: DataTypes.STRING(200),
      allowNull: true
    },
    subcontract_po_ref: {
      type: DataTypes.STRING(80),
      allowNull: true
    },
    customer_po_id: {
      type: DataTypes.INTEGER,
      allowNull: true,
      references: { model: 'CustomerPO', key: 'id' }
    },
    delivery_address: {
      type: DataTypes.TEXT,
      allowNull: true
    },
    transport_name: {
      type: DataTypes.STRING(120),
      allowNull: true
    },
    vehicle_no: {
      type: DataTypes.STRING(40),
      allowNull: true
    },
    driver_name: {
      type: DataTypes.STRING(120),
      allowNull: true
    },
    driver_phone: {
      type: DataTypes.STRING(20),
      allowNull: true
    },
    eway_bill_no: {
      type: DataTypes.STRING(80),
      allowNull: true
    },
    total_sent_qty: {
      type: DataTypes.DECIMAL(18, 3),
      allowNull: false,
      defaultValue: 0
    },
    total_received_qty: {
      type: DataTypes.DECIMAL(18, 3),
      allowNull: false,
      defaultValue: 0
    },
    total_amount: {
      type: DataTypes.DECIMAL(18, 2),
      allowNull: false,
      defaultValue: 0
    },
    remarks: {
      type: DataTypes.TEXT,
      allowNull: true
    },
    status: {
      type: DataTypes.ENUM('DRAFT', 'APPROVED', 'SENT', 'PARTIALLY_RECEIVED', 'CLOSED', 'CANCELLED'),
      allowNull: false,
      defaultValue: 'DRAFT'
    },
    created_by: {
      type: DataTypes.INTEGER,
      allowNull: true,
      references: { model: 'User', key: 'id' }
    },
    updated_by: {
      type: DataTypes.INTEGER,
      allowNull: true,
      references: { model: 'User', key: 'id' }
    }
  }, {
    tableName: 'subcontract_dc',
    indexes: [
      { fields: ['company_id', 'plant_id', 'fiscal_year_id'] },
      { unique: true, fields: ['company_id', 'dc_number'] },
      { fields: ['vendor_id'] },
      { fields: ['status'] },
      { fields: ['dc_date'] }
    ]
  });

  SubcontractDC.associate = function(models) {
    SubcontractDC.belongsTo(models.Company, { foreignKey: 'company_id', as: 'company' });
    SubcontractDC.belongsTo(models.Plant, { foreignKey: 'plant_id', as: 'plant' });
    SubcontractDC.belongsTo(models.FiscalYear, { foreignKey: 'fiscal_year_id', as: 'fiscalYear' });
    SubcontractDC.belongsTo(models.BusinessPartner, { foreignKey: 'vendor_id', as: 'vendor' });
    SubcontractDC.belongsTo(models.CustomerPO, { foreignKey: 'customer_po_id', as: 'customerPO' });
    SubcontractDC.belongsTo(models.User, { foreignKey: 'created_by', as: 'createdByUser' });
    SubcontractDC.belongsTo(models.User, { foreignKey: 'updated_by', as: 'updatedByUser' });
    SubcontractDC.hasMany(models.SubcontractDCItem, { foreignKey: 'subcontract_dc_id', as: 'items' });
    SubcontractDC.hasMany(models.SubcontractReceipt, { foreignKey: 'subcontract_dc_id', as: 'receipts' });
  };

  return SubcontractDC;
};
