<?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('tribunals', function (Blueprint $table) {
            $table->id();
            $table->foreignId('anime_id')->constrained()->cascadeOnDelete();
            $table->foreignId('episode_id')->nullable()->constrained()->nullOnDelete();
            $table->foreignId('created_by')->constrained('users')->cascadeOnDelete();
            $table->string('question', 280);
            $table->string('side_a', 100);
            $table->string('side_b', 100);
            $table->enum('status', ['open', 'closed'])->default('open');
            $table->string('verdict', 1)->nullable();
            $table->timestamp('closes_at')->nullable();
            $table->timestamps();
        });

        Schema::create('tribunal_votes', function (Blueprint $table) {
            $table->id();
            $table->foreignId('tribunal_id')->constrained()->cascadeOnDelete();
            $table->foreignId('user_id')->constrained()->cascadeOnDelete();
            $table->string('side', 1);
            $table->timestamp('created_at');
            $table->unique(['tribunal_id', 'user_id']);
        });

        Schema::create('tribunal_arguments', function (Blueprint $table) {
            $table->id();
            $table->foreignId('tribunal_id')->constrained()->cascadeOnDelete();
            $table->foreignId('user_id')->constrained()->cascadeOnDelete();
            $table->string('side', 1);
            $table->string('body', 500);
            $table->unsignedSmallInteger('vote_count')->default(0);
            $table->timestamps();
            $table->unique(['tribunal_id', 'user_id']);
        });

        Schema::create('tribunal_argument_votes', function (Blueprint $table) {
            $table->id();
            $table->foreignId('argument_id')->constrained('tribunal_arguments')->cascadeOnDelete();
            $table->foreignId('user_id')->constrained()->cascadeOnDelete();
            $table->timestamp('created_at');
            $table->unique(['argument_id', 'user_id']);
        });

        Schema::create('time_capsules', function (Blueprint $table) {
            $table->id();
            $table->foreignId('user_id')->constrained()->cascadeOnDelete();
            $table->foreignId('anime_id')->constrained()->cascadeOnDelete();
            $table->text('message');
            $table->timestamp('unlock_at');
            $table->timestamp('opened_at')->nullable();
            $table->timestamps();
        });

        Schema::create('spoiler_boxes', function (Blueprint $table) {
            $table->id();
            $table->foreignId('episode_id')->constrained()->cascadeOnDelete();
            $table->foreignId('user_id')->constrained()->cascadeOnDelete();
            $table->text('body');
            $table->boolean('is_spoiler')->default(false);
            $table->tinyInteger('spoiler_score')->default(0);
            $table->unsignedSmallInteger('likes')->default(0);
            $table->timestamps();
        });

        Schema::create('spoiler_box_likes', function (Blueprint $table) {
            $table->id();
            $table->foreignId('box_id')->constrained('spoiler_boxes')->cascadeOnDelete();
            $table->foreignId('user_id')->constrained()->cascadeOnDelete();
            $table->timestamp('created_at');
            $table->unique(['box_id', 'user_id']);
        });

        Schema::create('mood_logs', function (Blueprint $table) {
            $table->id();
            $table->foreignId('user_id')->nullable()->constrained()->nullOnDelete();
            $table->foreignId('episode_id')->constrained()->cascadeOnDelete();
            $table->string('mood', 20);
            $table->timestamp('created_at');
        });
    }

    public function down(): void
    {
        Schema::dropIfExists('mood_logs');
        Schema::dropIfExists('spoiler_box_likes');
        Schema::dropIfExists('spoiler_boxes');
        Schema::dropIfExists('time_capsules');
        Schema::dropIfExists('tribunal_argument_votes');
        Schema::dropIfExists('tribunal_arguments');
        Schema::dropIfExists('tribunal_votes');
        Schema::dropIfExists('tribunals');
    }
};
