module.exports = (sequelize, DataTypes) => {
  const FGDispatch = sequelize.define(
    'FGDispatch',
    {
      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' }
      },
      dispatch_no: {
        type: DataTypes.STRING(60),
        allowNull: false,
        unique: true
      },
      dispatch_date: {
        type: DataTypes.DATE,
        allowNull: false
      },
      invoice_id: {
        type: DataTypes.INTEGER,
        allowNull: true,
        references: { model: 'SalesInvoice', key: 'id' }
      },
      customer_id: {
        type: DataTypes.INTEGER,
        allowNull: true,
        references: { model: 'BusinessPartner', key: 'id' }
      },
      item_id: {
        type: DataTypes.INTEGER,
        allowNull: false,
        references: { model: 'Material', key: 'id' }
      },
      warehouse_id: {
        type: DataTypes.INTEGER,
        allowNull: false,
        references: { model: 'StorageLocation', key: 'id' }
      },
      route_card_id: {
        type: DataTypes.INTEGER,
        allowNull: true,
        references: { model: 'RouteCard', key: 'id' }
      },
      work_order_id: {
        type: DataTypes.INTEGER,
        allowNull: true,
        references: { model: 'WorkOrder', key: 'id' }
      },
      production_qty: {
        type: DataTypes.DECIMAL(18, 3),
        allowNull: false,
        defaultValue: 0
      },
      accepted_qty: {
        type: DataTypes.DECIMAL(18, 3),
        allowNull: false,
        defaultValue: 0
      },
      available_stock: {
        type: DataTypes.DECIMAL(18, 3),
        allowNull: false,
        defaultValue: 0
      },
      dispatch_qty: {
        type: DataTypes.DECIMAL(18, 3),
        allowNull: false,
        defaultValue: 0
      },
      balance_after_dispatch: {
        type: DataTypes.DECIMAL(18, 3),
        allowNull: false,
        defaultValue: 0
      },
      status: {
        type: DataTypes.ENUM('PENDING', 'IN_TRANSIT', 'COMPLETED', 'CANCELLED'),
        allowNull: false,
        defaultValue: 'PENDING'
      },
      transporter_name: {
        type: DataTypes.STRING(150),
        allowNull: true
      },
      vehicle_no: {
        type: DataTypes.STRING(50),
        allowNull: true
      },
      driver_name: {
        type: DataTypes.STRING(120),
        allowNull: true
      },
      submitted_at: {
        type: DataTypes.DATE,
        allowNull: true
      },
      submitted_by: {
        type: DataTypes.INTEGER,
        allowNull: true,
        references: { model: 'User', key: 'id' }
      },
      approved_at: {
        type: DataTypes.DATE,
        allowNull: true
      },
      approved_by: {
        type: DataTypes.INTEGER,
        allowNull: true,
        references: { model: 'User', key: 'id' }
      },
      transit_started_at: {
        type: DataTypes.DATE,
        allowNull: true
      },
      transit_started_by: {
        type: DataTypes.INTEGER,
        allowNull: true,
        references: { model: 'User', key: 'id' }
      },
      delivery_status: {
        type: DataTypes.ENUM('NOT_DELIVERED', 'PARTIALLY_DELIVERED', 'FULLY_DELIVERED'),
        allowNull: false,
        defaultValue: 'NOT_DELIVERED'
      },
      remarks: {
        type: DataTypes.TEXT,
        allowNull: true
      },
      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: 'fg_dispatch',
      indexes: [
        { unique: true, fields: ['dispatch_no'] },
        { fields: ['company_id', 'plant_id', 'fiscal_year_id'] },
        { fields: ['dispatch_date'] },
        { fields: ['invoice_id'] },
        { fields: ['item_id', 'warehouse_id'] },
        { fields: ['route_card_id'] },
        { fields: ['work_order_id'] },
        { fields: ['status'] },
        { fields: ['delivery_status'] },
        { fields: ['submitted_at'] },
        { fields: ['approved_at'] },
        { fields: ['transit_started_at'] }
      ]
    }
  );

  FGDispatch.associate = function (models) {
    FGDispatch.belongsTo(models.Company, { foreignKey: 'company_id', as: 'company' });
    FGDispatch.belongsTo(models.Plant, { foreignKey: 'plant_id', as: 'plant' });
    FGDispatch.belongsTo(models.FiscalYear, { foreignKey: 'fiscal_year_id', as: 'fiscalYear' });
    FGDispatch.belongsTo(models.BusinessPartner, { foreignKey: 'customer_id', as: 'customer' });
    FGDispatch.belongsTo(models.SalesInvoice, { foreignKey: 'invoice_id', as: 'salesInvoice' });
    FGDispatch.belongsTo(models.Material, { foreignKey: 'item_id', as: 'item' });
    FGDispatch.belongsTo(models.StorageLocation, { foreignKey: 'warehouse_id', as: 'warehouse' });
    FGDispatch.belongsTo(models.RouteCard, { foreignKey: 'route_card_id', as: 'routeCard' });
    FGDispatch.belongsTo(models.WorkOrder, { foreignKey: 'work_order_id', as: 'workOrder' });
    FGDispatch.hasMany(models.DeliveryChallan, { foreignKey: 'dispatch_id', as: 'deliveryChallans' });
    FGDispatch.belongsTo(models.User, { foreignKey: 'submitted_by', as: 'submittedByUser' });
    FGDispatch.belongsTo(models.User, { foreignKey: 'approved_by', as: 'approvedByUser' });
    FGDispatch.belongsTo(models.User, { foreignKey: 'transit_started_by', as: 'transitStartedByUser' });
    FGDispatch.belongsTo(models.User, { foreignKey: 'created_by', as: 'createdByUser' });
    FGDispatch.belongsTo(models.User, { foreignKey: 'updated_by', as: 'updatedByUser' });
  };

  return FGDispatch;
};
