<?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']);

/**
 * Slug generation
 */

$slug = Slug::make($data['name']);


/**
 * Duplicate check
 */
$stmt = $db->prepare("SELECT id FROM faq_categories WHERE slug = :slug");
$stmt->execute(['slug' => $slug]);
if ($stmt->fetch()) {
    Response::error('FAQ category already exists', 409);
}

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

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