<?php
require __DIR__ . '/../../app/bootstrap.php';
require __DIR__ . '/../../app/database.php';
require_login();
session_write_close();

header('Content-Type: application/json; charset=utf-8');

$taskToken = isset($_GET['task_token']) ? trim((string)$_GET['task_token']) : '';
if ($taskToken === '') {
    http_response_code(400);
    echo json_encode(array('success' => false, 'message' => '缺少任务标识。'), JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
    exit;
}

try {
    $task = find_site_build_task_by_token($taskToken);
    if (!$task) {
        http_response_code(404);
        echo json_encode(array('success' => false, 'message' => '未找到建站任务。'), JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
        exit;
    }

    echo json_encode(array(
        'success' => true,
        'task' => site_build_task_response_data($task)
    ), JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
} catch (Exception $e) {
    http_response_code(500);
    echo json_encode(array(
        'success' => false,
        'message' => $e->getMessage()
    ), JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
}
