<?php
declare(strict_types=1);

require_once __DIR__ . '/_bootstrap.php';

golder_sales_require_post();
$master = golder_master_pdo();
golder_sales_rate_limit($master, 'portal_login', 20, 300);

$input = golder_portal_api_payload();
$email = trim((string)($input['email'] ?? ''));
$password = (string)($input['password'] ?? '');

$res = golder_portal_authenticate($master, $email, $password);
if (empty($res['ok'])) {
    golder_json_error(401, 'UNAUTHORIZED', (string)($res['message'] ?? 'Portal login failed.'));
}

$user = is_array($res['user'] ?? null) ? $res['user'] : null;
if (!$user) {
    golder_json_error(401, 'UNAUTHORIZED', 'Portal login failed.');
}

golder_portal_login_user($user);

golder_json_response([
    'status' => 'ok',
    'user' => $user,
]);








