module.exports = (sequelize, DataTypes) => {
  const InventoryGoodsIssue = sequelize.define('InventoryGoodsIssue', {
    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'
      }
    },
    movement_number: {
      type: DataTypes.STRING(50),
      allowNull: false
    },
    movement_date: {
      type: DataTypes.DATEONLY,
      allowNull: false
    },
    movement_type: {
      type: DataTypes.STRING(10),
      allowNull: false,
      defaultValue: '201',
      comment: 'SAP Movement Type: 201=Goods Issue, 261=Goods Issue for Production'
    },
    reference_type: {
      type: DataTypes.ENUM('PRODUCTION_ORDER', 'SCRAP', 'CONSUMPTION', 'INTERNAL_USE', 'OTHER'),
      allowNull: true
    },
    reference_number: {
      type: DataTypes.STRING(100),
      allowNull: true
    },
    cost_center_id: {
      type: DataTypes.INTEGER,
      allowNull: true,
      references: {
        model: 'CostCenter',
        key: 'id'
      }
    },
    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_goods_issue',
    indexes: [
      {
        fields: ['company_id', 'plant_id', 'fiscal_year_id']
      },
      {
        fields: ['movement_number']
      },
      {
        fields: ['movement_date']
      },
      {
        fields: ['status']
      }
    ]
  });

  InventoryGoodsIssue.associate = function(models) {
    InventoryGoodsIssue.belongsTo(models.Company, { foreignKey: 'company_id', as: 'company' });
    InventoryGoodsIssue.belongsTo(models.Plant, { foreignKey: 'plant_id', as: 'plant' });
    InventoryGoodsIssue.belongsTo(models.FiscalYear, { foreignKey: 'fiscal_year_id', as: 'fiscalYear' });
    InventoryGoodsIssue.belongsTo(models.CostCenter, { foreignKey: 'cost_center_id', as: 'costCenter' });
    InventoryGoodsIssue.hasMany(models.InventoryGoodsIssueItem, { foreignKey: 'movement_id', as: 'items' });
    InventoryGoodsIssue.belongsTo(models.User, { foreignKey: 'posted_by', as: 'postedByUser' });
    InventoryGoodsIssue.belongsTo(models.User, { foreignKey: 'created_by', as: 'createdByUser' });
    InventoryGoodsIssue.belongsTo(models.User, { foreignKey: 'updated_by', as: 'updatedByUser' });
  };

  return InventoryGoodsIssue;
};

