<?php

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

/**
 * The same provenance as subscriptions.granted_* (see that migration's
 * docblock), for a module booked without a payment — "ein Plugin schenken".
 * A booked module already prices independently of the plan it sits on, so
 * granting one has to be recorded independently too.
 */
return new class extends Migration
{
    public function up(): void
    {
        Schema::table('subscription_addons', function (Blueprint $table) {
            $table->foreignId('granted_by')->nullable()->after('cancelled_at')
                ->constrained('operators')->nullOnDelete();
            $table->timestamp('granted_at')->nullable()->after('granted_by');
            $table->text('grant_note')->nullable()->after('granted_at');
            $table->timestamp('granted_until')->nullable()->after('grant_note');
            $table->unsignedInteger('catalogue_price_cents')->nullable()->after('granted_until');
        });
    }

    public function down(): void
    {
        Schema::table('subscription_addons', function (Blueprint $table) {
            $table->dropConstrainedForeignId('granted_by');
            $table->dropColumn(['granted_at', 'grant_note', 'granted_until', 'catalogue_price_cents']);
        });
    }
};
