<?php
require_once __DIR__ . '/../config/db.php';
require_once __DIR__ . '/../config/auth.php';
$user = requireAuth();
$db   = getDB();

$clientId = (int)post('client_id', 0);
if (!$clientId) apiError('Client ID required.', 422);

try {
    $stmt = $db->prepare("
        SELECT a.*,
               u.full_name AS created_by_name,
               (SELECT COUNT(*) FROM client_asset_images i WHERE i.asset_id = a.id) AS image_count
        FROM client_assets a
        LEFT JOIN users u ON u.id = a.created_by
        WHERE a.client_id = ?
        ORDER BY a.status, a.asset_type, a.name
    ");
    $stmt->execute([$clientId]);
    apiSuccess(['assets' => $stmt->fetchAll()]);
} catch (Exception $e) {
    apiError('Database error: ' . $e->getMessage(), 500);
}
