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

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

if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
    http_response_code(405);
    echo json_encode(array('success' => false, 'message' => '请求方式不正确。'), JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
    exit;
}

require_csrf();
try {
    $taskType = isset($_POST['task_type']) ? trim((string)$_POST['task_type']) : 'homepage';
    if ($taskType === 'article') {
        $domainIdsRaw = isset($_POST['domain_ids']) ? $_POST['domain_ids'] : array();
        $domainIds = is_array($domainIdsRaw) ? array_values(array_filter(array_map('intval', $domainIdsRaw))) : array();
        $payload = array(
            'task_type' => 'article',
            'domain_ids' => $domainIds,
            'domain_id' => !empty($domainIds) ? (int)$domainIds[0] : 0,
            'template_id' => isset($_POST['template_id']) ? (int)$_POST['template_id'] : 0,
            'model_id' => isset($_POST['model_id']) ? (int)$_POST['model_id'] : 0,
            'custom_prompt' => isset($_POST['custom_prompt']) ? trim((string)$_POST['custom_prompt']) : '',
            'keywords_text' => isset($_POST['keywords_text']) ? trim((string)$_POST['keywords_text']) : '',
            'article_length' => isset($_POST['article_length']) ? trim((string)$_POST['article_length']) : '',
            'path_prefix' => isset($_POST['path_prefix']) ? trim((string)$_POST['path_prefix']) : 'article',
            'interval_seconds' => isset($_POST['interval_seconds']) ? (int)$_POST['interval_seconds'] : 0,
            'generate_image' => isset($_POST['generate_image']) ? 1 : 0,
            'remark' => isset($_POST['remark']) ? trim((string)$_POST['remark']) : '',
            'publish_now' => isset($_POST['publish_now']) ? 1 : 0,
            'debug_mode' => !empty($_POST['debug_mode'])
        );
    } else {
        $payload = array(
            'task_type' => 'homepage',
            'domain_id' => isset($_POST['domain_id']) ? (int)$_POST['domain_id'] : 0,
            'template_id' => isset($_POST['template_id']) ? (int)$_POST['template_id'] : 0,
            'model_id' => isset($_POST['model_id']) ? (int)$_POST['model_id'] : 0,
            'keyword' => isset($_POST['keyword']) ? trim((string)$_POST['keyword']) : '',
            'site_name' => isset($_POST['site_name']) ? trim((string)$_POST['site_name']) : '',
            'site_description' => isset($_POST['site_description']) ? trim((string)$_POST['site_description']) : '',
            'site_language' => isset($_POST['site_language']) ? trim((string)$_POST['site_language']) : '',
            'remark' => isset($_POST['remark']) ? trim((string)$_POST['remark']) : '',
            'publish_now' => isset($_POST['publish_now']) ? 1 : 0,
            'debug_mode' => !empty($_POST['debug_mode'])
        );
    }

    $task = create_site_build_task($payload);

    echo json_encode(array(
        'success' => true,
        'message' => '建站任务已创建。',
        '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);
}
