<?php
/**
 * Admin menu registration.
 *
 * @package PrimeLink
 */

namespace PrimeLink\Core\Admin;

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

final class Menu {

    public static function register() {
        add_action( 'admin_menu', array( __CLASS__, 'add_menus' ) );
    }

    public static function add_menus() {
        $cap = 'manage_options';
        $slug = 'mt-primelink';

        add_menu_page(
            __( 'PrimeLink', 'primelink' ),
            __( 'PrimeLink', 'primelink' ),
            $cap,
            $slug,
            array( __CLASS__, 'render_dashboard' ),
            'dashicons-database-import',
            56
        );

        add_submenu_page( $slug, __( 'Dashboard', 'primelink' ), __( 'Dashboard', 'primelink' ), $cap, $slug, array( __CLASS__, 'render_dashboard' ) );
        add_submenu_page( $slug, __( 'New Import', 'primelink' ), __( 'New Import', 'primelink' ), $cap, 'mt-primelink-new-import', array( __CLASS__, 'render_new_import' ) );
        add_submenu_page( $slug, __( 'Manage Import', 'primelink' ), __( 'Manage Import', 'primelink' ), $cap, 'mt-primelink-manage-import', array( __CLASS__, 'render_manage_import' ) );
        add_submenu_page( $slug, __( 'New Export', 'primelink' ), __( 'New Export', 'primelink' ), $cap, 'mt-primelink-new-export', array( __CLASS__, 'render_new_export' ) );
        add_submenu_page( $slug, __( 'Manage Exports', 'primelink' ), __( 'Manage Exports', 'primelink' ), $cap, 'mt-primelink-manage-export', array( __CLASS__, 'render_manage_export' ) );
        add_submenu_page( $slug, __( 'Template Manager', 'primelink' ), __( 'Template Manager', 'primelink' ), $cap, 'mt-primelink-template-manager', array( __CLASS__, 'render_template_manager' ) );
        add_submenu_page( $slug, __( 'Live Feed', 'primelink' ), __( 'Live Feed', 'primelink' ), $cap, 'mt-primelink-live-feed', array( __CLASS__, 'render_live_feed' ) );
        add_submenu_page( $slug, __( 'Statistics & Analytics', 'primelink' ), __( 'Statistics & Analytics', 'primelink' ), $cap, 'mt-primelink-statistics', array( __CLASS__, 'render_statistics' ) );
        add_submenu_page( $slug, __( 'Settings', 'primelink' ), __( 'Settings', 'primelink' ), $cap, 'mt-primelink-settings', array( __CLASS__, 'render_settings' ) );
        add_submenu_page( $slug, __( 'System Status', 'primelink' ), __( 'System Status', 'primelink' ), $cap, 'mt-primelink-system-status', array( __CLASS__, 'render_system_status' ) );
    }

    private static function view( $template ) {
        $file = MT_PRIMELINK_PATH . 'templates/admin/' . $template . '.php';
        if ( file_exists( $file ) ) {
            include $file;
        } else {
            echo '<div class="wrap"><h1>Template missing: ' . esc_html( $template ) . '</h1></div>';
        }
    }

    public static function render_dashboard()      { self::view( 'dashboard/dashboard' ); }
    public static function render_new_import()     { self::view( 'imports/new-import-wizard' ); }
    public static function render_manage_import()  { self::view( 'imports/manage-imports' ); }
    public static function render_new_export() {
        // Check if export_type is specified, if not show selection page
        $export_type = isset( $_GET['export_type'] ) ? sanitize_text_field( $_GET['export_type'] ) : '';
        if ( empty( $export_type ) ) {
            self::view( 'exports/new-export-select' );
        } else {
            self::view( 'exports/new-export-wizard' );
        }
    }
    public static function render_manage_export()  { self::view( 'exports/manage-exports' ); }
    public static function render_template_manager() { self::view( 'templates/template-manager' ); }
    public static function render_live_feed()      { self::view( 'live-feed/live-feed' ); }
    public static function render_statistics()     { self::view( 'statistics/statistics' ); }
    public static function render_settings()       { self::view( 'settings/settings' ); }
    public static function render_system_status()  { self::view( 'system-status/system-status' ); }
}