<?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
    {
        Schema::table('movies', function (Blueprint $table) {
            if (! Schema::hasColumn('movies', 'views_count')) {
                $table->unsignedBigInteger('views_count')->default(0)->after('vote_average');
            }
            $table->index(['is_active', 'created_at'], 'movies_active_imported_idx');
            $table->index(['is_active', 'release_date'], 'movies_active_released_idx');
            $table->index(['is_active', 'views_count'], 'movies_active_views_idx');
        });

        Schema::table('shows', function (Blueprint $table) {
            if (! Schema::hasColumn('shows', 'views_count')) {
                $table->unsignedBigInteger('views_count')->default(0)->after('vote_average');
            }
            $table->index(['is_active', 'created_at'], 'shows_active_imported_idx');
            $table->index(['is_active', 'first_air_date'], 'shows_active_released_idx');
            $table->index(['is_active', 'views_count'], 'shows_active_views_idx');
        });
    }

    public function down(): void
    {
        Schema::table('movies', function (Blueprint $table) {
            $table->dropIndex('movies_active_imported_idx');
            $table->dropIndex('movies_active_released_idx');
            $table->dropIndex('movies_active_views_idx');
            $table->dropColumn('views_count');
        });
        Schema::table('shows', function (Blueprint $table) {
            $table->dropIndex('shows_active_imported_idx');
            $table->dropIndex('shows_active_released_idx');
            $table->dropIndex('shows_active_views_idx');
            $table->dropColumn('views_count');
        });
    }
};
