<?php

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

add_action('admin_enqueue_scripts', 'pmrcgold_inits');

function pmrcgold_inits()
{
    wp_enqueue_style(
        'Admin-style-Gold',
        PMRCGOLD_ASSETS_URL . 'css/admin-style-gold.css',
        array(),
        PRECIOUSUITE_MRC_VERSION
    );
    wp_enqueue_style(
        'ptgold-responsive',
        PMRCGOLD_ASSETS_URL . 'css/responsive.css',
        array('Admin-style-Gold'),
        PRECIOUSUITE_MRC_VERSION
    );
}

add_filter('plugin_row_meta', 'pmrcgold_plugin_row_meta', 10, 2);

function pmrcgold_plugin_row_meta($links, $file)
{
    if ('preciousuite-metal-rate-charts/preciousuite-metal-rate-charts.php' == $file) {
        $row_meta = array(
            'docs' => '<a href="' . esc_url('https://www.preciousuite.com') . '" target="_blank" aria-label="' . esc_attr__('Plugin Additional Links', 'preciousuite-metal-rate-charts') . '" style="color:green;">' . esc_html__('Documentation', 'preciousuite-metal-rate-charts') . '</a>'
        );

        return array_merge($links, $row_meta);
    }
    return (array) $links;
}

// ============================================================
// MENU REGISTRATION
// ============================================================

add_action('admin_menu', 'pmrcgold_menu', 40);

function pmrcgold_menu()
{
    // Main submenu for Metal Rate Charts under Preciousuite
    add_submenu_page(
        'preciousuite',
        __('Gold Price Widgets', 'preciousuite-metal-rate-charts'),
        __('Gold Price Widgets', 'preciousuite-metal-rate-charts'),
        'manage_options',
        'gold-price-widgets',
        'pmrcgold_WidgetPageMenu'
    );

    // Hidden page: API Settings
    add_submenu_page(
        null, // Hidden from sidebar
        __('API Settings', 'preciousuite-metal-rate-charts'),
        __('API Settings', 'preciousuite-metal-rate-charts'),
        'manage_options',
        'gold-price-api-settings',
        'pmrcgold_ApiSettingsPageMenu'
    );

    // Hidden page: Price Log
    add_submenu_page(
        null, // Hidden from sidebar
        __('Price Log', 'preciousuite-metal-rate-charts'),
        __('Price Log', 'preciousuite-metal-rate-charts'),
        'manage_options',
        'gold-price-logs',
        'pmrcgold_PriceLogPageMenu'
    );
}

function pmrcgold_mask_license_key($key)
{
    $len = strlen($key);
    if ($len <= 4) {
        return str_repeat('*', $len);  // too short, hide all
    }
    $visible_start = substr($key, 0, 2);
    $visible_end = substr($key, -2);
    $masked_middle = str_repeat('*', $len - 4);
    return $visible_start . $masked_middle . $visible_end;
}

require PRECIOUSUITE_MRC_PATH . 'includes/widgets-settings.php';
require PRECIOUSUITE_MRC_PATH . 'includes/shortcode.php';
require PRECIOUSUITE_MRC_PATH . 'includes/api-settings.php';
require PRECIOUSUITE_MRC_PATH . 'includes/price-log.php';

