<?php

namespace MintyPHP\Module\Helpdesk\Service;

use MintyPHP\Module\Helpdesk\Repository\DomainSecurityLevelRepository;

class DomainSecurityLevelService
{
    private const ALLOWED_LEVELS = ['niedrig', 'normal', 'hoch', 'kritisch'];

    private const BADGE_VARIANTS = [
        'niedrig' => 'info',
        'normal' => 'neutral',
        'hoch' => 'warning',
        'kritisch' => 'error',
    ];

    public function __construct(
        private readonly DomainSecurityLevelRepository $repository
    ) {
    }

    /**
     * @api Called from page actions and AJAX endpoints
     */
    public function getLevel(int $tenantId, string $domainNo): string
    {
        if ($domainNo === '') {
            return 'normal';
        }

        $row = $this->repository->findByTenantAndDomainNo($tenantId, $domainNo);

        if ($row === null) {
            return 'normal';
        }

        $level = (string) ($row['security_level'] ?? 'normal');

        return in_array($level, self::ALLOWED_LEVELS, true) ? $level : 'normal';
    }

    /**
     * @api Called from AJAX endpoints — returns level + note
     * @return array{level: string, note: string}
     */
    public function getDetails(int $tenantId, string $domainNo): array
    {
        if ($domainNo === '') {
            return ['level' => 'normal', 'note' => ''];
        }

        $row = $this->repository->findByTenantAndDomainNo($tenantId, $domainNo);

        if ($row === null) {
            return ['level' => 'normal', 'note' => ''];
        }

        $level = (string) ($row['security_level'] ?? 'normal');
        $note = (string) ($row['note'] ?? '');

        return [
            'level' => in_array($level, self::ALLOWED_LEVELS, true) ? $level : 'normal',
            'note' => $note,
        ];
    }

    /**
     * @api Called from page actions and AJAX endpoints
     */
    public function setLevel(int $tenantId, string $domainNo, string $level, ?int $userId, ?string $note = null): bool
    {
        if ($domainNo === '' || !in_array($level, self::ALLOWED_LEVELS, true)) {
            return false;
        }

        return $this->repository->upsert($tenantId, $domainNo, $level, $userId, $note);
    }

    /**
     * @api Called from domains-data endpoint for batch enrichment
     */
    public function getAllLevelsForDomains(int $tenantId, array $domainNos): array
    {
        if ($domainNos === []) {
            return [];
        }

        return $this->repository->listLevelsByDomainNos($tenantId, $domainNos);
    }

    /**
     * @api Called from domains-data endpoint for batch enrichment with note
     * @param list<string> $domainNos
     * @return array<string, array{level: string, note: string}>
     */
    public function getAllDetailsForDomains(int $tenantId, array $domainNos): array
    {
        if ($domainNos === []) {
            return [];
        }

        return $this->repository->listDetailsByDomainNos($tenantId, $domainNos);
    }

    /**
     * @api Called from views and AJAX responses for badge rendering
     */
    public static function getBadgeVariant(string $level): string
    {
        return self::BADGE_VARIANTS[$level] ?? 'neutral';
    }

    /**
     * @return list<string>
     */
    /**
     * @api Called from AJAX endpoint for validation
     */
    public static function getAllowedLevels(): array
    {
        return self::ALLOWED_LEVELS;
    }
}
