<?php

declare(strict_types=1);

require_once dirname(__DIR__, 3) . '/system/bootstrap.php';

$currentUser = myronRequireAuthenticatedUser();

if (($_SERVER['REQUEST_METHOD'] ?? 'GET') !== 'POST') {
    myronRedirect(myronBuildAdminUrl('dashboard', ['tab' => 'personal']));
}

try {
    myronRequireCsrf($_POST['csrf_token'] ?? null);
    myronDashboardNotesRepository()->deleteForUser(
        (string) $currentUser['id'],
        trim((string) ($_POST['note_id'] ?? ''))
    );

    myronRedirectWithFlash(myronBuildAdminUrl('dashboard', ['tab' => 'personal']), 'Note deleted.', 'success');
} catch (Throwable $exception) {
    myronRedirectWithFlash(myronBuildAdminUrl('dashboard', ['tab' => 'personal']), $exception->getMessage(), 'error');
}
