<?php
// includes/functions/tasknames/tasknames.php
if (!defined('ABSPATH')) exit;

/**
 * Standalone task name generator (no sessionStorage, no catalog JSON).
 * Example result: "SCB-20251106-4F9A • Request"
 *
 * Inputs (all optional):
 * - project_id: used to derive a prefix from the 'afb_project_prefix_map' option (lines like "123=SCB")
 * - first_name, last_name, subject, request_title: used to build a human label if available
 */
function afb_generate_task_name_standalone(array $args) : string {
    $project_id = (string)($args['project_id'] ?? '');

    // Get prefix map from option (same format you already use elsewhere)
    $map = [];
    $raw = (string)get_option('afb_project_prefix_map', '');
    foreach (explode("\n", trim($raw)) as $line) {
        if (strpos($line, '=') !== false) {
            [$pid, $pre] = array_map('trim', explode('=', $line, 2));
            if ($pid !== '') $map[$pid] = $pre;
        }
    }
    $prefix = $map[$project_id] ?? 'SCB';

    // Optional “human bits”
    $first = trim((string)($args['first_name'] ?? ''));
    $last  = trim((string)($args['last_name']  ?? ''));
    $topic = trim((string)($args['subject']    ?? $args['request_title'] ?? ''));

    $human = $topic !== '' ? $topic : trim("$first $last");
    if ($human === '') $human = 'Request';

    // Date + 4-char base36 entropy
    $ts    = current_time('timestamp');
    $ymd   = date('Ymd', $ts);
    $rand4 = strtoupper(substr(base_convert(random_int(2176782336, 78364164095), 10, 36), 0, 4));

    return sprintf('%s-%s-%s • %s', $prefix, $ymd, $rand4, $human);
}

/** ──────────────────────────────────────────────────────────────
 * Legacy stubs (safe no-ops). Keep them so older code won’t fatal.
 * You can delete these later once you’re sure nothing calls them.
 * ────────────────────────────────────────────────────────────── */
if (!function_exists('afb_taskname_resolve_template')) {
    function afb_taskname_resolve_template(string $catalog_json, string $service, string $subservice, ?string $program = null): ?string {
        return null; // deprecated: templates/sessionStorage not used in standalone mode
    }
}

if (!function_exists('afb_print_taskname_bootstrap')) {
    function afb_print_taskname_bootstrap(string $catalog_json, string $service, string $subservice, ?string $program = null, array $answers = []) : void {
        // deprecated: no output in standalone mode
    }
}
