<?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_registrations table to track user enrollment
     * in training programs including status, payment, and completion.
     *
     * @return void
     */
    public function up(): void
    {
        Schema::create('training_registrations', function (Blueprint $table) {
            $table->id();
            
            // Foreign Keys
            $table->foreignId('user_id')
                ->constrained('users')
                ->cascadeOnDelete();
            $table->foreignId('training_package_id')
                ->constrained('training_packages')
                ->cascadeOnDelete();
            
            // Registration Information
            $table->timestamp('registration_date')->useCurrent();
            
            // Status Tracking
            $table->enum('status', ['pending', 'approved', 'rejected', 'waitlisted', 'cancelled', 'completed'])
                ->default('pending');
            $table->timestamp('status_updated_at')->nullable();
            
            // Payment Information
            $table->enum('payment_status', ['not_required', 'pending', 'processing', 'completed', 'failed', 'refunded'])
                ->default('not_required');
            $table->string('payment_reference', 191)->nullable();
            $table->string('payment_method')->nullable(); // mtn_momo, bank_transfer, cash
            $table->decimal('amount_paid', 10, 2)->default(0.00);
            $table->timestamp('payment_date')->nullable();
            
            // Approval/Rejection Information
            $table->foreignId('approved_by')->nullable()->constrained('users')->nullOnDelete();
            $table->foreignId('rejected_by')->nullable()->constrained('users')->nullOnDelete();
            $table->text('rejection_reason')->nullable();
            $table->timestamp('approved_at')->nullable();
            $table->timestamp('rejected_at')->nullable();
            
            // Registration Source
            $table->enum('registration_source', ['website', 'dashboard', 'admin', 'public'])
                ->default('dashboard');
            $table->foreignId('registered_by')->nullable()->constrained('users')->nullOnDelete();
            
            // Completion Tracking
            $table->timestamp('completion_date')->nullable();
            $table->decimal('attendance_percentage', 5, 2)->default(0.00);
            $table->decimal('score', 5, 2)->nullable();
            $table->boolean('passed')->nullable();
            
            // Certificate Information
            $table->boolean('certificate_issued')->default(false);
            $table->unsignedBigInteger('certificate_id')->nullable();
            
            // Additional Information
            $table->text('notes')->nullable();
            $table->json('metadata')->nullable();
            
            // Timestamps
            $table->timestamps();
            
            // Unique Constraint - One registration per user per package
            $table->unique(['user_id', 'training_package_id'], 'unique_user_package');
            
            // Indexes
            $table->index('user_id');
            $table->index('training_package_id');
            $table->index('status');
            $table->index('payment_status');
            $table->index('registration_date');
        });
    }

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