module.exports = (sequelize, DataTypes) => {
  const SalesReturn = sequelize.define('SalesReturn', {
    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
    },
    customer_id: {
      type: DataTypes.INTEGER,
      allowNull: false,
      references: { model: 'BusinessPartner', key: 'id' }
    },
    invoice_id: {
      type: DataTypes.INTEGER,
      allowNull: false,
      references: { model: 'SalesInvoice', key: 'id' }
    },
    so_id: {
      type: DataTypes.INTEGER,
      allowNull: true,
      references: { model: 'SalesOrder', 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: 'sales_return',
    indexes: [
      { fields: ['company_id', 'plant_id', 'fiscal_year_id'] },
      { fields: ['return_number'], unique: true },
      { fields: ['invoice_id'] },
      { fields: ['customer_id'] },
      { fields: ['status'] }
    ]
  });

  SalesReturn.associate = function(models) {
    SalesReturn.belongsTo(models.Company, { foreignKey: 'company_id', as: 'company' });
    SalesReturn.belongsTo(models.Plant, { foreignKey: 'plant_id', as: 'plant' });
    SalesReturn.belongsTo(models.FiscalYear, { foreignKey: 'fiscal_year_id', as: 'fiscalYear' });
    SalesReturn.belongsTo(models.BusinessPartner, { foreignKey: 'customer_id', as: 'customer' });
    SalesReturn.belongsTo(models.SalesInvoice, { foreignKey: 'invoice_id', as: 'salesInvoice' });
    SalesReturn.belongsTo(models.SalesOrder, { foreignKey: 'so_id', as: 'salesOrder' });
    SalesReturn.belongsTo(models.StorageLocation, { foreignKey: 'warehouse_id', as: 'warehouse' });
    SalesReturn.hasMany(models.SalesReturnItem, { foreignKey: 'sales_return_id', as: 'items' });
    SalesReturn.belongsTo(models.User, { foreignKey: 'approved_by', as: 'approvedByUser' });
    SalesReturn.belongsTo(models.User, { foreignKey: 'created_by', as: 'createdByUser' });
    SalesReturn.belongsTo(models.User, { foreignKey: 'updated_by', as: 'updatedByUser' });
  };

  return SalesReturn;
};
