<?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' ) );
    }

    /**
     * Check if a module is enabled.
     *
     * @param string $module_name Module name (e.g., 'import', 'export').
     * @return bool
     */
    private static function is_module_enabled( $module_name ) {
        $settings = get_option( 'mt_primelink_settings', array() );
        $key = 'module_' . $module_name;
        // Default to enabled (1) for backward compatibility
        return isset( $settings[ $key ] ) ? (bool) $settings[ $key ] : true;
    }

    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' ) );
        
        // Import module
        if ( self::is_module_enabled( 'import' ) ) {
            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' ) );
        }
        
        // Export module
        if ( self::is_module_enabled( 'export' ) ) {
            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' ) );
        }
        
        // Templates module
        if ( self::is_module_enabled( 'templates' ) ) {
            add_submenu_page( $slug, __( 'Template Manager', 'primelink' ), __( 'Template Manager', 'primelink' ), $cap, 'mt-primelink-template-manager', array( __CLASS__, 'render_template_manager' ) );
        }
        
        // Live Feed module
        if ( self::is_module_enabled( 'live_feed' ) ) {
            add_submenu_page( $slug, __( 'Live Feed', 'primelink' ), __( 'Live Feed', 'primelink' ), $cap, 'mt-primelink-live-feed', array( __CLASS__, 'render_live_feed' ) );
        }
        
        // Statistics module
        if ( self::is_module_enabled( 'statistics' ) ) {
            add_submenu_page( $slug, __( 'Statistics & Analytics', 'primelink' ), __( 'Statistics & Analytics', 'primelink' ), $cap, 'mt-primelink-statistics', array( __CLASS__, 'render_statistics' ) );
        }
        
        // Settings is always available
        add_submenu_page( $slug, __( 'Settings', 'primelink' ), __( 'Settings', 'primelink' ), $cap, 'mt-primelink-settings', array( __CLASS__, 'render_settings' ) );
        
        // System Status module
        if ( self::is_module_enabled( 'system_status' ) ) {
            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() {
        if ( ! self::is_module_enabled( 'import' ) ) {
            wp_die( esc_html__( 'Import module is disabled. Please enable it in Settings → Modules.', 'primelink' ), esc_html__( 'Module Disabled', 'primelink' ), array( 'response' => 403 ) );
        }
        self::view( 'imports/new-import-wizard' );
    }
    
    public static function render_manage_import() {
        if ( ! self::is_module_enabled( 'import' ) ) {
            wp_die( esc_html__( 'Import module is disabled. Please enable it in Settings → Modules.', 'primelink' ), esc_html__( 'Module Disabled', 'primelink' ), array( 'response' => 403 ) );
        }
        self::view( 'imports/manage-imports' );
    }
    
    public static function render_new_export() {
        if ( ! self::is_module_enabled( 'export' ) ) {
            wp_die( esc_html__( 'Export module is disabled. Please enable it in Settings → Modules.', 'primelink' ), esc_html__( 'Module Disabled', 'primelink' ), array( 'response' => 403 ) );
        }
        // 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() {
        if ( ! self::is_module_enabled( 'export' ) ) {
            wp_die( esc_html__( 'Export module is disabled. Please enable it in Settings → Modules.', 'primelink' ), esc_html__( 'Module Disabled', 'primelink' ), array( 'response' => 403 ) );
        }
        self::view( 'exports/manage-exports' );
    }
    
    public static function render_template_manager() {
        if ( ! self::is_module_enabled( 'templates' ) ) {
            wp_die( esc_html__( 'Templates module is disabled. Please enable it in Settings → Modules.', 'primelink' ), esc_html__( 'Module Disabled', 'primelink' ), array( 'response' => 403 ) );
        }
        self::view( 'templates/template-manager' );
    }
    
    public static function render_live_feed() {
        if ( ! self::is_module_enabled( 'live_feed' ) ) {
            wp_die( esc_html__( 'Live Feed module is disabled. Please enable it in Settings → Modules.', 'primelink' ), esc_html__( 'Module Disabled', 'primelink' ), array( 'response' => 403 ) );
        }
        self::view( 'live-feed/live-feed' );
    }
    
    public static function render_statistics() {
        if ( ! self::is_module_enabled( 'statistics' ) ) {
            wp_die( esc_html__( 'Statistics module is disabled. Please enable it in Settings → Modules.', 'primelink' ), esc_html__( 'Module Disabled', 'primelink' ), array( 'response' => 403 ) );
        }
        self::view( 'statistics/statistics' );
    }
    
    public static function render_settings() {
        self::view( 'settings/settings' );
    }
    
    public static function render_system_status() {
        if ( ! self::is_module_enabled( 'system_status' ) ) {
            wp_die( esc_html__( 'System Status module is disabled. Please enable it in Settings → Modules.', 'primelink' ), esc_html__( 'Module Disabled', 'primelink' ), array( 'response' => 403 ) );
        }
        self::view( 'system-status/system-status' );
    }
}