<?php

namespace MintyPHP\Support\Search;

class SearchItemMapperProvider
{
    public static function mapPreviewItem(string $key, array $data, string $query): ?array
    {
        $subtitle = '';

        if ($key === 'users') {
            $label = trim(($data['first_name'] ?? '') . ' ' . ($data['last_name'] ?? ''));
            $subtitle = trim((string) ($data['email'] ?? ''));
            if ($label === '' && $subtitle !== '') {
                $label = $subtitle;
                $subtitle = '';
            }
            $url = lurl('admin/users/edit/' . ($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 !== '') {
                $subtitle = 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;
            $subtitleParts = [];
            if ($status !== '') {
                $subtitleParts[] = strtoupper($status);
            }
            if ($toEmail !== '' && $subject !== '') {
                $subtitleParts[] = $toEmail;
            }
            $subtitle = implode(' — ', $subtitleParts);
            $url = lurl('admin/mail-log/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;
        }

        $item = ['label' => $label, 'url' => $url];
        if ($subtitle !== '') {
            $item['subtitle'] = $subtitle;
        }

        return $item;
    }

    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 === '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'] ?? ''));
        } 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),
        ];
    }


}
