<?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);
$tenantAvatarService = (new TenantServicesFactory())->createTenantAvatarService();

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

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

$tenantAvatarService->delete($uuid);
Flash::success('Avatar removed', "admin/tenants/edit/{$uuid}", 'tenant_avatar_removed');
Router::redirect("admin/tenants/edit/{$uuid}");
