<?php
header('Content-Type: application/json');
require_once dirname(__DIR__) . '/include/docker.php';

if ($_SERVER['REQUEST_METHOD'] === 'GET') {
    echo json_encode(vv_dk_all());
    exit;
}

if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
    http_response_code(405);
    echo json_encode(['ok' => false, 'error' => 'GET or POST only']);
    exit;
}

$action    = trim($_POST['action']    ?? '');
$container = trim($_POST['container'] ?? '');
$folderId  = trim($_POST['folder_id'] ?? '');
$name      = trim($_POST['name']      ?? '');

$result = match ($action) {
    'move_container'   => vv_dk_move_container($container, $folderId),
    'create_folder'    => vv_dk_create_folder($name),
    'rename_folder'    => vv_dk_rename_folder($folderId, $name),
    'delete_folder'    => vv_dk_delete_folder($folderId),
    'sync_conf_to_json'=> vv_dk_sync_conf_to_json(),
    'sync_json_to_conf'=> vv_dk_sync_json_to_conf(),
    default            => ['ok' => false, 'error' => 'Unknown action: ' . $action],
};

echo json_encode($result);
