<?php

use MintyPHP\Router;
use MintyPHP\Service\Tenant\TenantServicesFactory;
use MintyPHP\Support\Flash;
use MintyPHP\Support\Guard;
use MintyPHP\Service\Access\PermissionService;

Guard::requireLogin();
Guard::requirePermissionOrForbidden(PermissionService::TENANTS_UPDATE);
$tenantFaviconService = (new TenantServicesFactory())->createTenantFaviconService();

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

$uuid = trim((string) ($id ?? ''));
if (!$tenantFaviconService->isValidUuid($uuid)) {
    Flash::error('Tenant not found', 'admin/tenants', 'tenant_not_found');
    Router::redirect('admin/tenants');
    return;
}

$tenantFaviconService->delete($uuid);
Flash::success('Favicon removed', "admin/tenants/edit/{$uuid}", 'tenant_favicon_removed');
Router::redirect("admin/tenants/edit/{$uuid}");
