<?php

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('academy_challenge_submissions', function (Blueprint $table): void {
            $table->id();
            $table->foreignId('challenge_id')->constrained('academy_challenges')->cascadeOnDelete();
            $table->foreignId('user_id')->constrained()->cascadeOnDelete();
            $table->foreignId('artwork_id')->nullable()->constrained('artworks')->nullOnDelete();
            $table->longText('prompt_used')->nullable();
            $table->longText('workflow_notes')->nullable();
            $table->string('ai_tool_used')->nullable();
            $table->boolean('is_ai_generated')->default(false);
            $table->boolean('is_ai_assisted')->default(true);
            $table->string('moderation_status')->default('pending');
            $table->timestamp('submitted_at')->nullable();
            $table->timestamps();
            $table->softDeletes();

            $table->unique(['challenge_id', 'user_id', 'artwork_id'], 'academy_challenge_user_artwork_unique');
            $table->index(['challenge_id', 'moderation_status'], 'academy_submissions_challenge_status_idx');
        });
    }

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