<?php

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

/**
 * Adds the demo-tenant lifecycle columns to the core `business` table without
 * touching the core migration (same approach SaasSwitcher used for
 * module_entitlements). All columns are nullable — a NULL demo_expires_at means
 * "not a demo tenant", so real businesses are unaffected and the expiry guard
 * is a no-op for them.
 */
return new class extends Migration
{
    public function up(): void
    {
        Schema::table('business', function (Blueprint $table) {
            if (! Schema::hasColumn('business', 'demo_slug')) {
                $table->string('demo_slug', 191)->nullable()->after('id')->index();
            }
            if (! Schema::hasColumn('business', 'demo_created_at')) {
                $table->timestamp('demo_created_at')->nullable();
            }
            if (! Schema::hasColumn('business', 'demo_expires_at')) {
                $table->timestamp('demo_expires_at')->nullable()->index();
            }
            if (! Schema::hasColumn('business', 'demo_status')) {
                // null = not a demo · active · expired · purged
                $table->string('demo_status', 20)->nullable();
            }
        });
    }

    public function down(): void
    {
        Schema::table('business', function (Blueprint $table) {
            foreach (['demo_slug', 'demo_created_at', 'demo_expires_at', 'demo_status'] as $col) {
                if (Schema::hasColumn('business', $col)) {
                    $table->dropColumn($col);
                }
            }
        });
    }
};
