<?php

namespace App\Http\Controllers;

use App\Support\SystemSettingsHelper;
use App\Models\User;
use App\Enums\UserTypes;
use Hypervel\Support\Facades\Auth;
use Hypervel\Support\Facades\Response;
use Psr\Http\Message\ResponseInterface;

class PwaManifestController
{
    /**
     * Serve the dynamic manifest.json
     */
    public function manifest(): ResponseInterface
    {
        $user = Auth::user();
        $isUltimate = $user && ($user->acct_type === UserTypes::ULTIMATE || $user->acct_type === UserTypes::ULTIMATE->value);
        
        $appName = SystemSettingsHelper::appName();
        $appDescription = SystemSettingsHelper::appDescription();
        
        // Default CDN icons — served from obj-vault-3a via cdn_asset()
        $icons = [
            ["sizes" => "48x48", "src" => cdn_asset('vendor/assets/icons/48x48.png'), "type" => "image/png"],
            ["sizes" => "72x72", "src" => cdn_asset('vendor/assets/icons/72x72.png'), "type" => "image/png"],
            ["sizes" => "96x96", "src" => cdn_asset('vendor/assets/icons/96x96.png'), "type" => "image/png"],
            ["sizes" => "144x144", "src" => cdn_asset('vendor/assets/icons/144x144.png'), "type" => "image/png"],
            ["sizes" => "192x192", "src" => cdn_asset('vendor/assets/icons/192x192.png'), "type" => "image/png"],
            ["sizes" => "512x512", "src" => cdn_asset('vendor/assets/icons/512x512.png'), "type" => "image/png"],
        ];

        // Use the custom branding icon uploaded via Ultimate Console (System Settings) for everyone.
        // Ultimate-tier users keep that override even when previewing the Ultimate Console persona.
        $customLogoHash = \App\Models\SystemSetting::getValue('app_logo');
        if ($customLogoHash && !$isUltimate) {
            $customLogoUrl = \App\Http\Controllers\FilesMainController::generateURLforFileListHash($customLogoHash);
            foreach ($icons as &$icon) {
                $icon['src'] = $customLogoUrl;
                $icon['type'] = 'image/png';
                $icon['purpose'] = 'any maskable';
            }
            unset($icon);
        } elseif ($isUltimate && $customLogoHash) {
            $customLogoUrl = \App\Http\Controllers\FilesMainController::generateURLforFileListHash($customLogoHash);
            foreach ($icons as &$icon) {
                $icon['src'] = $customLogoUrl;
            }
            unset($icon);
        }

        $manifest = [
            "name" => $isUltimate ? "Ultimate Console" : $appName,
            "short_name" => $isUltimate ? "Ultimate" : explode(' ', $appName)[0],
            "description" => $isUltimate ? "Premium Administrative Management" : $appDescription,
            "lang" => "en-US",
            "start_url" => "/",
            "display" => "standalone",
            "background_color" => $isUltimate ? "#020617" : SystemSettingsHelper::primaryColor(),
            "theme_color" => $isUltimate ? "#0d6efd" : SystemSettingsHelper::primaryColor(),
            "icons" => $icons
        ];

        return Response::json($manifest)
            ->withHeader('Content-Type', 'application/manifest+json');
    }
}
