<?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;
}

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

Flash::success('Logo updated', 'admin/settings', 'logo_updated');
Router::redirect('admin/settings');
