<?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('course_enrollments', function (Blueprint $table) {
            // Add columns only if they don't exist
            if (!$this->hasColumn('course_enrollments', 'last_accessed_at')) {
                $table->timestamp('last_accessed_at')->nullable();
            }
            if (!$this->hasColumn('course_enrollments', 'completed_at')) {
                $table->timestamp('completed_at')->nullable();
            }
            if (!$this->hasColumn('course_enrollments', 'payment_method_id')) {
                $table->unsignedInteger('payment_method_id')->nullable();
            }
            
            // Add index only if it doesn't exist
            if (!$this->hasIndex('course_enrollments', 'course_enrollments_user_id_status_index')) {
                $table->index(['user_id', 'status'], 'course_enrollments_user_id_status_index');
            }
        });
    }

    /**
     * Reverse the migrations.
     */
    public function down(): void
    {
        Schema::table('course_enrollments', function (Blueprint $table) {
            // Drop index if it exists
            if ($this->hasIndex('course_enrollments', 'course_enrollments_user_id_status_index')) {
                $table->dropIndex('course_enrollments_user_id_status_index');
            }
            
            // Drop columns if they exist
            if ($this->hasColumn('course_enrollments', 'payment_method_id')) {
                $table->dropColumn('payment_method_id');
            }
            if ($this->hasColumn('course_enrollments', 'completed_at')) {
                $table->dropColumn('completed_at');
            }
            if ($this->hasColumn('course_enrollments', 'last_accessed_at')) {
                $table->dropColumn('last_accessed_at');
            }
        });
    }
    
    /**
     * 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;
    }
};
