<?php

use MintyPHP\Http\SessionStoreInterface;
use MintyPHP\Module\Helpdesk\HelpdeskAuthorizationPolicy;
use MintyPHP\Module\Helpdesk\Service\DomainDetailService;
use MintyPHP\Module\Helpdesk\Service\DomainSecurityLevelService;
use MintyPHP\Router;
use MintyPHP\Support\Guard;

Guard::requireLogin();
Guard::requireAbilityOrForbidden(HelpdeskAuthorizationPolicy::ABILITY_ACCESS);
gridRequireGetRequest();

$request = requestInput();
$domainNo = trim((string) $request->query('domainNo', ''));
$customerNo = trim((string) $request->query('customerNo', ''));

if ($domainNo === '') {
    Router::json(['ok' => false, 'error' => 'Missing domain number']);

    return;
}

$session = app(SessionStoreInterface::class)->all();
$tenantId = (int) ($session['current_tenant']['id'] ?? 0);

$service = app(DomainDetailService::class);
$result = $service->loadDomainDetails($domainNo, $customerNo, $tenantId);

$securityLevelService = app(DomainSecurityLevelService::class);
$securityDetails = $tenantId > 0 ? $securityLevelService->getDetails($tenantId, $domainNo) : ['level' => 'normal', 'note' => ''];
$result['security_level'] = $securityDetails['level'];
$result['security_level_variant'] = DomainSecurityLevelService::getBadgeVariant($securityDetails['level']);
$result['security_level_note'] = $securityDetails['note'];

Router::json($result);
