<?php

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

return new class extends Migration
{
    /**
     * Run the migrations.
     */
    public function up(): void
    {
        Schema::create('operators', function (Blueprint $table) {
            $table->id();
            $table->string('name');
            $table->string('slug')->unique();
            $table->string('website')->nullable();
            $table->string('headquarters')->nullable();
            $table->text('description')->nullable();
            $table->integer('total_parks')->default(0);
            $table->integer('total_rides')->default(0);
            $table->timestamps();
        });

        Schema::create('manufacturers', function (Blueprint $table) {
            $table->id();
            $table->string('name');
            $table->string('slug')->unique();
            $table->string('website')->nullable();
            $table->string('headquarters')->nullable();
            $table->text('description')->default('');
            $table->unsignedInteger('total_rides')->default(0);
            $table->unsignedInteger('total_roller_coasters')->default(0);
            $table->boolean('is_active')->default(true);
            $table->timestamps();
            $table->softDeletes();

            // Indexes
            $table->index('is_active');
            $table->index('total_rides');
            $table->index(['name', 'is_active']);
        });

        // Create slug history table for tracking slug changes
        Schema::create('slug_histories', function (Blueprint $table) {
            $table->id();
            $table->morphs('sluggable'); // Creates sluggable_type and sluggable_id
            $table->string('slug');
            $table->timestamps();

            $table->index(['sluggable_type', 'slug']);
        });
    }

    /**
     * Reverse the migrations.
     */
    public function down(): void
    {
        Schema::dropIfExists('slug_histories');
        Schema::dropIfExists('manufacturers');
        Schema::dropIfExists('operators');
    }
};