module.exports = (sequelize, DataTypes) => {
  const PurchaseReturn = sequelize.define('PurchaseReturn', {
    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' }
    },
    return_number: {
      type: DataTypes.STRING(50),
      allowNull: false,
      unique: true
    },
    return_date: {
      type: DataTypes.DATEONLY,
      allowNull: false
    },
    vendor_id: {
      type: DataTypes.INTEGER,
      allowNull: false,
      references: { model: 'BusinessPartner', key: 'id' }
    },
    grn_id: {
      type: DataTypes.INTEGER,
      allowNull: false,
      references: { model: 'GoodsReceiptNote', key: 'id' }
    },
    po_id: {
      type: DataTypes.INTEGER,
      allowNull: true,
      references: { model: 'PurchaseOrder', key: 'id' }
    },
    warehouse_id: {
      type: DataTypes.INTEGER,
      allowNull: true,
      references: { model: 'StorageLocation', key: 'id' }
    },
    return_reason: {
      type: DataTypes.STRING(200),
      allowNull: true
    },
    remarks: {
      type: DataTypes.TEXT,
      allowNull: true
    },
    status: {
      type: DataTypes.ENUM('DRAFT', 'APPROVED'),
      allowNull: false,
      defaultValue: 'DRAFT'
    },
    approved_at: {
      type: DataTypes.DATE,
      allowNull: true
    },
    approved_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: 'purchase_return',
    indexes: [
      { fields: ['company_id', 'plant_id', 'fiscal_year_id'] },
      { fields: ['return_number'], unique: true },
      { fields: ['grn_id'] },
      { fields: ['vendor_id'] },
      { fields: ['status'] }
    ]
  });

  PurchaseReturn.associate = function(models) {
    PurchaseReturn.belongsTo(models.Company, { foreignKey: 'company_id', as: 'company' });
    PurchaseReturn.belongsTo(models.Plant, { foreignKey: 'plant_id', as: 'plant' });
    PurchaseReturn.belongsTo(models.FiscalYear, { foreignKey: 'fiscal_year_id', as: 'fiscalYear' });
    PurchaseReturn.belongsTo(models.BusinessPartner, { foreignKey: 'vendor_id', as: 'vendor' });
    PurchaseReturn.belongsTo(models.GoodsReceiptNote, { foreignKey: 'grn_id', as: 'grn' });
    PurchaseReturn.belongsTo(models.PurchaseOrder, { foreignKey: 'po_id', as: 'purchaseOrder' });
    PurchaseReturn.belongsTo(models.StorageLocation, { foreignKey: 'warehouse_id', as: 'warehouse' });
    PurchaseReturn.hasMany(models.PurchaseReturnItem, { foreignKey: 'purchase_return_id', as: 'items' });
    PurchaseReturn.belongsTo(models.User, { foreignKey: 'approved_by', as: 'approvedByUser' });
    PurchaseReturn.belongsTo(models.User, { foreignKey: 'created_by', as: 'createdByUser' });
    PurchaseReturn.belongsTo(models.User, { foreignKey: 'updated_by', as: 'updatedByUser' });
  };

  return PurchaseReturn;
};

