<?php
declare(strict_types=1);

require_once __DIR__ . '/_bootstrap.php';

golder_sales_require_post();
$master = golder_master_pdo();
golder_sales_rate_limit($master, 'sales_pricing', 60, 60);
golder_ensure_payment_tables($master);

$payload = golder_sales_payload();
$request = golder_sales_validate_plan_request($payload);
$normalized = $request['normalized'];
$rule = golder_find_pricing_rule($master, (string)$normalized['plan_type'], 'yearly', true);
if (!$rule) {
    $rule = golder_find_pricing_rule($master, (string)$normalized['plan_type'], 'yearly', false);
}
if (!$rule) {
    golder_json_error(404, 'NOT_FOUND', 'No pricing rule found.');
}

$pricing = golder_calculate_price($rule, $normalized);
golder_json_response([
    'status' => 'ok',
    'plan_type' => $normalized['plan_type'],
    'pc_devices' => $normalized['max_pc_devices'],
    'web_sessions' => $normalized['max_web_sessions'],
    'duration_key' => 'yearly',
    'total_amount' => number_format((float)$pricing['total'], 2, '.', ''),
    'currency' => (string)$pricing['currency'],
]);


