<?php
require_once __DIR__ . '/../config/database.php';
require_once __DIR__ . '/../services/NewsAggregatorService.php';
require_once __DIR__ . '/../services/GeoPoliticalAggregator.php';

function e($v)
{
    return htmlspecialchars((string)$v, ENT_QUOTES, 'UTF-8');
}

$symbol = $_GET['symbol'] ?? 'XAUUSD';

$news = NewsAggregatorService::check($symbol, 90);
$geo = GeoPoliticalAggregator::analyze($symbol);

?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Phase 1B Risk Aggregator Diagnostics</title>
<script src="https://cdn.tailwindcss.com"></script>
</head>
<body class="bg-slate-100 text-slate-900 min-h-screen">
<div class="max-w-7xl mx-auto px-6 py-10">

    <div class="mb-8">
        <h1 class="text-4xl font-black">Phase 1B Risk Aggregator Diagnostics</h1>
        <p class="text-slate-600 mt-2">Multi-source Macro + RSS + Geopolitical Intelligence</p>

        <form method="get" class="mt-5 flex gap-3 max-w-md">
            <input name="symbol" value="<?= e($symbol) ?>" class="w-full rounded-lg border border-slate-300 px-3 py-2">
            <button class="px-4 py-2 rounded-lg bg-blue-600 text-white font-bold">Test</button>
        </form>
    </div>

    <div class="grid grid-cols-1 lg:grid-cols-2 gap-6">

        <div class="bg-white rounded-2xl shadow-sm border border-slate-200 p-6">
            <h2 class="text-xl font-bold mb-4">Macro / Forex News Aggregator</h2>
            <p><strong>Risk:</strong> <?= e($news['risk'] ?? 'UNKNOWN') ?></p>
            <p><strong>Penalty:</strong> <?= e($news['score_penalty'] ?? 0) ?></p>
            <p><strong>Source:</strong> <?= e($news['source'] ?? '') ?></p>
            <p class="mt-2 text-slate-700"><?= e($news['message'] ?? '') ?></p>

            <div class="mt-5 space-y-3">
                <?php foreach (array_slice($news['events'] ?? [], 0, 8) as $event): ?>
                    <div class="rounded-xl border border-slate-200 bg-slate-50 p-4 text-sm">
                        <strong><?= e($event['title'] ?? 'News event') ?></strong>
                        <div class="text-slate-500 mt-1"><?= e($event['source'] ?? '') ?> <?= !empty($event['time']) ? ' | ' . e($event['time']) : '' ?></div>
                    </div>
                <?php endforeach; ?>
            </div>
        </div>

        <div class="bg-white rounded-2xl shadow-sm border border-slate-200 p-6">
            <h2 class="text-xl font-bold mb-4">Geopolitical Aggregator</h2>
            <p><strong>Risk:</strong> <?= e($geo['risk'] ?? 'UNKNOWN') ?></p>
            <p><strong>Impact:</strong> <?= e($geo['impact'] ?? 'Neutral') ?></p>
            <p><strong>Penalty:</strong> <?= e($geo['score_penalty'] ?? 0) ?></p>
            <p><strong>Raw Headlines Checked:</strong> <?= e($geo['raw_count'] ?? 0) ?></p>
            <p><strong>Source:</strong> <?= e($geo['source'] ?? '') ?></p>
            <p class="mt-2 text-slate-700"><?= e($geo['message'] ?? '') ?></p>

            <div class="mt-5 space-y-3">
                <?php foreach (array_slice($geo['events'] ?? [], 0, 8) as $event): ?>
                    <div class="rounded-xl border border-slate-200 bg-slate-50 p-4 text-sm">
                        <strong><?= e($event['title'] ?? 'Geo headline') ?></strong>
                        <div class="text-slate-500 mt-1"><?= e($event['source'] ?? '') ?> <?= !empty($event['published_at']) ? ' | ' . e($event['published_at']) : '' ?></div>
                    </div>
                <?php endforeach; ?>
            </div>
        </div>

    </div>
</div>
</body>
</html>
