<?php
require __DIR__ . '/../Login/config.php';
if (session_status() === PHP_SESSION_NONE) session_start();

if (!isset($_SESSION['user_id'])) {
  echo "Silakan login terlebih dahulu.";
  exit;
}

$user_id = $_SESSION['user_id'];
$book_id = $_POST['book_id'] ?? 0;
$rating = $_POST['rating'] ?? 0;
$komentar = trim($_POST['komentar'] ?? '');

if (!$book_id || $rating < 1 || $rating > 5 || empty($komentar)) {
  echo "Data tidak valid.";
  exit;
}

$stmt = $pdo->prepare("INSERT INTO reviews (user_id, book_id, rating, komentar) VALUES (?, ?, ?, ?)");
if ($stmt->execute([$user_id, $book_id, $rating, $komentar])) {
  echo "Ulasan berhasil dikirim. Terima kasih!";
} else {
  echo "Gagal menyimpan ulasan.";
}
?>
