module.exports = (sequelize, DataTypes) => {
  const DeliveryChallan = sequelize.define('DeliveryChallan', {
    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' }
    },
    challan_no: { type: DataTypes.STRING(60), allowNull: false },
    dc_date: { type: DataTypes.DATEONLY, allowNull: false },
    dispatch_id: {
      type: DataTypes.INTEGER,
      allowNull: false,
      references: { model: 'FGDispatch', key: 'id' }
    },
    dispatch_date: { type: DataTypes.DATE, allowNull: true },
    invoice_id: {
      type: DataTypes.INTEGER,
      allowNull: false,
      references: { model: 'SalesInvoice', key: 'id' }
    },
    customer_id: {
      type: DataTypes.INTEGER,
      allowNull: false,
      references: { model: 'BusinessPartner', key: 'id' }
    },
    customer_po_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(30), allowNull: true },
    eway_bill_no: { type: DataTypes.STRING(80), allowNull: true },
    payment_type: {
      type: DataTypes.ENUM('CREDIT', 'COD'),
      allowNull: false,
      defaultValue: 'CREDIT'
    },
    status: {
      type: DataTypes.ENUM('DRAFT', 'DELIVERED', 'CLOSED', 'CANCELLED'),
      allowNull: false,
      defaultValue: 'DRAFT'
    },
    delivery_status: {
      type: DataTypes.ENUM('IN_TRANSIT', 'DELIVERED'),
      allowNull: false,
      defaultValue: 'IN_TRANSIT'
    },
    remarks: { type: DataTypes.TEXT, allowNull: true },
    delivered_by_name: { type: DataTypes.STRING(120), allowNull: true },
    received_by: { type: DataTypes.STRING(120), allowNull: true },
    delivery_date: { type: DataTypes.DATEONLY, allowNull: true },
    delivery_time: { type: DataTypes.TIME, allowNull: true },
    receiver_signature: { type: DataTypes.TEXT, allowNull: true },
    delivery_proof_path: { type: DataTypes.STRING(255), allowNull: true },
    delivered_at: { type: DataTypes.DATE, allowNull: true },
    delivered_by: {
      type: DataTypes.INTEGER,
      allowNull: true,
      references: { model: 'User', key: 'id' }
    },
    closed_at: { type: DataTypes.DATE, allowNull: true },
    closed_by: {
      type: DataTypes.INTEGER,
      allowNull: true,
      references: { model: 'User', key: 'id' }
    },
    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: 'delivery_challan',
    indexes: [
      { unique: true, fields: ['company_id', 'plant_id', 'fiscal_year_id', 'challan_no'] },
      { fields: ['dispatch_id'] },
      { fields: ['invoice_id'] },
      { fields: ['customer_id'] },
      { fields: ['status'] },
      { fields: ['delivery_status'] },
      { fields: ['dc_date'] }
    ]
  });

  DeliveryChallan.associate = function(models) {
    DeliveryChallan.belongsTo(models.Company, { foreignKey: 'company_id', as: 'company' });
    DeliveryChallan.belongsTo(models.Plant, { foreignKey: 'plant_id', as: 'plant' });
    DeliveryChallan.belongsTo(models.FiscalYear, { foreignKey: 'fiscal_year_id', as: 'fiscalYear' });
    DeliveryChallan.belongsTo(models.FGDispatch, { foreignKey: 'dispatch_id', as: 'dispatch' });
    DeliveryChallan.belongsTo(models.SalesInvoice, { foreignKey: 'invoice_id', as: 'invoice' });
    DeliveryChallan.belongsTo(models.BusinessPartner, { foreignKey: 'customer_id', as: 'customer' });
    DeliveryChallan.hasMany(models.DeliveryChallanItem, { foreignKey: 'delivery_challan_id', as: 'items' });
    DeliveryChallan.hasMany(models.ReceiptEntry, { foreignKey: 'delivery_challan_id', as: 'receipts' });
    DeliveryChallan.belongsTo(models.User, { foreignKey: 'created_by', as: 'createdByUser' });
    DeliveryChallan.belongsTo(models.User, { foreignKey: 'updated_by', as: 'updatedByUser' });
    DeliveryChallan.belongsTo(models.User, { foreignKey: 'delivered_by', as: 'deliveredByUser' });
    DeliveryChallan.belongsTo(models.User, { foreignKey: 'closed_by', as: 'closedByUser' });
  };

  return DeliveryChallan;
};

