<?php

declare(strict_types=1);

function app_theme_store_file(): string
{
    return __DIR__ . '/app_theme_color.json';
}

function app_theme_store_default(): array
{
    return [
        'accent_color' => '#E53935',
        'border_color' => '#33FFFFFF',
        'button_color' => '#9E9E9E',
        'focus_color' => '#FFFFFF',
        'text_on_accent' => '#FFFFFF',
        'button_opacity' => 55,
        'button_pressed_opacity' => 70,
        'button_pill_opacity' => 50,
        'button_transparency_mode' => 'medio',
        'updated_at' => time(),
    ];
}

function app_theme_normalize_hex(?string $value, string $fallback): string
{
    $value = trim((string)$value);
    if ($value === '') return $fallback;
    if (str_starts_with(strtolower($value), '0x')) $value = '#' . substr($value, 2);
    if ($value[0] !== '#') $value = '#' . $value;
    $hex = substr($value, 1);
    if (preg_match('/^[0-9a-fA-F]{3}$/', $hex)) {
        $hex = $hex[0].$hex[0].$hex[1].$hex[1].$hex[2].$hex[2];
    }
    if (!preg_match('/^[0-9a-fA-F]{6}([0-9a-fA-F]{2})?$/', $hex)) return $fallback;
    return '#' . strtoupper($hex);
}

function app_theme_normalize_percent($value, int $fallback): int
{
    if (!is_numeric($value)) return $fallback;
    return max(0, min(100, (int)$value));
}

function app_theme_normalize_mode(?string $value, string $fallback = 'medio'): string
{
    $value = strtolower(trim((string)$value));
    return in_array($value, ['forte','medio','fraco'], true) ? $value : $fallback;
}

function app_theme_store_load(): array
{
    $default = app_theme_store_default();
    $file = app_theme_store_file();
    if (!is_file($file)) return $default;
    $raw = @file_get_contents($file);
    $json = is_string($raw) ? json_decode($raw, true) : null;
    if (!is_array($json)) return $default;
    return [
        'accent_color' => app_theme_normalize_hex($json['accent_color'] ?? '', $default['accent_color']),
        'border_color' => app_theme_normalize_hex($json['border_color'] ?? '', $default['border_color']),
        'button_color' => app_theme_normalize_hex($json['button_color'] ?? '', $default['button_color']),
        'focus_color' => app_theme_normalize_hex($json['focus_color'] ?? '', $default['focus_color']),
        'text_on_accent' => app_theme_normalize_hex($json['text_on_accent'] ?? '', $default['text_on_accent']),
        'button_opacity' => app_theme_normalize_percent($json['button_opacity'] ?? null, $default['button_opacity']),
        'button_pressed_opacity' => app_theme_normalize_percent($json['button_pressed_opacity'] ?? null, $default['button_pressed_opacity']),
        'button_pill_opacity' => app_theme_normalize_percent($json['button_pill_opacity'] ?? null, $default['button_pill_opacity']),
        'button_transparency_mode' => app_theme_normalize_mode($json['button_transparency_mode'] ?? '', $default['button_transparency_mode']),
        'updated_at' => (int)($json['updated_at'] ?? $default['updated_at']),
    ];
}

function app_theme_store_save(array $input): bool
{
    $default = app_theme_store_default();
    $payload = [
        'accent_color' => app_theme_normalize_hex($input['accent_color'] ?? '', $default['accent_color']),
        'border_color' => app_theme_normalize_hex($input['border_color'] ?? '', $default['border_color']),
        'button_color' => app_theme_normalize_hex($input['button_color'] ?? '', $default['button_color']),
        'focus_color' => app_theme_normalize_hex($input['focus_color'] ?? '', $default['focus_color']),
        'text_on_accent' => app_theme_normalize_hex($input['text_on_accent'] ?? '', $default['text_on_accent']),
        'button_opacity' => app_theme_normalize_percent($input['button_opacity'] ?? null, $default['button_opacity']),
        'button_pressed_opacity' => app_theme_normalize_percent($input['button_pressed_opacity'] ?? null, $default['button_pressed_opacity']),
        'button_pill_opacity' => app_theme_normalize_percent($input['button_pill_opacity'] ?? null, $default['button_pill_opacity']),
        'button_transparency_mode' => app_theme_normalize_mode($input['button_transparency_mode'] ?? '', $default['button_transparency_mode']),
        'updated_at' => time(),
    ];
    $json = json_encode($payload, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT);
    if (!is_string($json)) return false;
    $file = app_theme_store_file();
    $tmp = $file . '.tmp';
    if (@file_put_contents($tmp, $json, LOCK_EX) === false) return false;
    return @rename($tmp, $file);
}
