<?php
/**
 * Get Employee's HR (Reporting Manager) API
 */

require_once __DIR__ . '/../includes/init.php';
requireLogin();

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

$employeeId = isset($_GET['employee_id']) ? (int)$_GET['employee_id'] : 0;
$siteId = getCurrentSiteId();

if ($employeeId <= 0) {
    echo json_encode(['success' => false, 'message' => 'Invalid employee ID']);
    exit;
}

try {
    $db = getDB();
    
    // Get employee's HR (created_by user)
    $stmt = $db->prepare("
        SELECT u.full_name as hr_name, u.user_id as hr_id
        FROM employees e
        LEFT JOIN users u ON e.created_by = u.user_id
        WHERE e.employee_id = ? AND e.site_id = ?
    ");
    $stmt->execute([$employeeId, $siteId]);
    $result = $stmt->fetch();
    
    if ($result && $result['hr_name']) {
        echo json_encode([
            'success' => true,
            'hr_name' => $result['hr_name'],
            'hr_id' => $result['hr_id']
        ]);
    } else {
        echo json_encode([
            'success' => false,
            'message' => 'HR not found for this employee'
        ]);
    }
} catch (Exception $e) {
    error_log("Error getting employee HR: " . $e->getMessage());
    echo json_encode([
        'success' => false,
        'message' => 'Error fetching HR information'
    ]);
}

