<?php
if ( ! defined( 'ABSPATH' ) ) exit;

add_action( 'admin_menu', 'sifency_addons_register_admin_menu' );

function sifency_addons_register_admin_menu() {
    add_menu_page(
        esc_html__( 'Sifency Addons Settings', 'sifency-addons' ),
        esc_html__( 'Sifency Addons', 'sifency-addons' ),
        'manage_options',
        'sifency-addons',
        'sifency_addons_render_welcome_page',
        SIFENCY_ADDONS_URL . 'assets/images/logo.png',
        25
    );

    $submenus = [
        [ 'Dashboard', 'sifency-addons', 'sifency_addons_render_welcome_page' ],
        [ 'Theme Builder', 'sifency-theme-builder', 'sifency_addons_render_theme_builder_page' ],
        [ 'Elementor Widgets', 'sifency-addons-widgets', 'sifency_addons_render_addons_widgets_page' ],
        [ 'Reusable Templates', 'sifency-templates-shortcode', 'sifency_addons_render_templates_shortcode_page' ],
        [ 'Analytics', 'sifency-analytics', 'sifency_addons_render_analytics_page' ],
        [ 'Settings', 'sifency-settings', 'sifency_addons_render_settings_page' ],
        [ 'Plugins', 'sifency-extentions', 'sifency_addons_render_extentions_page' ],
        [ 'Prebuilt Websites', 'sifency-prebuild-website', 'sifency_addons_render_prebuild_website_page' ],
        [ 'Documentation', 'sifency-documentation', 'sifency_addons_render_documentation_page' ],
    ];

    if ( ! class_exists( 'Sifency_Core' ) ) {
        $submenus[] = [ 'Themes', 'sifency-themes', 'sifency_addons_render_themes_page' ];
        $submenus[] = [ 'Data Suite', 'sifency-data-suite', 'sifency_addons_render_data_suite_page' ];
        $submenus[] = [ 'Why Pro?', 'sifency-why-pro', 'sifency_addons_render_why_pro_page' ];
        $submenus[] = [ 'Activate', 'activation', 'sifency_addons_render_activate_page' ];
    }

    foreach ( $submenus as $submenu ) {
        add_submenu_page(
            'sifency-addons',
            esc_html( $submenu[0], 'sifency-addons' ),
            esc_html( $submenu[0], 'sifency-addons' ),
            'manage_options',
            $submenu[1],
            $submenu[2]
        );
    }

}
function sifency_admin_render_layout( $page_slug, $content_callback ) {

    echo '<div class="sifency-admin-wrapper">';

    if ( is_admin() && class_exists( 'Sifency_Unlock_Notice' ) ) {
        new Sifency_Unlock_Notice();
    }

    $topbar_file = SIFENCY_ADDONS_DIR . 'classes/admin-module/topbar/class-sifency-admin-topbar.php';
    if ( file_exists( $topbar_file ) ) {
        require_once $topbar_file;
        if ( class_exists( 'Sifency_Addons_Admin_Topbar' ) ) {
            $topbar = new Sifency_Addons_Admin_Topbar();
            $topbar->render( $page_slug );
        }
    }

    if ( is_callable( $content_callback ) ) {
        call_user_func( $content_callback );
    } else {
        echo '<p><em>Page content not available.</em></p>';
    }

    echo '</div>';
}

function sifency_addons_render_welcome_page() {
    $welcome_dashboard = SIFENCY_ADDONS_DIR . 'classes/admin-module/admin-dashboard/class-land.php';
    if ( file_exists( $welcome_dashboard ) ) {
        require_once $welcome_dashboard;
    }
    sifency_admin_render_layout( 'setup', function() {
        if ( class_exists( 'Sifency_Welcome_Admin_Dashboard' ) ) {
            $page = new Sifency_Welcome_Admin_Dashboard();
            $page->render_welcome_page();
        }
    });
}

function sifency_addons_render_activate_page() {
    sifency_admin_render_layout( 'activate', function() {
        if ( class_exists( 'Sifency_Addons_Theme_Builder_Master' ) ) {
            Sifency_Addons_Theme_Builder_Master::instance()->render_license_activation_form();
        }
    });
}

/**
 * Renders the "Widgets" page (page=sifency-addons-widgets).
 */
