<?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
    {
        // Kaldığın yerden devam et
        Schema::create('continue_watching', function (Blueprint $table) {
            $table->id();
            $table->foreignId('user_id')->constrained()->cascadeOnDelete();
            $table->foreignId('anime_id')->constrained()->cascadeOnDelete();
            $table->foreignId('episode_id')->constrained()->cascadeOnDelete();
            $table->unsignedSmallInteger('season_number')->default(1);
            $table->unsignedSmallInteger('episode_number')->default(1);
            $table->unsignedInteger('seconds_watched')->default(0);
            $table->unsignedInteger('total_seconds')->default(0);
            $table->unsignedTinyInteger('percent_complete')->default(0);
            $table->timestamp('updated_at')->useCurrent()->useCurrentOnUpdate();

            $table->unique(['user_id', 'anime_id']);
            $table->index(['user_id', 'updated_at']);
        });

        // İzleme listesi
        Schema::create('watchlists', function (Blueprint $table) {
            $table->id();
            $table->foreignId('user_id')->constrained()->cascadeOnDelete();
            $table->foreignId('anime_id')->constrained()->cascadeOnDelete();
            $table->string('status', 20)->default('plan'); // plan, watching, completed, dropped
            $table->timestamp('created_at')->useCurrent();

            $table->unique(['user_id', 'anime_id']);
            $table->index(['user_id', 'status']);
        });

        // Bölüm like/dislike
        Schema::create('episode_votes', function (Blueprint $table) {
            $table->id();
            $table->foreignId('user_id')->constrained()->cascadeOnDelete();
            $table->foreignId('episode_id')->constrained()->cascadeOnDelete();
            $table->tinyInteger('vote')->default(1); // 1=like, -1=dislike
            $table->timestamp('created_at')->useCurrent();

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

        // Anime puanlama (kullanıcı bazlı 1-10)
        Schema::create('anime_ratings', function (Blueprint $table) {
            $table->id();
            $table->foreignId('user_id')->constrained()->cascadeOnDelete();
            $table->foreignId('anime_id')->constrained()->cascadeOnDelete();
            $table->unsignedTinyInteger('rating'); // 1-10
            $table->timestamp('created_at')->useCurrent();
            $table->timestamp('updated_at')->useCurrent()->useCurrentOnUpdate();

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

        // Anime istekleri
        Schema::create('anime_requests', function (Blueprint $table) {
            $table->id();
            $table->foreignId('user_id')->nullable()->constrained()->nullOnDelete();
            $table->string('title', 200);
            $table->string('original_title', 200)->nullable();
            $table->text('note')->nullable();
            $table->string('status', 20)->default('pending'); // pending, approved, rejected, added
            $table->text('admin_note')->nullable();
            $table->unsignedInteger('vote_count')->default(1); // kaç kişi istedi
            $table->timestamps();

            $table->index(['status', 'vote_count']);
        });

        // Anime isteği oyları (aynı anime'yi birden fazla kişi isteyebilir)
        Schema::create('anime_request_votes', function (Blueprint $table) {
            $table->id();
            $table->foreignId('anime_request_id')->constrained()->cascadeOnDelete();
            $table->foreignId('user_id')->nullable()->constrained()->nullOnDelete();
            $table->string('ip', 45)->nullable();
            $table->timestamp('created_at')->useCurrent();

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

        // Başarım tanımları
        Schema::create('achievements', function (Blueprint $table) {
            $table->id();
            $table->string('key', 50)->unique();
            $table->string('title', 100);
            $table->string('description', 255);
            $table->string('icon', 50)->default('bi-trophy');
            $table->string('color', 20)->default('#f0883e');
            $table->string('condition_type', 30); // episodes_watched, hours_watched, watchlist_count, login_streak, request_approved
            $table->unsignedInteger('condition_value')->default(1);
            $table->timestamps();
        });

        // Kullanıcı başarımları
        Schema::create('user_achievements', function (Blueprint $table) {
            $table->id();
            $table->foreignId('user_id')->constrained()->cascadeOnDelete();
            $table->foreignId('achievement_id')->constrained()->cascadeOnDelete();
            $table->timestamp('earned_at')->useCurrent();

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

    public function down(): void
    {
        Schema::dropIfExists('user_achievements');
        Schema::dropIfExists('achievements');
        Schema::dropIfExists('anime_request_votes');
        Schema::dropIfExists('anime_requests');
        Schema::dropIfExists('anime_ratings');
        Schema::dropIfExists('episode_votes');
        Schema::dropIfExists('watchlists');
        Schema::dropIfExists('continue_watching');
    }
};
