<?php
/**
 * Admin Menu Setup
 *
 * Registers the plugin's menu item in the WordPress admin sidebar.
 * Uses add_menu_page() to create a top-level menu entry.
 */

// Prevent direct access
if ( ! defined( 'ABSPATH' ) ) {
    exit;
}

/**
 * Add settings page to admin menu
 */
function canvon_fb_add_settings_page() {
    // Load custom menu icon SVG
    $canvon_fb_icon_path = plugin_dir_path( dirname( __FILE__ ) ) . 'assets/images/admin-menu-icon.svg';
    $canvon_fb_icon_svg = file_exists( $canvon_fb_icon_path ) ? file_get_contents( $canvon_fb_icon_path ) : '';

    // Convert SVG to base64 data URI for WordPress admin menu
    $canvon_fb_menu_icon = !empty( $canvon_fb_icon_svg )
        ? 'data:image/svg+xml;base64,' . base64_encode( $canvon_fb_icon_svg )
        : 'dashicons-admin-generic'; // Fallback to dashicon

    // Main menu
    add_menu_page(
        'Canvon Floating Buttons Settings',  // Page title
        'Canvon Floating Buttons',            // Menu title
        'manage_options',                 // Capability required
        'canvon-floating-buttons',          // Menu slug
        'canvon_fb_render_settings_page',       // Callback function
        $canvon_fb_menu_icon                        // Icon (custom SVG or fallback)
    );

    // Appearance submenu
    add_submenu_page(
        'canvon-floating-buttons',          // Parent slug
        'Appearance Settings',            // Page title
        'Appearance',                     // Menu title
        'manage_options',                 // Capability
        'canvon-floating-buttons-appearance', // Menu slug
        'canvon_fb_render_appearance_page'      // Callback function
    );
}
add_action( 'admin_menu', 'canvon_fb_add_settings_page' );

/**
 * Render the appearance settings page
 */
function canvon_fb_render_appearance_page() {
    if ( ! current_user_can( 'manage_options' ) ) {
        return;
    }
    require_once plugin_dir_path( dirname( __FILE__ ) ) . 'admin/views/appearance-page.php';
}
