<?php
$curatedFile = '/inc/curated_news.json';
$curated = file_exists($curatedFile) ? json_decode(file_get_contents($curatedFile), true) : [];

$filterDate = $_GET['date'] ?? date('Y-m-d');
?>
<!DOCTYPE html>
<html>
<head>
    <title>CryptoBitSense News</title>
    <style>
        body {font-family: Arial; max-width:800px; margin:auto; padding:20px;}
        .news-item { border-bottom: 1px solid #ccc; padding: 10px 0; }
    </style>
</head>
<body>
<h1>Curated Crypto News (<?= htmlspecialchars($filterDate) ?>)</h1>

<form method="GET">
    <label>Filter by date:</label>
    <input type="date" name="date" value="<?= htmlspecialchars($filterDate) ?>">
    <button type="submit">Filter</button>
</form>

<?php
$found = false;
foreach ($curated as $news):
    if (strpos($news['timestamp'], $filterDate) === 0):
        $found = true;
?>
    <div class="news-item">
        <h3><?= htmlspecialchars($news['title']) ?></h3>
        <p><strong>Summary:</strong> <?= htmlspecialchars($news['summary']) ?></p>
        <p><strong>Takeaway:</strong> <?= htmlspecialchars($news['takeaway']) ?></p>
        <p><a href="<?= htmlspecialchars($news['source']) ?>" target="_blank">Read more</a></p>
        <small>Published: <?= htmlspecialchars($news['timestamp']) ?></small>
    </div>
<?php
    endif;
endforeach;

if (!$found) {
    echo "<p>No curated news available for this date.</p>";
}
?>

</body>
</html>