<?php
// POST /api/clients/delete.php
require_once __DIR__ . '/../config/db.php';
require_once __DIR__ . '/../config/auth.php';

$user = requireRole([1]); // Admin only
$db   = getDB();
$id   = (int)post('id', 0);

if (!$id) apiError('Client ID required.', 422);

// Check for linked records
$projectCount = (int)$db->prepare("SELECT COUNT(*) FROM projects WHERE client_id=?")->execute([$id]) ? 0 : 0;
$stmt = $db->prepare("SELECT COUNT(*) FROM projects WHERE client_id = ?");
$stmt->execute([$id]);
if ((int)$stmt->fetchColumn() > 0) {
    apiError('Cannot delete: client has linked projects. Archive the client instead.', 422);
}

$db->prepare("DELETE FROM clients WHERE id = ?")->execute([$id]);
apiSuccess([], 'Client deleted.');
