module.exports = (sequelize, DataTypes) => {
  const InventoryStockAdjustment = sequelize.define('InventoryStockAdjustment', {
    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'
      }
    },
    adjustment_number: {
      type: DataTypes.STRING(50),
      allowNull: false
    },
    adjustment_date: {
      type: DataTypes.DATEONLY,
      allowNull: false
    },
    adjustment_type: {
      type: DataTypes.ENUM('CYCLE_COUNT', 'PHYSICAL_INVENTORY', 'QUANTITY_ADJUSTMENT', 'VALUE_ADJUSTMENT'),
      allowNull: false,
      defaultValue: 'QUANTITY_ADJUSTMENT'
    },
    storage_location: {
      type: DataTypes.STRING(10),
      allowNull: true
    },
    status: {
      type: DataTypes.ENUM('DRAFT', 'POSTED', 'CANCELLED'),
      allowNull: false,
      defaultValue: 'DRAFT'
    },
    remarks: {
      type: DataTypes.TEXT,
      allowNull: true
    },
    posted_at: {
      type: DataTypes.DATE,
      allowNull: true
    },
    posted_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: 'inventory_stock_adjustment',
    indexes: [
      {
        fields: ['company_id', 'plant_id', 'fiscal_year_id']
      },
      {
        fields: ['adjustment_number']
      },
      {
        fields: ['adjustment_date']
      },
      {
        fields: ['status']
      }
    ]
  });

  InventoryStockAdjustment.associate = function(models) {
    InventoryStockAdjustment.belongsTo(models.Company, { foreignKey: 'company_id', as: 'company' });
    InventoryStockAdjustment.belongsTo(models.Plant, { foreignKey: 'plant_id', as: 'plant' });
    InventoryStockAdjustment.belongsTo(models.FiscalYear, { foreignKey: 'fiscal_year_id', as: 'fiscalYear' });
    InventoryStockAdjustment.hasMany(models.InventoryStockAdjustmentItem, { foreignKey: 'adjustment_id', as: 'items' });
    InventoryStockAdjustment.belongsTo(models.User, { foreignKey: 'posted_by', as: 'postedByUser' });
    InventoryStockAdjustment.belongsTo(models.User, { foreignKey: 'created_by', as: 'createdByUser' });
    InventoryStockAdjustment.belongsTo(models.User, { foreignKey: 'updated_by', as: 'updatedByUser' });
  };

  return InventoryStockAdjustment;
};

