<?php
require_once 'config.php';

header('Content-Type: application/json');

try {
    $data = json_decode(file_get_contents('php://input'), true);
    
    // Validate input
    if (empty($data['name']) || empty($data['review']) || empty($data['rating'])) {
        throw new Exception('All fields are required');
    }
    
    if (!is_numeric($data['rating']) || $data['rating'] < 1 || $data['rating'] > 5) {
        throw new Exception('Invalid rating value');
    }
    
    // Insert review
    $stmt = $pdo->prepare("INSERT INTO reviews (name, rating, review) VALUES (?, ?, ?)");
    $stmt->execute([
        htmlspecialchars(trim($data['name'])),
        (int)$data['rating'],
        htmlspecialchars(trim($data['review']))
    ]);
    
    echo json_encode(['success' => true]);
} catch (Exception $e) {
    echo json_encode(['success' => false, 'message' => $e->getMessage()]);
}
?>