<?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('subscriptions', function (Blueprint $table) {
            $table->uuid('id')->primary();

            // USER (nicht löschen!)
            $table->foreignUuid('user_id')->nullable()->constrained()->nullOnDelete();

            // PLAN (optional relation)
            $table->foreignUuid('plan_id')->nullable();

            // SNAPSHOT (KRITISCH!)
            $table->string('plan_name');
            $table->string('plan_slug');

            $table->integer('price'); // final price (z. B. 16500)
            $table->integer('original_price')->nullable(); // z. B. 18000
            $table->integer('discount_amount')->nullable(); // z. B. 1500

            $table->string('interval'); // monthly, yearly

            // STATUS
            $table->string('status'); // active, canceled, expired

            // ZEIT
            $table->timestamp('starts_at')->nullable();
            $table->timestamp('ends_at')->nullable();

            // PROVIDER
            $table->string('provider'); // mollie, stripe
            $table->string('provider_customer_id')->nullable();
            $table->string('provider_subscription_id')->nullable();

            $table->uuid('gifted_by')->nullable();
            $table->timestamp('gifted_at')->nullable();
            $table->string('gift_reason')->nullable();

            $table->foreign('gifted_by')->references('id')->on('users')->nullOnDelete();

            $table->string('provider')->nullable()->change();
            $table->timestamps();
        });
    }

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