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

$user = requireAuth();
$db   = getDB();

$clientId = (int)post('client_id', 0);
$body     = trim(post('body', ''));

if (!$clientId) apiError('Client ID required.', 422);
if (!$body)     apiError('Note body required.', 422);

$db->prepare("
    INSERT INTO client_notes (client_id, user_id, note_type, title, body, meeting_date)
    VALUES (?, ?, ?, ?, ?, ?)
")->execute([
    $clientId,
    $user['id'],
    post('note_type', 'general'),
    post('title'),
    $body,
    post('meeting_date') ?: null
]);

apiSuccess(['id' => (int)$db->lastInsertId()], 'Note added.');
