const { Model, DataTypes } = require('sequelize');

/**
 * HolidayState Model
 * Junction Table für Many-to-Many Beziehung zwischen Holiday und State
 */
class HolidayState extends Model {
  static initialize(sequelize) {
    HolidayState.init(
      {
        holiday_id: {
          type: DataTypes.BIGINT,
          primaryKey: true,
          allowNull: false,
          references: {
            model: 'holiday',
            key: 'id'
          }
        },
        state_id: {
          type: DataTypes.BIGINT,
          primaryKey: true,
          allowNull: false,
          references: {
            model: 'state',
            key: 'id'
          }
        }
      },
      {
        sequelize,
        tableName: 'holiday_state',
        timestamps: false,
        indexes: [
          {
            name: 'fk_holiday_state_holiday',
            fields: ['holiday_id']
          },
          {
            name: 'fk_holiday_state_state',
            fields: ['state_id']
          }
        ]
      }
    );

    return HolidayState;
  }

  /**
   * Definiert Assoziationen mit anderen Models
   */
  static associate(models) {
    // HolidayState gehört zu einem Holiday
    HolidayState.belongsTo(models.Holiday, {
      foreignKey: 'holiday_id',
      as: 'holiday'
    });

    // HolidayState gehört zu einem State
    HolidayState.belongsTo(models.State, {
      foreignKey: 'state_id',
      as: 'state'
    });
  }
}

module.exports = HolidayState;

