<?php

use MintyPHP\Module\Security\SecurityAuthorizationPolicy;
use MintyPHP\Module\Security\Service\SecurityReportLogoService;
use MintyPHP\Support\Guard;

// Streams the report logo image directly — no view template for this route.
define('MINTY_ALLOW_OUTPUT', true);

Guard::requireLogin();
Guard::requireAbilityOrForbidden(SecurityAuthorizationPolicy::ABILITY_SETTINGS_MANAGE);

$logoService = app(SecurityReportLogoService::class);

$query = requestInput()->queryAll();
$size = isset($query['size']) ? (int) $query['size'] : null;

$path = $logoService->findLogoPath($size);
if (!$path || !is_file($path)) {
    http_response_code(404);

    return;
}

$mime = $logoService->detectMime($path);
header('Content-Type: ' . $mime);
header('X-Content-Type-Options: nosniff');
header('Content-Security-Policy: sandbox');
header('Cache-Control: private, max-age=300');
header('Content-Length: ' . filesize($path));
readfile($path);
