<?php
declare(strict_types=1);
require_once __DIR__ . '/_bootstrap.php';

ensure_schema();
$pdo = db();

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
  $input = read_json();
  $keyword = trim((string)($input['keyword'] ?? ''));
  $reply = trim((string)($input['reply'] ?? ''));
  if ($keyword === '' || $reply === '') {
    json_response(['success' => false, 'error' => 'Missing fields']);
  }
  $stmt = $pdo->prepare("INSERT INTO auto_reply (keyword, reply) VALUES (?, ?)");
  $stmt->execute([$keyword, $reply]);
  json_response(['success' => true]);
}

$rules = $pdo->query("SELECT id, keyword, reply, created_at FROM auto_reply ORDER BY created_at DESC")->fetchAll();
json_response(['rules' => $rules]);
