<?php

namespace SWPanel\Pages;

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

use SWPanel\SettingsManager;

class ConceptPage {
    public function __construct() {
        add_action('admin_menu', [$this, 'register']);
        add_action('admin_post_swpanel_ai_concept_form', [$this, 'handleSubmit']);
    }

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

    public function render() {
        update_option('swpanel_ai_page', 'swpanel_ai_concept');

        $data = SettingsManager::getConceptData();
        
        include SWPANEL_PATH . 'admin/views/concept-form.php';
    }

    public function handleSubmit() {
        $nonce = isset($_POST['_wpnonce']) ? sanitize_text_field( wp_unslash( $_POST['_wpnonce'] ) ) : '';
        if ( ! $nonce || ! wp_verify_nonce( $nonce, 'swpanel_ai_concept' ) ) {
            wp_die( 'Invalid nonce' );
        }

        SettingsManager::saveConceptData($_POST);

        update_option('swpanel_ai_page', 'swpanel_ai_structure');
        wp_redirect(admin_url('admin.php?page=swpanel_ai_structure'));
        exit;
    }
}
