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

/**
 * Generate UNIQUE slug
 */
$slug = Slug::unique($name, 'blog_tags');

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

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