<?php

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

return new class extends Migration
{
    /**
     * Run the migrations.
     */
    public function up(): void
    {
        if (!Schema::hasTable('certificates')) {
            Schema::create('certificates', function (Blueprint $table) {
                $table->id();
                $table->foreignId('user_id')->constrained('users')->onDelete('cascade');
                $table->foreignId('exam_id')->nullable()->constrained('exams')->onDelete('set null');
                $table->foreignId('attempt_id')->nullable()->constrained('exam_attempts')->onDelete('set null');
                $table->string('title');
                $table->string('issuer')->default('ARMPO');
                $table->date('issued_date');
                $table->string('certificate_url')->nullable(); // URL to certificate image/PDF
                $table->string('verification_code')->unique(); // Unique code for verification
                $table->boolean('verified')->default(true);
                $table->string('badge')->nullable(); // Badge image URL
                $table->unsignedInteger('score')->nullable(); // Score achieved
                $table->string('pdf_path')->nullable(); // Path to PDF file
                $table->timestamps();
                
                $table->index(['user_id', 'exam_id']);
                $table->index('verification_code');
            });
        }
    }

    /**
     * Reverse the migrations.
     */
    public function down(): void
    {
        Schema::dropIfExists('certificates');
    }
};
