'use strict';
const {
  Model
} = require('sequelize');
module.exports = (sequelize, DataTypes) => {
  class Register extends Model {
    /**
     * Helper method for defining associations.
     * This method is not a part of Sequelize lifecycle.
     * The `models/index` file will call this method automatically.
     */
    static associate(models) {
      // define association here
        Register.belongsTo(models.Store, { foreignKey: 'storeId', as: 'store' });
        Register.hasMany(models.CashDrawerActivity, { foreignKey: 'registerId', as: 'cashActivities' });
        Register.hasMany(models.Transaction, { foreignKey: 'registerId', as: 'transactions' });
        Register.belongsTo(models.User, { foreignKey: 'openedBy', as: 'openedUser' });
        Register.belongsTo(models.User, { foreignKey: 'closedBy', as: 'closedUser' });
        Register.hasMany(models.CashDrawerSession, { foreignKey: 'registerId', as: 'cashDrawerSessions' });
        Register.hasMany(models.DailySales, { foreignKey: 'registerId', as: 'dailySales' });

        Register.hasMany(models.Return, { foreignKey: 'registerId', as: 'returns' });

         Register.hasMany(models.RegisterAssignment, {
        foreignKey: 'registerId',
        as: 'assignments'
           });

         // Convenience association to get currently assigned users directly
     Register.belongsToMany(models.User, {
    through: models.RegisterAssignment,
    foreignKey: 'registerId',
    otherKey: 'userId',
    as: 'assignedUsers',
    scope: { isActive: true }
  });
     
       
    }
  }
  Register.init({
    storeId: DataTypes.INTEGER,
    name: DataTypes.STRING,
    code: DataTypes.STRING,
    openingCash: DataTypes.DECIMAL,
    currentCash: DataTypes.DECIMAL,
    isOpen: DataTypes.BOOLEAN,
    openedBy: DataTypes.INTEGER,
    openedAt: DataTypes.DATE,
    closedBy: DataTypes.INTEGER,
    closedAt: DataTypes.DATE,
    status: DataTypes.STRING,
    isActive: DataTypes.BOOLEAN
  }, {
    sequelize,
    modelName: 'Register',
  });
  return Register;
};