<?php

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

return new class extends Migration
{
    /**
     * Run the migrations.
     *
     * Creates the training_certificates table to store generated
     * certificates for completed training programs.
     *
     * @return void
     */
    public function up(): void
    {
        Schema::create('training_certificates', function (Blueprint $table) {
            $table->id();
            
            // Foreign Keys
            $table->foreignId('user_id')
                ->constrained('users')
                ->cascadeOnDelete();
            $table->foreignId('training_package_id')
                ->constrained('training_packages')
                ->cascadeOnDelete();
            $table->foreignId('registration_id')
                ->nullable()
                ->constrained('training_registrations')
                ->nullOnDelete();
            
            // Certificate Number (Unique Identifier)
            $table->string('certificate_number', 191)->unique();
            
            // Certificate Details
            $table->string('title', 191)->nullable();
            $table->text('description')->nullable();
            $table->text('skills_acquired')->nullable();
            
            // Issue Information
            $table->date('issue_date');
            $table->date('expiry_date')->nullable();
            $table->boolean('is_valid')->default(true);
            
            // Format and Design
            $table->enum('format_type', ['portrait', 'landscape'])->default('portrait');
            $table->string('template_used', 191)->default('default');
            
            // File Storage
            $table->string('certificate_path', 500)->nullable();
            $table->string('pdf_path', 500)->nullable();
            $table->string('thumbnail_path', 500)->nullable();
            
            // Signatures and Seals
            $table->string('trainer_signature_path', 500)->nullable();
            $table->string('trainer_name')->nullable();
            $table->string('trainer_title')->nullable();
            $table->string('organization_seal_path', 500)->nullable();
            
            // Organization Information
            $table->string('organization_name', 191)->nullable();
            $table->string('organization_logo_path', 500)->nullable();
            
            // Verification
            $table->string('verification_code', 191)->unique();
            $table->string('verification_url', 500)->nullable();
            $table->integer('verification_count')->default(0);
            $table->timestamp('last_verified_at')->nullable();
            
            // Generated By
            $table->foreignId('generated_by')->nullable()->constrained('users')->nullOnDelete();
            
            // Revocation Information
            $table->boolean('is_revoked')->default(false);
            $table->timestamp('revoked_at')->nullable();
            $table->foreignId('revoked_by')->nullable()->constrained('users')->nullOnDelete();
            $table->text('revocation_reason')->nullable();
            
            // Timestamps
            $table->timestamps();
            
            // Indexes
            $table->index('user_id');
            $table->index('training_package_id');
            $table->index('certificate_number');
            $table->index('verification_code');
            $table->index('issue_date');
            $table->index('is_valid');
        });
    }

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