<?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
    {
        // Anime takip
        Schema::create('anime_follows', function (Blueprint $table) {
            $table->id();
            $table->foreignId('user_id')->constrained()->cascadeOnDelete();
            $table->foreignId('anime_id')->constrained()->cascadeOnDelete();
            $table->timestamp('created_at')->useCurrent();
            $table->unique(['user_id', 'anime_id']);
            $table->index(['anime_id']);
        });

        // Kullanıcı bildirimleri
        Schema::create('user_notifications', function (Blueprint $table) {
            $table->id();
            $table->foreignId('user_id')->constrained()->cascadeOnDelete();
            $table->string('type', 50)->default('episode');
            $table->json('data');
            $table->timestamp('read_at')->nullable();
            $table->timestamp('created_at')->useCurrent();
            $table->index(['user_id', 'read_at']);
            $table->index(['user_id', 'created_at']);
        });

        // Bölüm notları
        Schema::create('episode_notes', function (Blueprint $table) {
            $table->id();
            $table->foreignId('user_id')->constrained()->cascadeOnDelete();
            $table->foreignId('episode_id')->constrained()->cascadeOnDelete();
            $table->foreignId('anime_id')->constrained()->cascadeOnDelete();
            $table->text('content');
            $table->unsignedInteger('timestamp_at')->nullable();
            $table->timestamps();
            $table->index(['user_id', 'episode_id']);
            $table->index(['user_id', 'anime_id']);
        });
    }

    public function down(): void
    {
        Schema::dropIfExists('episode_notes');
        Schema::dropIfExists('user_notifications');
        Schema::dropIfExists('anime_follows');
    }
};
