<?php

function getInboxData($conn, $role, $department_id) {

    date_default_timezone_set("Asia/Kuala_Lumpur");

    $now = time();
    $output = [];

    /* ==================================
       TECHNICIAN NOTIFICATIONS
    ================================== */
    if ($role === "technician") {

        $user_id = $_SESSION['user_id'];

        /* ==================================
           EQUIPMENT DUE REMINDERS
        ================================== */
        $stmt = mysqli_prepare($conn, "
            SELECT
                id,
                equipment_name,
                next_due_date
            FROM equipment
            WHERE department_id = ?
            AND status = 'Active'
            AND next_due_date IS NOT NULL
        ");

        mysqli_stmt_bind_param(
            $stmt,
            "i",
            $department_id
        );

        mysqli_stmt_execute($stmt);

        $result = mysqli_stmt_get_result($stmt);

        while ($row = mysqli_fetch_assoc($result)) {

            $due = strtotime($row['next_due_date']);

            $diff = $due - $now;

            if ($diff <= 0) {

                $time_text = "⚠️ Equipment Maintenance Overdue";
                $class = "overdue";

            } else {

                $hours = floor($diff / 3600);
                $days  = floor($hours / 24);

                if ($days > 0) {

                    $time_text = "$days day(s) left";
                    $class = "normal";

                } else {

                    $time_text = "$hours hour(s) left";
                    $class = "warning";
                }
            }

            $output[] = [

                "equipment_name" =>
                    $row['equipment_name'],

                "due_date" =>
                    date("d M Y h:i A", $due),

                "time_text" =>
                    $time_text,

                "class" =>
                    $class,

                "link" =>
                    "equipment/equipment_details.php?id=" . $row['id']
            ];
        }

        /* ==================================
           MAINTENANCE NOTIFICATIONS
        ================================== */
        $stmt2 = mysqli_prepare($conn, "
            SELECT
                mr.id,
                mr.status,
                mr.hod_comment,
                mr.maintenance_date,
                e.equipment_name
            FROM maintenance_record mr
            JOIN equipment e
            ON mr.equipment_id = e.id
            WHERE mr.technician_id = ?
            ORDER BY mr.updated_at DESC
            LIMIT 15
        ");

        mysqli_stmt_bind_param(
            $stmt2,
            "i",
            $user_id
        );

        mysqli_stmt_execute($stmt2);

        $result2 = mysqli_stmt_get_result($stmt2);

        while ($row = mysqli_fetch_assoc($result2)) {

            /* HOD REVIEW */
            if ($row['status'] === 'HOD Review') {

                $output[] = [

                    "equipment_name" =>
                        $row['equipment_name'],

                    "due_date" => !empty($row['maintenance_date'])
                    ? date("d M Y h:i A", strtotime($row['maintenance_date']))
                    : "Not Scheduled",

                    "time_text" =>
                        "🟡 Waiting for HOD Review",

                    "class" =>
                        "warning",

                    "link" =>
                        "/../maintenance/record_details.php?id=" . $row['id']
                ];
            }

            /* HOD REJECTED */
            elseif ($row['status'] === 'HOD Rejected') {

                $comment = !empty($row['hod_comment'])
                    ? " - " . $row['hod_comment']
                    : "";

                $output[] = [

                    "equipment_name" =>
                        $row['equipment_name'],

                    "due_date" => !empty($row['maintenance_date'])
                    ? date("d M Y h:i A", strtotime($row['maintenance_date']))
                    : "Not Scheduled",

                    "time_text" =>
                        "🔴 HOD Rejected" . $comment,

                    "class" =>
                        "overdue",

                    "link" =>
                        "/../maintenance/record_details.php?id=" . $row['id']
                ];
            }

            
        }
    }

    return $output;
}