<?php
require_once __DIR__ . '/../includes/db.php';
require_once __DIR__ . '/../includes/auth.php';

// Prepare test data
// Let's find or insert a test manager employee and their reporting manager
// Owner User: ID = 1
// Manager User: ID = 2
// Kamal3 (employee ID 1, user ID 2) reporting to Owner (user ID 1)

// Clear previous test requests to keep DB clean
query("DELETE FROM leave_requests WHERE reason = 'ROUTING_TEST_REASON'");

// Simulate Manager (Kamal3) applying for leave
// Employee ID: 1, User ID: 2, Company ID: 1
// Kamal3 has Reporting Manager = User ID 1 (Owner)
$company_id = 1;
$manager_user_id = 2; // Kamal3 user ID
$manager_emp_id = 1;  // Kamal3 employee ID
$owner_user_id = 1;

// Insert a pending leave request for Kamal3 (the Manager), which is assigned to Owner (User ID 1)
query("
    INSERT INTO leave_requests (
        employee_id, company_id, reporting_manager_id, leave_type, duration_type, 
        start_date, end_date, reason, status
    ) VALUES (?, ?, ?, 'casual', 'full', '2026-06-01', '2026-06-02', 'ROUTING_TEST_REASON', 'pending')
", [$manager_emp_id, $company_id, $owner_user_id]);

$reqId = lastInsertId();
echo "Inserted test leave request ID {$reqId} for Employee {$manager_emp_id} routing to Manager {$owner_user_id}.\n";

// Test 1: Check pending approvals list for Kamal3 (User ID 2)
// Since Kamal3 is a manager, let's query their pending approvals.
// It should NOT include this request because Kamal3 is not the reporting manager for this request (Owner is).
$pending_for_kamal = fetchAll("
    SELECT r.id FROM leave_requests r
    WHERE r.company_id = ? AND r.reporting_manager_id = ? AND r.status = 'pending'
", [$company_id, $manager_user_id]);

$found = false;
foreach ($pending_for_kamal as $p) {
    if ($p['id'] == $reqId) {
        $found = true;
    }
}

if ($found) {
    echo "FAILED: Leave request {$reqId} was found in the manager's own pending approvals list!\n";
    exit(1);
} else {
    echo "PASSED: Leave request {$reqId} was NOT found in the manager's own pending approvals list.\n";
}

// Test 2: Simulate Kamal3 (User ID 2) trying to action/approve this request
// In api/leaves.php, we do:
// if ($req['reporting_manager_id'] != $user_id) { ... fail ... }
$req = fetch("SELECT * FROM leave_requests WHERE id = ?", [$reqId]);
if ($req['reporting_manager_id'] != $manager_user_id) {
    echo "PASSED: API checks succeeded. User ID {$manager_user_id} is not the designated reporting manager (expected {$req['reporting_manager_id']}).\n";
} else {
    echo "FAILED: Kamal3 was incorrectly recognized as the reporting manager.\n";
    exit(1);
}

// Test 3: Check pending approvals list for Owner (User ID 1)
$pending_for_owner = fetchAll("
    SELECT r.id FROM leave_requests r
    WHERE r.company_id = ? AND r.reporting_manager_id = ? AND r.status = 'pending'
", [$company_id, $owner_user_id]);

$found_by_owner = false;
foreach ($pending_for_owner as $p) {
    if ($p['id'] == $reqId) {
        $found_by_owner = true;
    }
}

if ($found_by_owner) {
    echo "PASSED: Leave request {$reqId} is correctly routed to Owner (User ID 1).\n";
} else {
    echo "FAILED: Leave request {$reqId} was not found in the Owner's pending approvals list.\n";
    exit(1);
}

// Clean up
query("DELETE FROM leave_requests WHERE id = ?", [$reqId]);
echo "Cleaned up test records. All routing tests PASSED successfully!\n";
