<?php

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

/**
 * Versioned theme storage. Every "Save" in the admin appearance editor inserts
 * a new row; only one row at a time carries `is_active = true`. The single-
 * active invariant is enforced at the application layer (a partial unique
 * index on `is_active` isn't portable across MySQL/SQLite).
 *
 * `knobs` is the small set of admin inputs (accent OKLCH triple, neutral_temp,
 * font, radius, density, elevation, button_shape, mode_pref). `derived` caches
 * the full token map computed by `App\Support\ThemePalette::derive` so admin
 * surfaces don't recompute on every page; it's lazily refilled on read when
 * empty. `mode_overrides` and `assets` are Sprint 1 stubs (Sprint 3 fills
 * them) — kept in the schema so we don't migrate again later.
 */
return new class extends Migration
{
    public function up(): void
    {
        Schema::create('theme_versions', function (Blueprint $table) {
            $table->id();
            $table->string('name', 191);
            $table->json('knobs');
            $table->json('derived')->nullable();
            $table->json('mode_overrides')->nullable();
            $table->json('assets')->nullable();
            $table->boolean('is_active')->default(false);
            $table->foreignId('created_by')->nullable()->constrained('users')->nullOnDelete();
            $table->timestamp('applied_at')->nullable();
            $table->timestamps();

            $table->index('is_active');
        });

        DB::table('theme_versions')->insert([
            'name' => 'Anthropic',
            'knobs' => json_encode($this->defaultKnobs()),
            'derived' => null,
            'mode_overrides' => json_encode(new \stdClass()),
            'assets' => json_encode(new \stdClass()),
            'is_active' => true,
            'created_by' => null,
            'applied_at' => now(),
            'created_at' => now(),
            'updated_at' => now(),
        ]);
    }

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

    /**
     * Initial knob defaults. Mirrors the literal in `Settings::defaultThemeKnobs()`
     * — the seed must round-trip identically through both paths so a deleted
     * row + cold start lands the app on exactly the same visual baseline as
     * the seeded row.
     */
    private function defaultKnobs(): array
    {
        return [
            'accent' => ['l' => 0.67, 'c' => 0.135, 'h' => 37],
            'neutral_temp' => 'warm',
            'font_sans' => 'Poppins',
            'font_display' => 'Lora',
            'font_subheading' => 'Lora',
            'radius' => 'rounded',
            'density' => 'comfortable',
            'elevation' => 'flat',
            'button_shape' => 'rounded',
            'mode_pref' => 'light',
            'weight_body' => 400,
            'weight_subheading' => 500,
            'weight_heading' => 600,
            'weight_display' => 600,
            'scale_h1' => ['size' => 56, 'line' => 1.05, 'tracking' => -0.025],
            'scale_h2' => ['size' => 40, 'line' => 1.1, 'tracking' => -0.02],
            'scale_h3' => ['size' => 26, 'line' => 1.2, 'tracking' => -0.005],
            'scale_body' => ['size' => 17, 'line' => 1.55],
        ];
    }
};
