#!/usr/bin/env php
<?php
/**
 * File infra validation findings as tickets (tag 20260608-validation).
 *
 * Usage:
 *   php scripts/ingest_validation_findings_20260608.php [--url=BASE] [--dry-run]
 */
declare(strict_types=1);

$opts = getopt('', ['url::', 'dry-run']);
$base = rtrim($opts['url'] ?? 'https://apps.f0xx.org/app/androidcast_project/crashes', '/');
$dryRun = isset($opts['dry-run']);
$uploadUrl = $base . '/api/ticket_upload.php';

function val_tags(string $kind): array {
    $base = [
        ['id' => 'ticket', 'label' => 'ticket', 'bg' => '#6366f1'],
        ['id' => '20260608-validation', 'label' => '20260608-validation', 'bg' => '#f59e0b'],
        ['id' => 'infra', 'label' => 'infra', 'bg' => '#0ea5e9'],
    ];
    if ($kind === 'issue') {
        $base[] = ['id' => 'issue', 'label' => 'issue', 'bg' => '#94a3b8'];
    }
    return $base;
}

$tasks = [
    [
        'key' => 'SVC-OTA',
        'kind' => 'ticket',
        'text' => 'ISSUE SVC-OTA: OTA channel HTTP 400 on apps.f0xx.org — wire /v0/ota/ on apps vhost or align device ota.base_url. See docs/20260608_BE_SERVICES_and_infra.md',
    ],
    [
        'key' => 'SVC-URL',
        'kind' => 'ticket',
        'text' => 'ISSUE SVC-URL: Legacy f0xx.org crash upload URL 404 — production base should be apps.f0xx.org. Update settings.json / dev defaults.',
    ],
    [
        'key' => 'SVC-DIAG',
        'kind' => 'issue',
        'text' => 'ISSUE SVC-DIAG: diag.php returns 403 at public edge — acceptable; devices use upload.php + heartbeat.php. Documented in BE services map.',
    ],
    [
        'key' => 'SFU-SPEC',
        'kind' => 'issue',
        'text' => 'PLANNING SFU-SPEC: Janus/mediasoup relay post-alpha — branch feature/sfu-relay (hidden). Await owner spec. Gray section in docs/20260608_BE_SERVICES_and_infra.md',
    ],
    [
        'key' => 'graphs-5.1',
        'kind' => 'ticket',
        'status' => 'closed',
        'text' => 'CLOSED 5.1: Graph brick drill-down — full overlay, axes, legend, hover implemented in graphs.js (2026-06-08).',
    ],
    [
        'key' => 'graphs-5.2',
        'kind' => 'ticket',
        'status' => 'closed',
        'text' => 'CLOSED 5.2: Graph columns 1/2/4, clickable bricks, full-width shell--graphs-full CSS (2026-06-08).',
    ],
    [
        'key' => 'browser-js',
        'kind' => 'ticket',
        'status' => 'closed',
        'text' => 'CLOSED: Browser SyntaxError on console JS — deployed assets pass node --check; validate_be_services.sh added.',
    ],
];

$device = ['manufacturer' => 'validation', 'model' => 'infra', 'sdk_int' => 34, 'release' => '14'];
$app = ['package' => 'com.foxx.androidcast', 'version_name' => 'validation', 'version_code' => 1];
$now = (int) round(microtime(true) * 1000);

foreach ($tasks as $task) {
    $text = trim($task['text']);
    $key = $task['key'];
    $status = $task['status'] ?? 'open';
    $tags = val_tags($task['kind']);
    if ($status === 'closed') {
        $tags[] = ['id' => 'closed', 'label' => 'closed', 'bg' => '#6b7280'];
    } else {
        $tags[] = ['id' => 'open', 'label' => 'open', 'bg' => '#22c55e'];
    }
    $payload = [
        'schema_version' => 1,
        'ingest_kind' => 'ticket',
        'ticket_id' => 'validation-20260608-' . preg_replace('/[^a-z0-9.-]+/i', '-', $key),
        'title' => mb_substr($text, 0, 256),
        'brief' => mb_substr($text, 0, 512),
        'body' => $text,
        'opened_at_epoch_ms' => $now,
        'rating' => $status === 'closed' ? 1 : 3,
        'owner' => 'admin',
        'tags' => $tags,
        'device' => $device,
        'app' => $app,
    ];
    if ($dryRun) {
        echo "DRY {$key}\n";
        continue;
    }
    $ch = curl_init($uploadUrl);
    curl_setopt_array($ch, [
        CURLOPT_POST => true,
        CURLOPT_RETURNTRANSFER => true,
        CURLOPT_HTTPHEADER => ['Content-Type: application/json'],
        CURLOPT_POSTFIELDS => json_encode($payload, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES),
        CURLOPT_TIMEOUT => 30,
    ]);
    $resp = curl_exec($ch);
    $code = (int) curl_getinfo($ch, CURLINFO_HTTP_CODE);
    curl_close($ch);
    if ($code >= 200 && $code < 300) {
        echo "OK {$key} HTTP {$code}\n";
    } else {
        echo "FAIL {$key} HTTP {$code} {$resp}\n";
    }
}
