<?php

declare(strict_types=1);

namespace Maestro\Core\Support;

use Illuminate\Support\Facades\Storage;
use Maestro\Core\Models\MaestroAppSetting;

final class MaestroAppSettingsPresenter
{
    /**
     * Logo URLs when using shipped defaults are omitted (null): the SPA uses bundled assets.
     * Non-null values are overrides stored on the public disk.
     *
     * @return array{
     *   theme: array<string, mixed>|null,
     *   logos: array<string, string|null>,
     *   publicPage: array{enabled: bool, mode: string, rootMode: string},
     *   maestroCore: array{npm: string|null, composer: string|null}
     * }
     */
    public static function toArray(?MaestroAppSetting $row = null): array
    {
        $row ??= MaestroAppSetting::instance();

        return [
            'theme' => $row->theme,
            'logos' => [
                'sidebar' => self::storagePublicUrlOrNull($row->logo_sidebar_path),
                'header' => self::storagePublicUrlOrNull($row->logo_header_path),
                'compact' => self::storagePublicUrlOrNull($row->logo_compact_path),
                'favicon' => self::storagePublicUrlOrNull($row->favicon_path),
                'loginHero' => self::storagePublicUrlOrNull($row->login_hero_path),
            ],
            'offerDocumentBackgrounds' => self::offerDocumentBackgroundEntries($row),
            'publicPage' => self::publicPageSettings($row),
            'maestroCore' => MaestroCoreVersions::toArray(),
        ];
    }

    /**
     * @return array{enabled: bool, mode: string, rootMode: string}
     */
    private static function publicPageSettings(MaestroAppSetting $row): array
    {
        $settings = is_array($row->public_page) ? $row->public_page : [];

        $enabled = array_key_exists('enabled', $settings)
            ? filter_var($settings['enabled'], FILTER_VALIDATE_BOOLEAN, FILTER_NULL_ON_FAILURE)
            : false;
        $mode = isset($settings['mode']) && is_string($settings['mode'])
            ? $settings['mode']
            : 'core_default';

        if (! in_array($mode, ['core_default', 'customer_override'], true)) {
            $mode = 'core_default';
        }

        $rootMode = isset($settings['root_mode']) && is_string($settings['root_mode'])
            ? $settings['root_mode']
            : 'login';
        if (! in_array($rootMode, ['dashboard', 'login', 'public'], true)) {
            $rootMode = 'login';
        }

        return [
            'enabled' => $enabled ?? false,
            'mode' => $mode,
            'rootMode' => $rootMode,
        ];
    }

    private static function storagePublicUrlOrNull(?string $storageRelativePath): ?string
    {
        if ($storageRelativePath === null || $storageRelativePath === '') {
            return null;
        }

        return Storage::disk('public')->url($storageRelativePath);
    }

    /**
     * @return list<array{path: string, url: string}>
     */
    private static function offerDocumentBackgroundEntries(MaestroAppSetting $row): array
    {
        $paths = $row->offer_document_background_paths ?? [];
        if (! is_array($paths)) {
            return [];
        }

        $out = [];
        foreach ($paths as $path) {
            if (! is_string($path) || $path === '') {
                continue;
            }
            $out[] = [
                'path' => $path,
                'url' => Storage::disk('public')->url($path),
            ];
        }

        return $out;
    }
}
