<?php

use MintyPHP\Router;
use MintyPHP\Service\Branding\BrandingServicesFactory;
use MintyPHP\Support\Flash;
use MintyPHP\Support\Guard;
use MintyPHP\Service\Access\PermissionService;

Guard::requireLogin();
Guard::requirePermission(PermissionService::SETTINGS_UPDATE);

if (($_SERVER['REQUEST_METHOD'] ?? 'GET') !== 'POST') {
    Router::redirect('admin/settings');
    return;
}

$faviconService = (new BrandingServicesFactory())->createBrandingFaviconService();
$result = $faviconService->saveUpload($_FILES['favicon'] ?? []);
if (!($result['ok'] ?? false)) {
    $error = $result['error'] ?? t('Upload failed');
    Flash::error($error, 'admin/settings', 'favicon_upload_failed');
    Router::redirect('admin/settings');
    return;
}

Flash::success('Favicon updated', 'admin/settings', 'favicon_updated');
Router::redirect('admin/settings');
