<?php

namespace MintyPHP\Support\Search;

use MintyPHP\Service\User\UserAvatarService;

class SearchItemMapperProvider
{
    /** @var (callable(): UserAvatarService)|null */
    private static $userAvatarServiceResolver = null;

    public static function configure(callable $userAvatarServiceResolver): void
    {
        self::$userAvatarServiceResolver = $userAvatarServiceResolver;
    }

    public static function mapPreviewItem(string $key, array $data, string $query): ?array
    {
        if ($key === 'users') {
            $label = trim(($data['first_name'] ?? '') . ' ' . ($data['last_name'] ?? ''));
            $email = trim((string) ($data['email'] ?? ''));
            if ($email !== '') {
                $label = $label === '' ? $email : ($label . ' — ' . $email);
            }
            $url = lurl('admin/users/edit/' . ($data['uuid'] ?? '')) . '?search=' . urlencode($query);
        } elseif ($key === 'address-book') {
            $label = trim(($data['first_name'] ?? '') . ' ' . ($data['last_name'] ?? ''));
            $email = trim((string) ($data['email'] ?? ''));
            if ($email !== '') {
                $label = $label === '' ? $email : ($label . ' — ' . $email);
            }
            $url = lurl('address-book/view/' . ($data['uuid'] ?? '')) . '?search=' . urlencode($query);
        } elseif ($key === 'permissions') {
            $label = (string) ($data['description'] ?? '');
            $url = lurl('admin/permissions/edit/' . ($data['id'] ?? '')) . '?search=' . urlencode($query);
        } elseif ($key === 'scheduled-jobs') {
            $jobLabel = trim((string) ($data['label'] ?? ''));
            $jobKey = trim((string) ($data['job_key'] ?? ''));
            $status = trim((string) ($data['last_run_status'] ?? ''));
            $label = $jobLabel !== '' ? $jobLabel : $jobKey;
            if ($status !== '') {
                $label = trim($label . ' — ' . strtoupper($status));
            }
            $url = lurl('admin/scheduled-jobs/edit/' . ($data['id'] ?? '')) . '?search=' . urlencode($query);
        } elseif ($key === 'mail-log') {
            $toEmail = trim((string) ($data['to_email'] ?? ''));
            $subject = trim((string) ($data['subject'] ?? ''));
            $status = trim((string) ($data['status'] ?? ''));
            $label = $subject !== '' ? $subject : $toEmail;
            if ($status !== '') {
                $label = trim($label . ' — ' . strtoupper($status));
            }
            $url = lurl('admin/mail-log/view/' . ($data['id'] ?? '')) . '?search=' . urlencode($query);
        } elseif ($key === 'api-audit') {
            $method = strtoupper(trim((string) ($data['method'] ?? '')));
            $path = trim((string) ($data['path'] ?? ''));
            $statusCode = (int) ($data['status_code'] ?? 0);
            $requestId = trim((string) ($data['request_id'] ?? ''));
            $label = trim($method . ' ' . $path);
            if ($statusCode > 0) {
                $label = trim($label . ' — ' . $statusCode);
            }
            if ($label === '' && $requestId !== '') {
                $label = $requestId;
            }
            $url = lurl('admin/api-audit/view/' . ($data['id'] ?? '')) . '?search=' . urlencode($query);
        } elseif ($key === 'system-audit') {
            $eventType = trim((string) ($data['event_type'] ?? ''));
            $outcome = strtolower(trim((string) ($data['outcome'] ?? '')));
            $requestId = trim((string) ($data['request_id'] ?? ''));
            $label = $eventType !== '' ? $eventType : $requestId;
            if ($outcome !== '') {
                $label = trim($label . ' — ' . $outcome);
            }
            $url = lurl('admin/system-audit/view/' . ($data['id'] ?? '')) . '?search=' . urlencode($query);
        } elseif ($key === 'pages') {
            $label = (string) ($data['slug'] ?? '');
            $url = lurl('page/' . $label) . '?search=' . urlencode($query);
        } else {
            $label = (string) ($data['description'] ?? '');
            $url = lurl('admin/' . $key . '/edit/' . ($data['uuid'] ?? '')) . '?search=' . urlencode($query);
        }

        if ($label === '') {
            return null;
        }

        return ['label' => $label, 'url' => $url];
    }

