<?php

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

/**
 * Replaces the free-form theme columns with a preset-driven scheme.
 *
 * The old `theme_primary`/`theme_secondary`/`theme_background`/`theme_foreground`
 * hex columns could only ever describe four of the ~39 design tokens the app
 * uses, which is why applying a theme never reskinned the sidebar or login page.
 * Colour identity is now a single `theme_preset` key expanded server-side by
 * {@see ThemePresets}.
 *
 * The four legacy colour columns are intentionally LEFT IN PLACE and simply no
 * longer read, so this migration is non-destructive and reversible.
 */
return new class extends Migration
{
    /**
     * Old free-form radius values mapped onto the new named keys, so a tenant
     * who had chosen "Large" keeps a large radius instead of silently falling
     * back to the default.
     */
    private const RADIUS_MAP = [
        '0rem' => 'none',
        '0px' => 'none',
        '0' => 'none',
        '0.25rem' => 'xs',
        '0.3rem' => 'xs',
        '0.375rem' => 'sm',
        '0.5rem' => 'md',
        '0.75rem' => 'lg',
        '1rem' => 'xl',
    ];

    /** Legacy CSS font stacks mapped onto the new font catalogue keys. */
    private const FONT_MAP = [
        "'Inter', sans-serif" => 'inter',
        "'Roboto', sans-serif" => 'roboto',
        "'Playfair Display', serif" => 'playfair-display',
        "'Courier New', monospace" => 'ibm-plex-mono',
        'system-ui, sans-serif' => 'system',
    ];

    public function up(): void
    {
        Schema::table('tenant_settings', function (Blueprint $table) {
            // Null means "platform default" (ThemePresets::DEFAULT_KEY), which
            // reproduces the palette the app already ships — so existing tenants
            // see no visual change until a theme is explicitly chosen.
            $table->string('theme_preset', 64)->nullable()->after('theme_font_family');
            $table->string('theme_font_body', 64)->nullable()->after('theme_preset');
            // Null means "same family as the body font" — the common case.
            $table->string('theme_font_heading', 64)->nullable()->after('theme_font_body');
            $table->string('theme_scale', 32)->nullable()->after('theme_font_heading');
        });

        $this->migrateLegacyValues();
    }

    /**
     * Carry the two legacy settings that have a direct equivalent (radius and
     * font family) onto the new columns. The four hex colour columns have no
     * equivalent — a preset is a coordinated 39-token palette, and there is no
     * sound way to reverse-engineer one from a single brand hex.
     */
    private function migrateLegacyValues(): void
    {
        foreach (self::RADIUS_MAP as $legacy => $key) {
            DB::table('tenant_settings')
                ->where('theme_radius', $legacy)
                ->update(['theme_radius' => $key]);
        }

        foreach (self::FONT_MAP as $legacy => $key) {
            DB::table('tenant_settings')
                ->where('theme_font_family', $legacy)
                ->whereNull('theme_font_body')
                ->update(['theme_font_body' => $key]);
        }
    }

    public function down(): void
    {
        Schema::table('tenant_settings', function (Blueprint $table) {
            $table->dropColumn(['theme_preset', 'theme_font_body', 'theme_font_heading', 'theme_scale']);
        });
    }
};
