<?php

require_once __DIR__ . '/../library/auth.php';

allow_roles(['admin', 'teacher', 'accountant']);

header('Content-Type: application/json');

$school_id = $_SESSION['school_id'];

$search = trim($_GET['q'] ?? '');

$response = [];

if (strlen($search) < 2) {

    echo json_encode($response);
    exit;
}

try {

    /*
    |--------------------------------------------------------------------------
    | STUDENT SEARCH
    |--------------------------------------------------------------------------
    */

    $student_stmt = $pdo->prepare("
        SELECT

            s.id,
            s.name,
            s.phone,
            s.admission_no,

            ss.roll_no,

            c.class_name

        FROM students s

        LEFT JOIN student_sessions ss
        ON ss.student_id = s.id

        LEFT JOIN classes c
        ON c.id = ss.class_id

        WHERE s.school_id = ?

        AND (

            s.name LIKE ?
            OR s.phone LIKE ?
            OR s.admission_no LIKE ?
            OR ss.roll_no LIKE ?

        )

        LIMIT 5
    ");

    $student_search = '%' . $search . '%';

    $student_stmt->execute([

        $school_id,
        $student_search,
        $student_search,
        $student_search,
        $student_search

    ]);

    $students = $student_stmt->fetchAll(PDO::FETCH_ASSOC);

    foreach ($students as $student) {

        $response[] = [

            'type' => 'Student',

            'title' => $student['name'],

            'subtitle' =>

                'Class: ' .
                ($student['class_name'] ?? '-') .

                ' | Roll No: ' .
                ($student['roll_no'] ?? '-') .

                ' | Admission: ' .
                ($student['admission_no'] ?? '-'),

            'url' =>

                WEB_ROOT .
                'student/view.php?id=' .
                $student['id']
        ];
    }

    /*
|--------------------------------------------------------------------------
| STAFF SEARCH
|--------------------------------------------------------------------------
*/

$staff_stmt = $pdo->prepare("
    SELECT

        id,
        name,
        qualification,
        mobile_no

    FROM teachers

    WHERE school_id = ?

    AND (

        name LIKE ?
        OR mobile_no LIKE ?

    )

    LIMIT 5
");

$staff_stmt->execute([

    $school_id,
    $student_search,
    $student_search

]);

$staffs = $staff_stmt->fetchAll(PDO::FETCH_ASSOC);

foreach ($staffs as $staff) {

    $response[] = [

        'type' => 'Staff',

        'title' => $staff['name'],

        'subtitle' =>

            'Qualification: ' .
            ($staff['qualification'] ?? '-') .

            ' | Mobile: ' .
            ($staff['mobile_no'] ?? '-'),

        'url' =>

            WEB_ROOT .
            'teacher/view.php?id=' .
            $staff['id']
    ];
}

   /*
|--------------------------------------------------------------------------
| FEES SEARCH
|--------------------------------------------------------------------------
*/

$fees_stmt = $pdo->prepare("
    SELECT

        f.id,
        f.receipt_no,
        f.paid_amount,

        s.name

    FROM fee_payments f

    INNER JOIN students s
    ON s.id = f.student_id

    WHERE f.school_id = ?

    AND (

        f.receipt_no LIKE ?
        OR s.name LIKE ?

    )

    LIMIT 5
");

$fees_stmt->execute([

    $school_id,
    $student_search,
    $student_search

]);

$fees = $fees_stmt->fetchAll(PDO::FETCH_ASSOC);

foreach ($fees as $fee) {

    $response[] = [

        'type' => 'Fees',

        'title' =>

            $fee['receipt_no'],

        'subtitle' =>

            'Student: ' .
            $fee['name'] .

            ' | Amount: ₹' .
            $fee['paid_amount'],

        'url' =>

            WEB_ROOT .
            'fees/view_receipt.php?id=' .
            $fee['id']
    ];
}

} catch (Exception $e) {

    $response[] = [

        'type' => 'Error',

        'title' => 'Search Error',

        'subtitle' => $e->getMessage(),

        'url' => '#'
    ];
}

echo json_encode($response);

exit;