function sifency_addons_render_addons_widgets_page() {
    sifency_admin_render_layout( 'widgets', function() {
        $admin_manager = new Sifency_Admin_Page_Widgets_List();
        $admin_manager->render_admin_page();
    });
}

function sifency_addons_render_theme_builder_page() {
    sifency_admin_render_layout( 'theme-builder', function() {
        if ( class_exists( 'Sifency_Theme_Builder_Admin' ) ) {
            $admin = new Sifency_Theme_Builder_Admin();
            $admin->render_admin_page();
        }
    });
}

function sifency_addons_render_templates_shortcode_page() {
    $section_template_builder = SIFENCY_ADDONS_DIR . 'classes/template-shortcode/class-template.php';
    if ( file_exists( $section_template_builder ) ) {
        require_once $section_template_builder;
    }
    sifency_admin_render_layout( 'template-maker', function() {
        if ( class_exists( 'Sifency_Section_Builder_Admin' ) ) {
            $page = new Sifency_Section_Builder_Admin();
            $page->render_admin_page();
        }
    });
}

function sifency_addons_render_prebuild_website_page() {
    $prebuild_theme = SIFENCY_ADDONS_DIR . 'classes/admin-module/prebuild/init.php';
    if ( file_exists( $prebuild_theme ) ) {
        require_once $prebuild_theme;
    }
    // Render the shared admin layout
    sifency_admin_render_layout( 'prebuild-website', function() {
        if ( class_exists( 'Sifency_Addons_Prebuild_Website' ) ) {
            $page = new Sifency_Addons_Prebuild_Website();
            $page->render();
        }
    });
}

function sifency_addons_render_themes_page() {
    $theme = SIFENCY_ADDONS_DIR . 'classes/admin-module/theme/class-theme.php';
    if ( file_exists( $theme ) ) {
        require_once $theme;
    }
    sifency_admin_render_layout( 'themes', function() {
        if ( class_exists( 'Sifency_Addons_Admin_Themes' ) ) {
            $page = new Sifency_Addons_Admin_Themes();
            $page->render();
        }
    });
}

function sifency_addons_render_extentions_page() {
    sifency_admin_render_layout( 'extentions', function() {
        if ( class_exists( 'Sifency_Installer_Admin' ) ) {
            $installer_page = Sifency_Installer_Admin::instance();
            $installer_page->render_admin_page();
        }
    });

}

function sifency_addons_render_settings_page() {
    sifency_admin_render_layout( 'settings', function() {
        // Use the static instance method instead of creating a new object
        Sifency_Addons_Admin_Options::instance()->render_settings_page();
    });
}

/**
 * Renders the main "Data Suite" admin page with Import, Export, and Reset tools.
 */
function sifency_addons_render_data_suite_page() {
    sifency_admin_render_layout( 'data_suite', function() {
        if ( class_exists( 'Sifency_Addons_Data_Suite' ) ) {
            $page = new Sifency_Addons_Data_Suite();
            $page->render();
        }
    });
}

/**
 * Render the "Why Pro" admin page
 */
function sifency_addons_render_why_pro_page() {
    $why_pro_file = SIFENCY_ADDONS_DIR . 'classes/admin-module/why-me/why-me.php';
    if ( file_exists( $why_pro_file ) ) {
        require_once $why_pro_file;
    }
    sifency_admin_render_layout( 'PRO', function() {
        if ( class_exists( 'Sifency_Addons_Admin_Why_Pro' ) ) {
            $page = new Sifency_Addons_Admin_Why_Pro();
            $page->render();
        }
    });
}

/**
 * Renders the Documentation page with help center and guides
 */
function sifency_addons_render_documentation_page() {
    sifency_admin_render_layout( 'documentation', function() {
        if ( class_exists( 'Sifency_Addons_Documentation' ) ) {
            Sifency_Addons_Documentation::render_page();
        } else {
            echo '<p><em>' . esc_html__( 'Documentation class not found.', 'sifency-addons' ) . '</em></p>';
        }
    });
}

/**
 * Renders the Analytics Dashboard page
 */
function sifency_addons_render_analytics_page() {
    sifency_admin_render_layout( 'analytics', function() {
        if ( class_exists( 'Sifency_Addons_Widget_Analytics' ) ) {
            Sifency_Addons_Widget_Analytics::render_dashboard();
        } else {
            echo '<p><em>' . esc_html__( 'Analytics class not found.', 'sifency-addons' ) . '</em></p>';
        }
    });
}
