<?php

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

/**
 * Generic key/value site settings store. JSON value column carries scalars,
 * arrays, and objects with one schema. The admin Settings page reads/writes
 * these — moving previously-env-only values (signup-disabled flag,
 * KIT_AUTHOR_EMAILS, support email, etc.) into the DB so the script-buyer
 * can toggle without redeploying.
 */
return new class extends Migration
{
    public function up(): void
    {
        Schema::create('settings', function (Blueprint $table) {
            $table->string('key', 64)->primary();
            $table->json('value')->nullable();
            $table->timestamps();
        });

        $now = now();
        DB::table('settings')->insert([
            ['key' => 'site.name', 'value' => json_encode('AppShot AI'), 'created_at' => $now, 'updated_at' => $now],
            ['key' => 'site.support_email', 'value' => json_encode(null), 'created_at' => $now, 'updated_at' => $now],
            ['key' => 'site.terms_url', 'value' => json_encode(null), 'created_at' => $now, 'updated_at' => $now],
            ['key' => 'site.privacy_url', 'value' => json_encode(null), 'created_at' => $now, 'updated_at' => $now],
            ['key' => 'signup.disabled', 'value' => json_encode(false), 'created_at' => $now, 'updated_at' => $now],
            ['key' => 'kit_author_emails', 'value' => json_encode(''), 'created_at' => $now, 'updated_at' => $now],
        ]);
    }

    public function down(): void
    {
        Schema::dropIfExists('settings');
    }
};
