<?php
session_start();
if (!isset($_SESSION['admin'])) {
  die('Unauthorized access.');
}

$id = (int)($_POST['id'] ?? -1);
$reply = trim($_POST['reply'] ?? '');

if ($id < 0 || !$reply) {
  die('Invalid input.');
}

$file = '../comments.txt';
if (!file_exists($file)) {
  die('Comments file not found.');
}

$entries = explode("---\n", file_get_contents($file));
if (!isset($entries[$id])) {
  die('Comment not found.');
}

$entries[$id] .= "\nReply: " . $reply;
file_put_contents($file, implode("---\n", $entries));

header('Location: dashboard.php');
