<?php

namespace App\Database\Migrations;

use CodeIgniter\Database\Migration;

class RevertPrintRequestsForeignKey extends Migration
{
    public function up()
    {
        // Drop the old foreign key if it exists
        $db = \Config\Database::connect();
        $keys = $db->getForeignKeyData('print_requests');
        foreach ($keys as $key) {
            if ($key->constraint_name === 'print_requests_class_id_foreign') {
                $this->forge->dropForeignKey('print_requests', 'print_requests_class_id_foreign');
                break;
            }
        }

        // Add the new foreign key
        $this->forge->addForeignKey('class_id', 'classes', 'id', 'CASCADE', 'CASCADE');
        
        // Process the modifications
        $this->forge->processIndexes('print_requests');
    }

    public function down()
    {
        // Drop the new foreign key
        $this->forge->dropForeignKey('print_requests', 'print_requests_class_id_foreign');

        // Re-add the old foreign key
        $this->forge->addForeignKey('class_id', 'class_sections', 'id', 'CASCADE', 'CASCADE');

        // Process the modifications
        $this->forge->processIndexes('print_requests');
    }
}