function pmrcgold_render_dashboard_nav($active_page)
{
    ?>
    <aside class="preciousuite-dashboard-nav">
        <div class="preciousuite-dashboard-brand plugin-dashboard-brand">
            <div class="preciousuite-page-brand">
                <div class="preciousuite-box preciousuite-logo">
                    <a href="https://www.preciousuite.com" target="_blank" rel="noopener noreferrer"
                        class="preciousuite-page-title">
                        <img src="<?php echo esc_url(PMRCGOLD_ASSETS_URL . 'uploads/Preciousuite_Logo.png'); ?>"
                            alt="Preciousuite Logo" style="max-width: 150px;">
                    </a>
                </div>
            </div>
        </div>
        <nav class="preciousuite-dashboard-nav-inner">
            <div class="preciousuite-nav-links">
                <a class="<?php echo $active_page === 'gold-price-widgets' ? 'is-active' : ''; ?>"
                    href="<?php echo esc_url(admin_url('admin.php?page=gold-price-widgets')); ?>">
                    <span class="dashicons dashicons-admin-generic"
                        style="margin-right: .75rem; align-self: center;"></span>
                    <?php esc_html_e('Widget Settings', 'preciousuite-metal-rate-charts'); ?>
                </a>
                <a class="<?php echo $active_page === 'gold-price-api-settings' ? 'is-active' : ''; ?>"
                    href="<?php echo esc_url(admin_url('admin.php?page=gold-price-api-settings')); ?>">
                    <span class="dashicons dashicons-admin-network"
                        style="margin-right: .75rem; align-self: center;"></span>
                    <?php esc_html_e('API Settings', 'preciousuite-metal-rate-charts'); ?>
                </a>
                <a class="<?php echo $active_page === 'gold-price-logs' ? 'is-active' : ''; ?>"
                    href="<?php echo esc_url(admin_url('admin.php?page=gold-price-logs')); ?>">
                    <span class="dashicons dashicons-list-view" style="margin-right: .75rem; align-self: center;"></span>
                    <?php esc_html_e('Price Log', 'preciousuite-metal-rate-charts'); ?>
                </a>
            </div>
            <div class="preciousuite-sidebar-help-card"
                style="margin-top:20px; padding: 15px; background: #f8fafc; border-radius: 8px;">
                <strong>Need help?</strong>
                <p style="font-size:13px; color:#64748b; margin-top:5px;">Check our documentation or contact support.</p>
                <a href="https://wordpress.org/support/plugin/preciousuite-metal-rate-charts/" target="_blank"
                    style="color:#2563eb; font-size:13px; font-weight:600; text-decoration:none;">Visit Support &rarr;</a>
            </div>
        </nav>
    </aside>
    <?php
}

