<?php

namespace SWPanel\Pages;

defined('ABSPATH') || die('No direct access.');

use SWPanel\SettingsManager;

class CreatingPage {
    public function __construct() {
        add_action('admin_menu', [$this, 'register']);
        add_action('wp_ajax_swpanel_ai_create_task', ['SWPanel\\SettingsManager', 'createTask']);
        add_action('wp_ajax_swpanel_ai_check_task', function() {
            $task = get_transient('swpanel_ai_background_task');
            if (!$task) {
                wp_send_json_error(__('Task not found', 'swpanel-ai'));
            }
            wp_send_json_success($task);
        });
        add_action('swpanel_ai_long_task', function () {
            SettingsManager::creatingPage();
        });
    }

    public function register() {
        add_submenu_page(null, __('SWPanel Creating', 'swpanel-ai'), __('SWPanel Creating', 'swpanel-ai'), 'manage_options', 'swpanel_ai_creating', [$this, 'render']);
    }

    public function render() {
        update_option('swpanel_ai_page', 'swpanel_ai_creating');
        include SWPANEL_PATH . 'admin/views/creating-page.php';
    }
}
