<?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 mentorship_certificates table for certificate generation and tracking.
     * Supports A4 Portrait and Landscape orientations with modern design.
     */
    public function up(): void
    {
        Schema::create('mentorship_certificates', function (Blueprint $table) {
            $table->id();
            
            // Assignment relationship
            $table->foreignId('assignment_id')
                ->constrained('mentorship_assignments')
                ->cascadeOnDelete();
            
            // Programme reference
            $table->foreignId('programme_id')
                ->constrained('mentorship_programmes')
                ->cascadeOnDelete();
            
            // User relationship
            $table->foreignId('user_id')
                ->constrained('users')
                ->cascadeOnDelete()
                ->comment('The mentee who completed the programme');
            
            // Certificate identification
            $table->string('certificate_number', 191)->unique();
            $table->string('verification_code', 50)->unique();
            
            // Certificate content
            $table->string('recipient_name', 191);
            $table->string('programme_name', 191);
            $table->text('description')->nullable();
            $table->text('skills_acquired')->nullable()->comment('JSON array of skills');
            
            // Certificate format
            $table->enum('orientation', ['portrait', 'landscape'])->default('landscape');
            $table->string('template', 191)->default('default');
            $table->json('custom_fields')->nullable();
            
            // Organization details (snapshot at generation time)
            $table->string('organization_name', 191);
            $table->text('organization_address')->nullable();
            $table->string('organization_seal', 500)->nullable();
            $table->string('organization_logo', 500)->nullable();
            
            // Signatory information
            $table->string('signatory_name', 191)->nullable();
            $table->string('signatory_title', 191)->nullable();
            $table->string('signatory_signature', 500)->nullable();
            
            // Mentor information (for certificate)
            $table->string('mentor_name', 191)->nullable();
            $table->string('mentor_signature', 500)->nullable();
            
            // Dates
            $table->date('completion_date');
            $table->date('issue_date');
            $table->date('expiry_date')->nullable()->comment('For time-limited certifications');
            
            // File storage
            $table->string('file_path', 500)->nullable();
            $table->string('file_name', 191)->nullable();
            $table->string('file_type', 50)->default('pdf');
            $table->integer('file_size')->nullable()->comment('File size in bytes');
            
            // Download tracking
            $table->integer('download_count')->default(0);
            $table->timestamp('first_downloaded_at')->nullable();
            $table->timestamp('last_downloaded_at')->nullable();
            $table->json('download_history')->nullable();
            
            // Generation metadata
            $table->foreignId('generated_by')
                ->nullable()
                ->constrained('users')
                ->nullOnDelete();
            $table->timestamp('generated_at')->nullable();
            $table->string('generation_ip', 45)->nullable();
            
            // Status
            $table->enum('status', ['active', 'revoked', 'expired'])->default('active');
            $table->timestamp('revoked_at')->nullable();
            $table->text('revocation_reason')->nullable();
            $table->foreignId('revoked_by')
                ->nullable()
                ->constrained('users')
                ->nullOnDelete();
            
            // Verification tracking
            $table->integer('verification_count')->default(0);
            $table->timestamp('last_verified_at')->nullable();
            
            // Timestamps
            $table->timestamps();
            $table->softDeletes();
            
            // Indexes
            $table->index(['assignment_id', 'status']);
            $table->index(['user_id', 'status']);
            $table->index(['programme_id', 'status']);
            $table->index('certificate_number');
            $table->index('verification_code');
        });
    }

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