<?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
    {
        // 1) NicoNico tarzı timestamp yorumları
        Schema::create('episode_timestamp_comments', function (Blueprint $table) {
            $table->id();
            $table->foreignId('episode_id')->constrained()->cascadeOnDelete();
            $table->foreignId('user_id')->nullable()->constrained()->nullOnDelete();
            $table->unsignedSmallInteger('timestamp_sec');   // videonun kaçıncı saniyesi
            $table->string('body', 100);                     // max 100 karakter, kısa kalasın
            $table->string('color', 7)->default('#ffffff');  // hex renk
            $table->boolean('is_hidden')->default(false);    // mod silme
            $table->timestamp('created_at')->useCurrent();

            $table->index(['episode_id', 'timestamp_sec']);
            $table->index(['user_id']);
        });

        // 2) Tahmin oyunu — bölüm öncesi tahminler
        Schema::create('episode_predictions', function (Blueprint $table) {
            $table->id();
            $table->foreignId('episode_id')->constrained()->cascadeOnDelete();
            $table->foreignId('user_id')->constrained()->cascadeOnDelete();
            $table->string('body', 280);                     // tahmin metni
            $table->boolean('is_correct')->nullable();       // null=beklemede, true/false=sonuç
            $table->unsignedSmallInteger('vote_count')->default(0);
            $table->timestamps();

            $table->unique(['episode_id', 'user_id']);       // bölüm başına 1 tahmin
            $table->index(['episode_id', 'vote_count']);
        });

        // 3) Tahmin oyları
        Schema::create('prediction_votes', function (Blueprint $table) {
            $table->id();
            $table->foreignId('prediction_id')->constrained('episode_predictions')->cascadeOnDelete();
            $table->foreignId('user_id')->constrained()->cascadeOnDelete();
            $table->timestamp('created_at')->useCurrent();

            $table->unique(['prediction_id', 'user_id']);
        });

        // 4) Watch Party odaları
        Schema::create('watch_parties', function (Blueprint $table) {
            $table->id();
            $table->string('room_code', 10)->unique();
            $table->foreignId('host_user_id')->constrained('users')->cascadeOnDelete();
            $table->foreignId('episode_id')->constrained()->cascadeOnDelete();
            $table->unsignedInteger('current_sec')->default(0);  // senkron zaman
            $table->boolean('is_playing')->default(false);
            $table->timestamp('synced_at')->useCurrent()->useCurrentOnUpdate();
            $table->unsignedTinyInteger('max_members')->default(10);
            $table->boolean('is_private')->default(false);
            $table->string('password', 60)->nullable();
            $table->timestamps();

            $table->index(['room_code']);
            $table->index(['episode_id']);
        });

        // 5) Watch Party üyeleri
        Schema::create('watch_party_members', function (Blueprint $table) {
            $table->id();
            $table->foreignId('party_id')->constrained('watch_parties')->cascadeOnDelete();
            $table->foreignId('user_id')->constrained()->cascadeOnDelete();
            $table->timestamp('joined_at')->useCurrent();
            $table->timestamp('last_ping')->useCurrent()->useCurrentOnUpdate();

            $table->unique(['party_id', 'user_id']);
            $table->index(['party_id', 'last_ping']);
        });

        // 6) İlk kez izleyenler — aktif oturum kaydı (60dk TTL, cron temizler)
        Schema::create('first_watch_sessions', function (Blueprint $table) {
            $table->id();
            $table->foreignId('episode_id')->constrained()->cascadeOnDelete();
            $table->foreignId('user_id')->nullable()->constrained()->nullOnDelete();
            $table->string('session_id', 64)->nullable();    // misafir desteği
            $table->boolean('is_first_time')->default(true); // kullanıcı "ilk kez" dedi mi
            $table->timestamp('created_at')->useCurrent();
            $table->timestamp('last_seen')->useCurrent()->useCurrentOnUpdate();

            $table->index(['episode_id', 'last_seen']);
            $table->index(['user_id']);
        });
    }

    public function down(): void
    {
        Schema::dropIfExists('first_watch_sessions');
        Schema::dropIfExists('watch_party_members');
        Schema::dropIfExists('watch_parties');
        Schema::dropIfExists('prediction_votes');
        Schema::dropIfExists('episode_predictions');
        Schema::dropIfExists('episode_timestamp_comments');
    }
};
