<?php
declare(strict_types=1);

require_once __DIR__ . '/../../../security/auth_guard.php';
require_once __DIR__ . '/../../../config/database.php';
require_once __DIR__ . '/../../../core/Response.php';
require_once __DIR__ . '/../../../core/Validator.php';
require_once __DIR__ . '/../../../core/Slug.php';

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

Validator::required($data, ['name']);

$name = trim($data['name']);
if ($name === '') {
    Response::error('Category name is required', 422);
}

// ✅ Correct slug generation
$slug = Slug::unique($name, 'blog_categories');

/**
 * Insert
 */
$stmt = $db->prepare("
    INSERT INTO blog_categories (name, slug, status, created_at)
    VALUES (:name, :slug, 'Active', NOW())
");
$stmt->execute([
    'name' => $name,
    'slug' => $slug
]);

Response::success('Blog category created successfully');