function pmrcgold_render_dashboard_sidebar()
{
    ?>
    <aside class="preciousuite-dashboard-sidebar preciousuite-page-right-sidebar">
        <section class="preciousuite-sidebar-card preciousuite-pro-card">
            <h2>
                <?php esc_html_e('Preciousuite Pro Bundle', 'preciousuite-metal-rate-charts'); ?>
                <svg width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="#4f46e5" stroke-width="2"
                    stroke-linecap="round" stroke-linejoin="round">
                    <path d="M6 3h12l4 6-10 12L2 9l4-6z"></path>
                    <path d="M2 9h20"></path>
                    <path d="M12 21L8 9"></path>
                    <path d="M12 21l4-12"></path>
                    <path d="M6 3l2 6"></path>
                    <path d="M18 3l-2 6"></path>
                </svg>
            </h2>
            <p class="plugin-left-side-card-desc">
                <?php esc_html_e('Grow your jewelry store with advanced pricing, premium templates, and priority support.', 'preciousuite-metal-rate-charts'); ?>
            </p>
            <div class="preciousuite-pro-features">
                <div class="preciousuite-pro-feature">
                    <svg width="18" height="18" viewBox="0 0 24 24" fill="#10b981" stroke="#ffffff" stroke-width="2">
                        <circle cx="12" cy="12" r="11" stroke="none" />
                        <path d="M8 12.5l3 3 5-6" />
                    </svg>
                    <span><?php esc_html_e('Advanced product pricing workflows', 'preciousuite-metal-rate-charts'); ?></span>
                </div>
                <div class="preciousuite-pro-feature">
                    <svg width="18" height="18" viewBox="0 0 24 24" fill="#10b981" stroke="#ffffff" stroke-width="2">
                        <circle cx="12" cy="12" r="11" stroke="none" />
                        <path d="M8 12.5l3 3 5-6" />
                    </svg>
                    <span><?php esc_html_e('Professional invoice and certificate templates', 'preciousuite-metal-rate-charts'); ?></span>
                </div>
                <div class="preciousuite-pro-feature">
                    <svg width="18" height="18" viewBox="0 0 24 24" fill="#10b981" stroke="#ffffff" stroke-width="2">
                        <circle cx="12" cy="12" r="11" stroke="none" />
                        <path d="M8 12.5l3 3 5-6" />
                    </svg>
                    <span><?php esc_html_e('Priority support for setup questions', 'preciousuite-metal-rate-charts'); ?></span>
                </div>
            </div>
            <a class="button button-primary" href="https://preciousuite.com/products"
                target="_blank"><?php esc_html_e('View Upgrade', 'preciousuite-metal-rate-charts'); ?></a>
            <small><?php esc_html_e('Optional bundle features. No forced upsells.', 'preciousuite-metal-rate-charts'); ?></small>
        </section>

        <section class="preciousuite-sidebar-card preciousuite-quick-actions">
            <h2>
                <svg width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="#4f46e5" stroke-width="2"
                    stroke-linecap="round" stroke-linejoin="round">
                    <path d="M4 19.5A2.5 2.5 0 0 1 6.5 17H20"></path>
                    <path d="M6.5 2H20v20H6.5A2.5 2.5 0 0 1 4 19.5v-15A2.5 2.5 0 0 1 6.5 2z"></path>
                </svg>
                <?php esc_html_e('Quick Actions', 'preciousuite-metal-rate-charts'); ?>
            </h2>
            <div class="preciousuite-action-list">
                <a href="<?php echo esc_url(admin_url('admin.php?page=gold-price-widgets')); ?>">
                    <svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"
                        stroke-linecap="round" stroke-linejoin="round">
                        <rect x="3" y="3" width="18" height="18" rx="2" ry="2"></rect>
                        <line x1="3" y1="9" x2="21" y2="9"></line>
                        <line x1="9" y1="21" x2="9" y2="9"></line>
                    </svg>
                    <?php esc_html_e('Widget Settings', 'preciousuite-metal-rate-charts'); ?>
                </a>
                <a href="<?php echo esc_url(admin_url('admin.php?page=preciousuite-support')); ?>">
                    <svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"
                        stroke-linecap="round" stroke-linejoin="round">
                        <path d="M18 8A6 6 0 0 0 6 8c0 7-3 9-3 9h18s-3-2-3-9"></path>
                        <path d="M13.73 21a2 2 0 0 1-3.46 0"></path>
                    </svg>
                    <?php esc_html_e('Support', 'preciousuite-metal-rate-charts'); ?>
                </a>
                <a href="https://wordpress.org/support/plugin/preciousuite-metal-rate-charts/reviews/#new-post"
                    target="_blank" rel="noopener noreferrer">
                    <svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"
                        stroke-linecap="round" stroke-linejoin="round">
                        <polygon
                            points="12 2 15.09 8.26 22 9.27 17 14.14 18.18 21.02 12 17.77 5.82 21.02 7 14.14 2 9.27 8.91 8.26 12 2">
                        </polygon>
                    </svg>
                    <?php esc_html_e('Leave Review', 'preciousuite-metal-rate-charts'); ?>
                </a>
            </div>
        </section>

        <section class="preciousuite-sidebar-card preciousuite-learn-card">
            <h2>
                <svg width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="#4f46e5" stroke-width="2"
                    stroke-linecap="round" stroke-linejoin="round">
                    <path d="M2 3h6a4 4 0 0 1 4 4v14a3 3 0 0 0-3-3H2z"></path>
                    <path d="M22 3h-6a4 4 0 0 0-4 4v14a3 3 0 0 1 3-3h7z"></path>
                </svg>
                <?php esc_html_e('Learn Widgets', 'preciousuite-metal-rate-charts'); ?>
            </h2>
            <p><?php esc_html_e('Configure your gold price widgets and place them anywhere on your site using shortcodes.', 'preciousuite-metal-rate-charts'); ?>
            </p>
            <a href="https://preciousuite.com/docs/" target="_blank"
                class="preciousuite-link"><?php esc_html_e('View documentation', 'preciousuite-metal-rate-charts'); ?>
                &rarr;</a>
        </section>
    </aside>
    <?php
}
