<?php
declare(strict_types=1);

Auth::requireRole('analyst');
Csrf::check();

$companyId = (int) post('company_id', 0);
$body = trim((string) post('body', ''));

if ($body === '') {
    redirect('companies', 'view', ['id' => $companyId]);
}

Database::run(
    'INSERT INTO notes (company_id, user_id, note_date, body) VALUES (:c, :u, CURDATE(), :b)',
    ['c' => $companyId, 'u' => Auth::id(), 'b' => $body]
);

Audit::log('create', 'note', Database::lastId(), ['company_id' => $companyId]);
redirect('companies', 'view', ['id' => $companyId]);
