/**
 * Database Migration: Fix login_history foreign key constraint
 * Remove the foreign key constraint from login_history.user_id to user.id
 * since login_history can contain both user and admin logins
 */

module.exports = {
  up: async (queryInterface) => {
    // Drop the foreign key constraint
    try {
      await queryInterface.removeConstraint('login_history', 'login_history_ibfk_1');
    } catch (error) {
      // Constraint might not exist, continue
      console.log('Constraint not found, skipping removal');
    }
  },

  down: async (queryInterface, Sequelize) => {
    // Re-add the foreign key constraint if needed
    await queryInterface.addConstraint('login_history', {
      fields: ['user_id'],
      type: 'foreign key',
      name: 'login_history_ibfk_1',
      references: {
        table: 'user',
        field: 'id',
      },
      onUpdate: 'CASCADE',
      onDelete: 'CASCADE',
    });
  },
};
