<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
    /**
     * Run the migrations.
     */
    public function up(): void
    {
        Schema::table('exam_bookings', function (Blueprint $table) {
            // Add columns only if they don't exist
            if (!$this->hasColumn('exam_bookings', 'exam_id')) {
                $table->foreignId('exam_id')->nullable()->constrained('exams')->onDelete('cascade');
            }
            if (!$this->hasColumn('exam_bookings', 'user_id')) {
                $table->foreignId('user_id')->nullable()->constrained('users')->onDelete('cascade');
            }
            if (!$this->hasColumn('exam_bookings', 'exam_type')) {
                $table->string('exam_type')->nullable(); // online, in_person, etc.
            }
            if (!$this->hasColumn('exam_bookings', 'date')) {
                $table->date('date')->nullable();
            }
            if (!$this->hasColumn('exam_bookings', 'time')) {
                $table->time('time')->nullable();
            }
            if (!$this->hasColumn('exam_bookings', 'location')) {
                $table->string('location')->nullable();
            }
            if (!$this->hasColumn('exam_bookings', 'status')) {
                $table->enum('status', ['pending', 'confirmed', 'cancelled', 'completed'])->default('pending');
            }
            if (!$this->hasColumn('exam_bookings', 'notes')) {
                $table->text('notes')->nullable();
            }
            
            // Add indexes
            if (!$this->hasIndex('exam_bookings', 'exam_bookings_exam_id_index')) {
                $table->index('exam_id');
            }
            if (!$this->hasIndex('exam_bookings', 'exam_bookings_user_id_index')) {
                $table->index('user_id');
            }
            if (!$this->hasIndex('exam_bookings', 'exam_bookings_date_index')) {
                $table->index('date');
            }
        });
    }

    /**
     * Reverse the migrations.
     */
    public function down(): void
    {
        Schema::table('exam_bookings', function (Blueprint $table) {
            // Drop indexes if they exist
            if ($this->hasIndex('exam_bookings', 'exam_bookings_date_index')) {
                $table->dropIndex('exam_bookings_date_index');
            }
            if ($this->hasIndex('exam_bookings', 'exam_bookings_user_id_index')) {
                $table->dropIndex('exam_bookings_user_id_index');
            }
            if ($this->hasIndex('exam_bookings', 'exam_bookings_exam_id_index')) {
                $table->dropIndex('exam_bookings_exam_id_index');
            }
            
            // Drop columns if they exist
            if ($this->hasColumn('exam_bookings', 'notes')) {
                $table->dropColumn('notes');
            }
            if ($this->hasColumn('exam_bookings', 'status')) {
                $table->dropColumn('status');
            }
            if ($this->hasColumn('exam_bookings', 'location')) {
                $table->dropColumn('location');
            }
            if ($this->hasColumn('exam_bookings', 'time')) {
                $table->dropColumn('time');
            }
            if ($this->hasColumn('exam_bookings', 'date')) {
                $table->dropColumn('date');
            }
            if ($this->hasColumn('exam_bookings', 'exam_type')) {
                $table->dropColumn('exam_type');
            }
            if ($this->hasColumn('exam_bookings', 'user_id')) {
                $table->dropColumn('user_id');
            }
            if ($this->hasColumn('exam_bookings', 'exam_id')) {
                $table->dropColumn('exam_id');
            }
        });
    }
    
    /**
     * Check if a column exists on a table
     */
    private function hasColumn(string $table, string $column): bool
    {
        $databaseName = DB::getDatabaseName();
        
        $result = DB::select(
            "SELECT COUNT(*) as count 
             FROM information_schema.columns 
             WHERE table_schema = ? 
             AND table_name = ? 
             AND column_name = ?",
            [$databaseName, $table, $column]
        );
        
        return $result[0]->count > 0;
    }
    
    /**
     * Check if an index exists on a table
     */
    private function hasIndex(string $table, string $index): bool
    {
        $databaseName = DB::getDatabaseName();
        
        $result = DB::select(
            "SELECT COUNT(*) as count 
             FROM information_schema.statistics 
             WHERE table_schema = ? 
             AND table_name = ? 
             AND index_name = ?",
            [$databaseName, $table, $index]
        );
        
        return $result[0]->count > 0;
    }
};
