<?php

/**
 * Admin Menu Registration
 */

if (!defined('ABSPATH')) {
    exit;
}

// Include view files in correct order to avoid circular dependencies
require_once dirname(__FILE__) . '/../views/main-page.php';
require_once dirname(__FILE__) . '/../views/auth-page.php';
require_once dirname(__FILE__) . '/../views/dashboard-page.php';
require_once dirname(__FILE__) . '/../views/settings-page.php';
require_once dirname(__FILE__) . '/../views/article-generator-page.php';
require_once dirname(__FILE__) . '/../views/title-article-page.php';
require_once dirname(__FILE__) . '/../views/article-status-page.php';

/**
 * Register admin menu pages
 */
function content_craft_ai_register_admin_menu()
{
    // Check authentication status - this will verify with the API if needed
    $is_authenticated = content_craft_ai_is_authenticated();

    // Always register the auth page for fallback
    add_menu_page(
        'Content Craft AI',
        'Content Craft AI',
        'manage_options',
        'content-craft-ai',
        'content_craft_ai_render_main_page',
        'dashicons-admin-generic',
        30
    );

    if ($is_authenticated) {
        // When authenticated, register the dashboard
        add_menu_page(
            'Content Craft AI',
            'Content Craft AI',
            'manage_options',
            'content-craft-ai-dashboard',
            'content_craft_ai_render_dashboard_page',
            'dashicons-admin-generic',
            30
        );

        // Add article generator as submenu
        add_submenu_page(
            'content-craft-ai-dashboard',
            'Article Generator',
            'Article Generator',
            'manage_options',
            'content-craft-ai-article-generator',
            'content_craft_ai_render_article_generator_page'
        );

        // Add title to article as submenu
        add_submenu_page(
            'content-craft-ai-dashboard',
            'Title Generator',
            'Title Generator',
            'manage_options',
            'content-craft-ai-title-article',
            'content_craft_ai_render_title_article_page'
        );

        // Add article status as submenu
        add_submenu_page(
            'content-craft-ai-dashboard',
            'Article Status',
            'Article Status',
            'manage_options',
            'content-craft-ai-article-status',
            'content_craft_ai_render_article_status_page'
        );

        // Add settings as submenu
        add_submenu_page(
            'content-craft-ai-dashboard',
            'Settings',
            'Settings',
            'manage_options',
            'content-craft-ai-settings',
            'content_craft_ai_render_settings_page'
        );

        // Rename the first submenu item to Dashboard (instead of "Content Craft AI")
        global $submenu;
        if (isset($submenu['content-craft-ai-dashboard'])) {
            $submenu['content-craft-ai-dashboard'][0][0] = 'Dashboard';
        }

        // Hide the auth page from menu when logged in
        remove_menu_page('content-craft-ai');
    }
}

/**
 * Initialize the plugin
 */
function content_craft_ai_init()
{
    // Register menus and assets
    add_action('admin_menu', 'content_craft_ai_register_admin_menu');
    add_action('admin_enqueue_scripts', 'content_craft_ai_register_admin_assets');
}
