<?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
    {
        // Sayfa görüntüleme logları
        Schema::create('analytics_pageviews', function (Blueprint $table) {
            $table->id();
            $table->foreignId('user_id')->nullable()->constrained()->nullOnDelete();
            $table->string('session_id', 64)->nullable();
            $table->string('url', 500)->nullable();
            $table->string('page_type', 30)->default('other'); // home, anime, player, search, ai, genre, other
            $table->foreignId('anime_id')->nullable()->constrained()->nullOnDelete();
            $table->foreignId('episode_id')->nullable()->constrained()->nullOnDelete();
            $table->string('ip', 45)->nullable();
            $table->string('country', 100)->nullable();
            $table->string('city', 100)->nullable();
            $table->string('device', 20)->default('unknown'); // mobile, desktop, tablet
            $table->string('browser', 50)->nullable();
            $table->string('referrer', 500)->nullable();
            $table->timestamp('created_at')->useCurrent();

            $table->index(['created_at']);
            $table->index(['page_type', 'created_at']);
            $table->index(['anime_id', 'created_at']);
            $table->index(['user_id', 'created_at']);
            $table->index(['ip']);
        });

        // Video izleme olayları
        Schema::create('analytics_watch_events', function (Blueprint $table) {
            $table->id();
            $table->foreignId('user_id')->nullable()->constrained()->nullOnDelete();
            $table->string('session_id', 64)->nullable();
            $table->foreignId('anime_id')->nullable()->constrained()->nullOnDelete();
            $table->foreignId('episode_id')->nullable()->constrained()->nullOnDelete();
            $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('created_at')->useCurrent();

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

        // AI sorgu logları
        Schema::create('analytics_ai_queries', function (Blueprint $table) {
            $table->id();
            $table->foreignId('user_id')->nullable()->constrained()->nullOnDelete();
            $table->string('query_type', 30); // chat, recommend, search, episode_info, similar
            $table->text('query_text')->nullable();
            $table->timestamp('created_at')->useCurrent();

            $table->index(['query_type', 'created_at']);
            $table->index(['user_id', 'created_at']);
            $table->index(['created_at']);
        });
    }

    public function down(): void
    {
        Schema::dropIfExists('analytics_ai_queries');
        Schema::dropIfExists('analytics_watch_events');
        Schema::dropIfExists('analytics_pageviews');
    }
};
