'use strict';

/** @type {import('sequelize-cli').Migration} */
module.exports = {
  up: async (queryInterface, Sequelize) => {
    // Menghapus constraint foreign key yang ada jika perlu
    try {
      await queryInterface.removeConstraint('employees', 'employees_department_id_fkey');
      console.log('Removed constraint: employees_department_id_fkey');
    } catch (error) {
      console.log('Constraint employees_department_id_fkey not found, skipping removal.');
    }

    // Menambahkan constraint foreign key yang baru untuk department_id
    await queryInterface.addConstraint('employees', {
      fields: ['department_id'],
      type: 'foreign key',
      name: 'employees_department_id_fkey',
      references: {
        table: 'departments',
        field: 'id'
      },
      onDelete: 'SET NULL',
      onUpdate: 'CASCADE'
    });
    console.log('Added constraint: employees_department_id_fkey');

    // Menghapus constraint foreign key yang ada jika perlu
    try {
      await queryInterface.removeConstraint('employees', 'employees_company_id_fkey');
      console.log('Removed constraint: employees_company_id_fkey');
    } catch (error) {
      console.log('Constraint employees_company_id_fkey not found, skipping removal.');
    }

    // Menambahkan constraint foreign key yang baru untuk company_id
    await queryInterface.addConstraint('employees', {
      fields: ['company_id'],
      type: 'foreign key',
      name: 'employees_company_id_fkey',
      references: {
        table: 'companies',
        field: 'id'
      },
      onDelete: 'SET NULL',
      onUpdate: 'CASCADE'
    });
    console.log('Added constraint: employees_company_id_fkey');

    // Menghapus constraint foreign key yang ada jika perlu
    try {
      await queryInterface.removeConstraint('employees', 'employees_designation_id_fkey');
      console.log('Removed constraint: employees_designation_id_fkey');
    } catch (error) {
      console.log('Constraint employees_designation_id_fkey not found, skipping removal.');
    }

    // Menambahkan constraint foreign key yang baru untuk designation_id
    await queryInterface.addConstraint('employees', {
      fields: ['designation_id'],
      type: 'foreign key',
      name: 'employees_designation_id_fkey',
      references: {
        table: 'designations',
        field: 'id'
      },
      onDelete: 'SET NULL',
      onUpdate: 'CASCADE'
    });
    console.log('Added constraint: employees_designation_id_fkey');
  },
  down: async (queryInterface, Sequelize) => {
    // Menghapus constraint foreign key yang baru ditambahkan
    await queryInterface.removeConstraint('employees', 'employees_department_id_fkey');
    console.log('Removed constraint: employees_department_id_fkey');

    await queryInterface.removeConstraint('employees', 'employees_company_id_fkey');
    console.log('Removed constraint: employees_company_id_fkey');

    await queryInterface.removeConstraint('employees', 'employees_designation_id_fkey');
    console.log('Removed constraint: employees_designation_id_fkey');

    // Jika perlu, tambahkan kembali constraint yang lama di sini
    // await queryInterface.addConstraint('employees', {
    //     fields: ['department_id'],
    //     type: 'foreign key',
    //     name: 'old_constraint_name', // Ganti dengan nama constraint lama
    //     references: {
    //         table: 'departments',
    //         field: 'id'
    //     },
    //     onDelete: 'CASCADE', // Atur sesuai kebutuhan
    //     onUpdate: 'CASCADE'
    // });
  }
};
