<?php

use App\Enums\ParticipantStatusEnum;
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
    {
        Schema::create('contest_participants', function (Blueprint $table) {
            $table->id();
            $table->foreignIdFor(\App\Models\Contest::class)->constrained()->cascadeOnDelete();
            $table->foreignIdFor(\App\Models\User::class)->constrained()->cascadeOnDelete();
            $table->foreignIdFor(\App\Models\ContestPrize::class, 'prize_id')
                ->nullable()
                ->constrained('contest_prizes')
                ->nullOnDelete(); // Nullable if no prize is awarded
            $table->integer('score')->default(0);
            $table->integer('time_taken')->default(0) // in seconds
                ->comment('Total time taken by the participant to complete the contest');
            $table->timestamp('submitted_at')->nullable();
            $table->enum('status', ParticipantStatusEnum::values())
                ->default(ParticipantStatusEnum::PENDING) // Assuming ParticipantStatusEnum is defined elsewhere
                ->comment('Status of the participant in the contest, e.g., pending, completed, etc.');
            $table->timestamps();
            $table->unique(['contest_id', 'user_id']); // One entry per user per contest

        });
    }

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