<?php
header('Content-Type: application/json');
require 'db_connect.php';

$data = json_decode(file_get_contents('php://input'), true);

if (!isset($data['title']) || !isset($data['content'])) {
    echo json_encode(['success' => false, 'error' => 'Missing required fields']);
    exit;
}

$title = $data['title'];
$content = $data['content'];
$slug = strtolower(preg_replace('/[^a-z0-9]+/', '-', trim($title)));
$created_at = date('Y-m-d H:i:s');
$updated_at = $created_at;

try {
    $stmt = $pdo->prepare("INSERT INTO pages (title, slug, content, created_at, updated_at) VALUES (?, ?, ?, ?, ?)");
    $stmt->execute([$title, $slug, $content, $created_at, $updated_at]);
    echo json_encode(['success' => true, 'page_id' => $pdo->lastInsertId()]);
} catch (Exception $e) {
    echo json_encode(['success' => false, 'error' => $e->getMessage()]);
}