    public static function mapResultItem(string $key, string $label, array $data): ?array
    {
        $title = '';
        $description = '';
        $url = '';
        $image = '';

        if ($key === 'users') {
            $title = trim(($data['first_name'] ?? '') . ' ' . ($data['last_name'] ?? ''));
            $description = (string) ($data['email'] ?? '');
            $url = lurl('admin/users/edit/' . ($data['uuid'] ?? ''));
        } elseif ($key === 'address-book') {
            $title = trim(($data['first_name'] ?? '') . ' ' . ($data['last_name'] ?? ''));
            $description = (string) ($data['email'] ?? '');
            $uuid = (string) ($data['uuid'] ?? '');
            $url = lurl('address-book/view/' . $uuid);
            $userAvatarService = self::userAvatarService();
            if ($uuid !== '' && $userAvatarService->hasAvatar($uuid)) {
                $image = lurl('admin/users/avatar-file') . '?uuid=' . urlencode($uuid) . '&size=64';
            }
        } elseif ($key === 'permissions') {
            $title = (string) ($data['key'] ?? '');
            $description = (string) ($data['description'] ?? '');
            $url = lurl('admin/permissions/edit/' . ($data['id'] ?? ''));
        } elseif ($key === 'scheduled-jobs') {
            $jobLabel = trim((string) ($data['label'] ?? ''));
            $jobKey = trim((string) ($data['job_key'] ?? ''));
            $title = $jobLabel !== '' ? $jobLabel : $jobKey;
            $status = trim((string) ($data['last_run_status'] ?? ''));
            $description = $status !== '' ? strtoupper($status) : ($jobKey !== '' ? $jobKey : '');
            $url = lurl('admin/scheduled-jobs/edit/' . ($data['id'] ?? ''));
        } elseif ($key === 'pages') {
            $title = (string) ($data['slug'] ?? '');
            $description = t('Page');
            $url = lurl('page/' . $title);
        } elseif ($key === 'mail-log') {
            $title = trim((string) ($data['subject'] ?? ''));
            if ($title === '') {
                $title = trim((string) ($data['to_email'] ?? ''));
            }
            $status = trim((string) ($data['status'] ?? ''));
            $toEmail = trim((string) ($data['to_email'] ?? ''));
            $description = trim(($status !== '' ? strtoupper($status) : '') . ($toEmail !== '' ? ' — ' . $toEmail : ''));
            $url = lurl('admin/mail-log/view/' . ($data['id'] ?? ''));
        } elseif ($key === 'api-audit') {
            $method = strtoupper(trim((string) ($data['method'] ?? '')));
            $path = trim((string) ($data['path'] ?? ''));
            $statusCode = (int) ($data['status_code'] ?? 0);
            $requestId = trim((string) ($data['request_id'] ?? ''));
            $title = trim($method . ' ' . $path);
            if ($title === '') {
                $title = $requestId;
            }
            $description = trim(($statusCode > 0 ? (string) $statusCode : '') . ($requestId !== '' ? ' — ' . $requestId : ''));
            $url = lurl('admin/api-audit/view/' . ($data['id'] ?? ''));
        } elseif ($key === 'system-audit') {
            $eventType = trim((string) ($data['event_type'] ?? ''));
            $outcome = strtolower(trim((string) ($data['outcome'] ?? '')));
            $requestId = trim((string) ($data['request_id'] ?? ''));
            $title = $eventType !== '' ? $eventType : $requestId;
            $description = trim(($outcome !== '' ? strtoupper($outcome) : '') . ($requestId !== '' ? ' — ' . $requestId : ''));
            $url = lurl('admin/system-audit/view/' . ($data['id'] ?? ''));
        } else {
            $title = (string) ($data['description'] ?? '');
            $description = $label;
            $url = lurl('admin/' . $key . '/edit/' . ($data['uuid'] ?? ''));
        }

        if ($title === '' || $url === '') {
            return null;
        }

        return [
            'type' => $label,
            'title' => $title,
            'description' => $description,
            'url' => $url,
            'image' => $image,
            'icon' => SearchUiMetaProvider::iconForKey($key),
        ];
    }

    private static function userAvatarService(): UserAvatarService
    {
        if (!is_callable(self::$userAvatarServiceResolver)) {
            throw new \RuntimeException('SearchItemMapperProvider is not configured for dependency: ' . UserAvatarService::class);
        }

        $service = (self::$userAvatarServiceResolver)();
        if (!$service instanceof UserAvatarService) {
            throw new \RuntimeException('SearchItemMapperProvider resolver returned invalid dependency: ' . UserAvatarService::class);
        }

        return $service;
    }

}
