module.exports = (sequelize, DataTypes) => {
  const GeneralDC = sequelize.define('GeneralDC', {
    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
    },
    machine_id: {
      type: DataTypes.INTEGER,
      allowNull: false,
      references: { model: 'Machine', key: 'id' }
    },
    vendor_id: {
      type: DataTypes.INTEGER,
      allowNull: true,
      references: { model: 'BusinessPartner', key: 'id' }
    },
    operation_type: {
      type: DataTypes.STRING(120),
      allowNull: true
    },
    reference_no: {
      type: DataTypes.STRING(80),
      allowNull: true
    },
    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: 'general_dc',
    indexes: [
      { fields: ['company_id', 'plant_id', 'fiscal_year_id'] },
      { unique: true, fields: ['company_id', 'dc_number'] },
      { fields: ['machine_id'] },
      { fields: ['vendor_id'] },
      { fields: ['status'] },
      { fields: ['dc_date'] }
    ]
  });

  GeneralDC.associate = function(models) {
    GeneralDC.belongsTo(models.Company, { foreignKey: 'company_id', as: 'company' });
    GeneralDC.belongsTo(models.Plant, { foreignKey: 'plant_id', as: 'plant' });
    GeneralDC.belongsTo(models.FiscalYear, { foreignKey: 'fiscal_year_id', as: 'fiscalYear' });
    GeneralDC.belongsTo(models.Machine, { foreignKey: 'machine_id', as: 'machine' });
    GeneralDC.belongsTo(models.BusinessPartner, { foreignKey: 'vendor_id', as: 'vendor' });
    GeneralDC.belongsTo(models.User, { foreignKey: 'created_by', as: 'createdByUser' });
    GeneralDC.belongsTo(models.User, { foreignKey: 'updated_by', as: 'updatedByUser' });
    GeneralDC.hasMany(models.GeneralDCItem, { foreignKey: 'general_dc_id', as: 'items' });
    GeneralDC.hasMany(models.GeneralReceipt, { foreignKey: 'general_dc_id', as: 'receipts' });
  };

  return GeneralDC;
};
