<?php

declare(strict_types=1);

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

return new class extends Migration
{
    public function up(): void
    {
        Schema::create('group_challenge_outcomes', function (Blueprint $table): void {
            $table->id();
            $table->foreignId('group_challenge_id')->constrained('group_challenges')->cascadeOnDelete();
            $table->foreignId('artwork_id')->constrained('artworks')->cascadeOnDelete();
            $table->foreignId('user_id')->nullable()->constrained('users')->nullOnDelete();
            $table->string('outcome_type', 32);
            $table->unsignedInteger('position')->nullable();
            $table->unsignedInteger('sort_order')->default(0);
            $table->string('title_override', 120)->nullable();
            $table->text('note')->nullable();
            $table->foreignId('awarded_by_user_id')->nullable()->constrained('users')->nullOnDelete();
            $table->timestamp('awarded_at')->nullable();
            $table->timestamps();

            $table->unique(['group_challenge_id', 'artwork_id', 'outcome_type'], 'group_challenge_outcomes_unique');
            $table->index(['group_challenge_id', 'outcome_type', 'sort_order'], 'group_challenge_outcomes_type_sort_idx');
        });
    }

    public function down(): void
    {
        Schema::dropIfExists('group_challenge_outcomes');
    }
};