<?php
require_once 'init.php';

if (!userHasRole(['Admin', 'Manager'])) {
    http_response_code(403);
    echo json_encode(['status' => 'error', 'message' => 'Permission denied.']);
    exit();
}

$client_id = $_GET['client_id'] ?? null;
header('Content-Type: application/json');

if (!$client_id || !is_numeric($client_id)) {
    http_response_code(400);
    echo json_encode(['status' => 'error', 'message' => 'Invalid client ID.']);
    exit();
}

$stmt = $conn->prepare("SELECT * FROM client_onboarding_checklist WHERE client_id = ?");
$stmt->bind_param("i", $client_id);
$stmt->execute();
$checklist_data = $stmt->get_result()->fetch_assoc();
$stmt->close();

if (!$checklist_data) {
    // Agar client ke liye koi checklist nahi hai, to ek nayi banayein
    $conn->query("INSERT INTO client_onboarding_checklist (client_id) VALUES ($client_id)");
    $checklist_data = $conn->query("SELECT * FROM client_onboarding_checklist WHERE client_id = $client_id")->fetch_assoc();
}

echo json_encode(['status' => 'success', 'checklist' => $checklist_data]);
$conn->close();
?>