<?php
session_start();
require_once '../config/database.php';

if (!isset($_SESSION["user_id"])) { echo "Not logged in"; exit; }

$userId = (int)$_SESSION["user_id"];

echo "<pre style='font-family:monospace;background:#0f172a;color:#e2e8f0;padding:24px;font-size:13px;'>";
echo "=== CUSTOMER HR DEBUG ===\n\n";
echo "user_id: $userId\n";
echo "role: {$_SESSION['role']}\n\n";

/* Check customers table */
$c = $pdo->prepare("SELECT id, customer_code, full_name, balance FROM customers WHERE user_id=?");
$c->execute([$userId]);
$cust = $c->fetch(PDO::FETCH_ASSOC);
echo "customers row: " . ($cust ? "FOUND — id={$cust['id']} name={$cust['full_name']}" : "NOT FOUND") . "\n\n";

/* Check company_staff table exists */
try {
    $cs = $pdo->prepare("SELECT cs.*, c.name AS company_name FROM company_staff cs JOIN companies c ON c.id=cs.company_id WHERE cs.user_id=?");
    $cs->execute([$userId]);
    $csRow = $cs->fetch(PDO::FETCH_ASSOC);
    echo "company_staff row: " . ($csRow ? "FOUND — company={$csRow['company_name']} role={$csRow['staff_role']}" : "NOT FOUND") . "\n";
    if (!$csRow) {
        echo "\n⚠️  This user has no company_staff record.\n";
        echo "The HR section only shows for users who are in company_staff table.\n\n";
        echo "All company_staff rows:\n";
        $all = $pdo->query("SELECT cs.user_id, cs.company_id, cs.staff_role, u.name, u.role FROM company_staff cs JOIN users u ON u.id=cs.user_id")->fetchAll(PDO::FETCH_ASSOC);
        foreach ($all as $r) {
            echo "  user_id={$r['user_id']} name={$r['name']} role={$r['role']} company_id={$r['company_id']} staff_role={$r['staff_role']}\n";
        }
    }
} catch (Throwable $e) {
    echo "company_staff ERROR: " . $e->getMessage() . "\n";
}

echo "</pre>";