module.exports = (sequelize, DataTypes) => {
  const MaterialReturnNote = sequelize.define('MaterialReturnNote', {
    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'
      }
    },
    mrn_number: {
      type: DataTypes.STRING(50),
      allowNull: false,
      unique: true
    },
    return_date: {
      type: DataTypes.DATEONLY,
      allowNull: false
    },
    production_order_id: {
      type: DataTypes.INTEGER,
      allowNull: true,
      references: {
        model: 'ProductionOrder',
        key: 'id'
      }
    },
    department_id: {
      type: DataTypes.INTEGER,
      allowNull: true,
      references: {
        model: 'Department',
        key: 'id'
      },
      comment: 'Department that returned the materials to stores'
    },
    material_issue_id: {
      type: DataTypes.INTEGER,
      allowNull: true,
      references: {
        model: 'MaterialIssue',
        key: 'id'
      }
    },
    returned_by: {
      type: DataTypes.INTEGER,
      allowNull: true,
      references: {
        model: 'User',
        key: 'id'
      }
    },
    received_by: {
      type: DataTypes.INTEGER,
      allowNull: true,
      references: {
        model: 'User',
        key: 'id'
      }
    },
    status: {
      type: DataTypes.ENUM('DRAFT', 'IN_TRANSIT', '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: 'material_return_note',
    indexes: [
      { fields: ['company_id', 'plant_id', 'fiscal_year_id'] },
      { fields: ['mrn_number'], unique: true },
      { fields: ['production_order_id'] },
      { fields: ['material_issue_id'] },
      { fields: ['department_id'] },
      { fields: ['status'] }
    ]
  });

  MaterialReturnNote.associate = function(models) {
    MaterialReturnNote.belongsTo(models.Company, { foreignKey: 'company_id', as: 'company' });
    MaterialReturnNote.belongsTo(models.Plant, { foreignKey: 'plant_id', as: 'plant' });
    MaterialReturnNote.belongsTo(models.FiscalYear, { foreignKey: 'fiscal_year_id', as: 'fiscalYear' });
    MaterialReturnNote.belongsTo(models.ProductionOrder, { foreignKey: 'production_order_id', as: 'productionOrder' });
    MaterialReturnNote.belongsTo(models.Department, { foreignKey: 'department_id', as: 'department' });
    MaterialReturnNote.belongsTo(models.MaterialIssue, { foreignKey: 'material_issue_id', as: 'materialIssue' });
    MaterialReturnNote.hasMany(models.MaterialReturnNoteItem, { foreignKey: 'mrn_id', as: 'items' });
    MaterialReturnNote.belongsTo(models.User, { foreignKey: 'returned_by', as: 'returnedByUser' });
    MaterialReturnNote.belongsTo(models.User, { foreignKey: 'received_by', as: 'receivedByUser' });
    MaterialReturnNote.belongsTo(models.User, { foreignKey: 'posted_by', as: 'postedByUser' });
    MaterialReturnNote.belongsTo(models.User, { foreignKey: 'created_by', as: 'createdByUser' });
    MaterialReturnNote.belongsTo(models.User, { foreignKey: 'updated_by', as: 'updatedByUser' });
  };

  return MaterialReturnNote;
};

