<?php
/**
 * Resource registry bridge for the content agents. Reads curated sources (mde_resources)
 * per agent_type so the Node pipeline never needs DB creds.
 *
 *   php api/cli/resources.php list <agent_type>     -> JSON array of active resources
 *   php api/cli/resources.php touch <id> [<id>...]  -> stamp last_used_at
 */
require __DIR__ . '/../vendor/autoload.php';
require __DIR__ . '/../config.php';

$cmd = $argv[1] ?? '';

if ($cmd === 'list') {
    $type = $argv[2] ?? '';
    if ($type === '') { fwrite(STDERR, "usage: resources.php list <agent_type>\n"); exit(1); }
    $rows = \Db::select(
        "SELECT resource_id, name, url, kind, notes, priority
         FROM mde_resources
         WHERE agent_type = ? AND active = 1
         ORDER BY priority DESC, resource_id ASC",
        [$type]
    );
    echo json_encode($rows, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE);
    exit;
}

if ($cmd === 'touch') {
    $ids = array_slice($argv, 2);
    foreach ($ids as $id) {
        \Db::execute("UPDATE mde_resources SET last_used_at = NOW() WHERE resource_id = ?", [(int) $id]);
    }
    echo json_encode(['ok' => true, 'touched' => count($ids)]);
    exit;
}

fwrite(STDERR, "usage: resources.php list <agent_type> | touch <id...>\n");
exit(1);
