<?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('abandoned_checkouts', function (Blueprint $table) {
            $table->id();
            $table->foreignId('user_id')->constrained()->onDelete('cascade');
            $table->foreignId('package_id')->constrained()->onDelete('cascade');
            $table->string('email'); // Denormalisiert für Performance
            $table->json('checkout_state')->nullable(); // Gespeicherter Checkout-Zustand
            $table->integer('last_step')->default(1); // Letzter erreichter Schritt
            $table->timestamp('abandoned_at');
            $table->timestamp('reminded_at')->nullable(); // Wann zuletzt erinnert
            $table->string('reminder_stage')->default('none'); // none, 1h, 24h, 1w
            $table->timestamp('expires_at')->nullable(); // Wann der Checkout verfällt
            $table->boolean('converted')->default(false); // Ob erfolgreich abgeschlossen
            $table->timestamps();

            $table->index(['email', 'reminder_stage']);
            $table->index(['abandoned_at']);
            $table->index(['reminded_at']);
        });
    }

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