<?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('subscription_changes', function (Blueprint $table) {
            $table->id();
            $table->foreignId('subscription_id')->constrained()->onDelete('cascade');
            $table->foreignId('user_id')->constrained()->onDelete('cascade');
            $table->enum('change_type', ['plan_change', 'cancellation', 'pause', 'resume', 'migration', 'provider_change']);
            $table->string('change_description');
            $table->json('old_values')->nullable();
            $table->json('new_values')->nullable();
            $table->string('reason')->nullable();
            $table->timestamp('effective_at');
            $table->timestamp('processed_at')->nullable();
            $table->boolean('is_processed')->default(false);
            $table->json('metadata')->nullable();
            $table->timestamps();

            $table->index(['subscription_id', 'change_type']);
            $table->index(['user_id', 'effective_at']);
            $table->index(['is_processed', 'processed_at']);
        });
    }

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