<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration {
    /**
     * TrackingController::watch() artık izleme (session + bölüm) başına tek
     * satır tutup güncelliyor; bu index olmadan her ping tabloyu tarardı.
     */
    public function up(): void
    {
        if (! Schema::hasIndex('analytics_watch_events', 'analytics_watch_events_session_id_episode_id_index')) {
            Schema::table('analytics_watch_events', function (Blueprint $table) {
                $table->index(['session_id', 'episode_id']);
            });
        }
    }

    public function down(): void
    {
        Schema::table('analytics_watch_events', function (Blueprint $table) {
            $table->dropIndex('analytics_watch_events_session_id_episode_id_index');
        });
    }
};
