<?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('trial_extensions', function (Blueprint $table) {
            $table->id();
            $table->foreignId('subscription_id')->constrained()->onDelete('cascade');
            $table->foreignId('user_id')->constrained()->onDelete('cascade');
            $table->integer('extension_days');
            $table->string('reason')->nullable();
            $table->enum('extension_type', ['manual', 'automatic', 'compensation']);
            $table->timestamp('original_trial_ends_at');
            $table->timestamp('new_trial_ends_at');
            $table->timestamp('granted_at');
            $table->foreignId('granted_by_admin_id')->nullable()->constrained('users')->onDelete('set null');
            $table->json('metadata')->nullable();
            $table->timestamps();

            $table->index(['subscription_id', 'extension_type']);
            $table->index(['user_id', 'granted_at']);
        });
    }

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