<?php
header("Content-Type: application/json");
$method = $_SERVER['REQUEST_METHOD'];
$host = 'localhost'; // Change as needed
$db = 'u950110925_notes';
$user = 'u950110925_notes'; // Change as needed
$pass = 'Notesnotes12345678'; // Change as needed

$conn = new mysqli($host, $user, $pass, $db);

if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}

switch ($method) {
    case 'GET':
        $sql = "SELECT id, author, content FROM notes_baccarat";
        $result = $conn->query($sql);
        $notes = [];
        while ($row = $result->fetch_assoc()) {
            $notes[] = $row;
        }
        echo json_encode($notes);
        break;

    case 'POST':
        $input = json_decode(file_get_contents('php://input'), true);
        $author = $conn->real_escape_string($input['author']);
        $content = $conn->real_escape_string($input['content']);
        $sql = "INSERT INTO notes_baccarat (author, content) VALUES ('$author', '$content')";
        if ($conn->query($sql) === TRUE) {
            $note_id = $conn->insert_id;
            echo json_encode(['id' => $note_id, 'author' => $author, 'content' => $content]);
        } else {
            http_response_code(500);
            echo json_encode(['error' => $conn->error]);
        }
        break;

    case 'DELETE':
        $id = intval(basename($_SERVER['REQUEST_URI']));
        $sql = "DELETE FROM notes_baccarat WHERE id = $id";
        if ($conn->query($sql) === TRUE) {
            http_response_code(204);
        } else {
            http_response_code(500);
            echo json_encode(['error' => $conn->error]);
        }
        break;

    default:
        http_response_code(405);
        break;
}

$conn->close();
?>
