<?php
declare(strict_types=1);

namespace App\Models;

use App\Core\Model;
use Throwable;

final class SearchModel extends Model
{
    public function search(string $q): array
    {
        $q = trim($q);
        if ($q === '') {
            return [
                'users' => [],
                'conferences' => [],
                'submissions' => [],
                'papers' => [],
            ];
        }

        return [
            'users' => $this->searchUsers($q),
            'conferences' => $this->searchConferences($q),
            'submissions' => $this->searchSubmissions($q),
            'papers' => $this->searchPapers($q),
        ];
    }

    private function searchUsers(string $q): array
    {
        try {
            $stmt = $this->db->prepare("
                SELECT u.id, u.first_name, u.last_name, u.email, r.code AS role_code
                FROM users u
                LEFT JOIN roles r ON r.id = u.role_id
                WHERE u.first_name LIKE :q
                   OR u.last_name LIKE :q
                   OR u.email LIKE :q
                ORDER BY u.id DESC
                LIMIT 10
            ");
            $stmt->execute([':q' => '%' . $q . '%']);
            return $stmt->fetchAll();
        } catch (Throwable $e) {
            return [];
        }
    }

    private function searchConferences(string $q): array
    {
        try {
            $stmt = $this->db->prepare("
                SELECT id, slug, title_ar, title_en, short_name, status
                FROM conferences
                WHERE title_ar LIKE :q
                   OR title_en LIKE :q
                   OR short_name LIKE :q
                   OR slug LIKE :q
                ORDER BY id DESC
                LIMIT 10
            ");
            $stmt->execute([':q' => '%' . $q . '%']);
            return $stmt->fetchAll();
        } catch (Throwable $e) {
            return [];
        }
    }

    private function searchSubmissions(string $q): array
    {
        try {
            $stmt = $this->db->prepare("
                SELECT s.id, s.submission_no, s.title, s.status, c.title_ar, c.title_en
                FROM submissions s
                LEFT JOIN conferences c ON c.id = s.conference_id
                WHERE s.title LIKE :q
                   OR s.abstract LIKE :q
                   OR s.keywords LIKE :q
                   OR s.submission_no LIKE :q
                ORDER BY s.id DESC
                LIMIT 15
            ");
            $stmt->execute([':q' => '%' . $q . '%']);
            return $stmt->fetchAll();
        } catch (Throwable $e) {
            return [];
        }
    }

    private function searchPapers(string $q): array
    {
        try {
            $stmt = $this->db->prepare("
                SELECT p.paper_id, s.submission_no, s.title AS submission_title, p.doi, c.title_ar, c.title_en
                FROM proceedings p
                INNER JOIN submissions s ON s.id = p.paper_id
                INNER JOIN conferences c ON c.id = p.conference_id
                WHERE (p.is_published = 1 OR p.is_published IS NULL)
                  AND (
                    s.title LIKE :q
                    OR s.abstract LIKE :q
                    OR s.keywords LIKE :q
                    OR s.submission_no LIKE :q
                    OR p.doi LIKE :q
                  )
                ORDER BY p.id DESC
                LIMIT 15
            ");
            $stmt->execute([':q' => '%' . $q . '%']);
            return $stmt->fetchAll();
        } catch (Throwable $e) {
            return [];
        }
    }
}
