<?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('assessments', function (Blueprint $table) {
            $table->id();
            $table->string('slug')->unique();
            $table->string('title');
            $table->string('tagline')->nullable();
            $table->text('description')->nullable();
            $table->string('emoji', 16)->nullable();
            $table->unsignedTinyInteger('est_minutes')->default(3);
            $table->unsignedSmallInteger('cooldown_days')->default(30);
            $table->boolean('is_active')->default(true);
            $table->json('settings')->nullable();
            $table->timestamps();
        });

        Schema::create('assessment_questions', function (Blueprint $table) {
            $table->id();
            $table->foreignId('assessment_id')->constrained()->cascadeOnDelete();
            $table->string('section')->nullable();          // micro-section label shown between groups
            $table->string('section_feedback')->nullable(); // encouraging interstitial copy
            $table->text('prompt');
            $table->text('help')->nullable();
            $table->string('type')->default('choice');      // choice | emoji_scale | this_or_that
            $table->string('dimension')->nullable();        // e.g. spending, saving, debt, investing, protection
            $table->json('options');                        // [{key, label, emoji, points}]
            $table->unsignedSmallInteger('sort')->default(0);
            $table->boolean('is_active')->default(true);
            $table->timestamps();
        });

        Schema::create('assessment_bands', function (Blueprint $table) {
            $table->id();
            $table->foreignId('assessment_id')->constrained()->cascadeOnDelete();
            $table->string('dimension')->nullable();        // null = overall result band
            $table->unsignedTinyInteger('min_percent');
            $table->unsignedTinyInteger('max_percent');
            $table->string('label');                        // e.g. "Money Builder"
            $table->string('emoji', 16)->nullable();
            $table->string('color', 16)->nullable();
            $table->string('headline')->nullable();
            $table->text('message')->nullable();
            $table->json('advice')->nullable();             // ["tip one", "tip two", ...]
            $table->unsignedSmallInteger('sort')->default(0);
            $table->timestamps();
        });

        Schema::create('assessment_attempts', function (Blueprint $table) {
            $table->id();
            $table->uuid('uuid')->unique();
            $table->foreignId('assessment_id')->constrained()->cascadeOnDelete();
            $table->foreignId('user_id')->nullable()->constrained()->nullOnDelete();
            $table->foreignId('band_id')->nullable()->constrained('assessment_bands')->nullOnDelete();
            $table->string('guest_name')->nullable();
            $table->string('guest_email')->nullable()->index();
            $table->string('status')->default('in_progress'); // in_progress | completed
            $table->json('answers')->nullable();               // {question_id: option_key}
            $table->unsignedInteger('score')->nullable();
            $table->unsignedInteger('max_score')->nullable();
            $table->decimal('percent', 5, 2)->nullable();
            $table->json('dimension_percents')->nullable();    // {dimension: percent}
            $table->timestamp('started_at')->nullable();
            $table->timestamp('completed_at')->nullable();
            $table->timestamps();
        });
    }

    public function down(): void
    {
        Schema::dropIfExists('assessment_attempts');
        Schema::dropIfExists('assessment_bands');
        Schema::dropIfExists('assessment_questions');
        Schema::dropIfExists('assessments');
    }
};
