<?php
header('Content-Type: application/json');
require_once __DIR__ . '/../../../shared/utils/helpers.php';
require_once __DIR__ . '/../../../shared/auth/jwt.php';
require_once __DIR__ . '/../../../shared/database/connection.php';

$userData = JWTAuth::authenticate();
if (!$userData) {
    jsonResponse(['success' => false, 'error' => 'No autenticado'], 401);
}

$input = json_decode(file_get_contents('php://input'), true);
$comando = trim($input['comando'] ?? '');
$descripcion = trim($input['descripcion'] ?? '');
$plantilla_id = $input['plantilla_id'] ?? null;

if (empty($comando) || empty($plantilla_id)) {
    jsonResponse(['success' => false, 'error' => 'Faltan datos requeridos'], 400);
}

try {
    $db = getDB();
    $stmt = $db->prepare("
        INSERT INTO comandos_discord (comando, descripcion, plantilla_id)
        VALUES (?, ?, ?)
    ");
    $stmt->execute([$comando, $descripcion, $plantilla_id]);
    
    jsonResponse(['success' => true]);
} catch (PDOException $e) {
    if ($e->getCode() == 23000) {
        jsonResponse(['success' => false, 'error' => 'Este comando ya existe'], 409);
    }
    jsonResponse(['success' => false, 'error' => $e->getMessage()], 500);
}
