<?php
// Register the admin post action handlers early
add_action('admin_init', function() {
    add_action('admin_post_nopriv_ri_theme_notice', 'dacdb_ri_theme_notice_content');
    add_action('admin_post_ri_theme_notice', 'dacdb_ri_theme_notice_content');
});

function dacdb_riThemeLogo($atts)
{
    // Enqueue styles and modal assets
    wp_enqueue_style('show-riThemeLogo', MMPDB_PLUGIN_URL . 'css/show-riThemeLogo.css', array(), MMPDB_PLUGIN_VERSION, 'all');
    MMPDB_Modal::enqueue_assets();

    // Handle width attribute for placeholder
    $width = isset($atts['width']) ? $atts['width'] : '100%';
    if (is_numeric($width)) {
        $width .= 'px';
    }

    // Handle height attribute for placeholder
    $height = isset($atts['height']) ? $atts['height'] : 'auto';
    if (is_numeric($height)) {
        $height .= 'px';
    }

    // Create placeholder div with same dimensions
    $html = sprintf(
        '<div class="theme-placeholder" style="width:%s; height:%s;"></div>',
        esc_attr($width),
        esc_attr($height)
    );

    // Show admin notice only to users who can manage options
    if (current_user_can('manage_options')) {
        // Generate a unique ID for this instance
        $notice_id = 'riThemeNotice_' . wp_rand();
        
        // Compact notice with modal trigger
        $notice = sprintf(
            '<div id="%1$s" class="notice notice-warning" style="margin: 10px 0; padding: 8px 12px;">
                <p style="margin: 0;">
                    <span class="dashicons dashicons-warning" style="color: #72aee6;"></span>
                    <strong>Notice:</strong> The RI Presidential Theme Logo shortcode is being decommissioned.
                    <a href="%2$s" class="button button-small modal-link">
                        More Information
                    </a>
                </p>
            </div>',
            esc_attr($notice_id),
            esc_url(admin_url('admin-post.php?action=ri_theme_notice'))
        );

        $html .= $notice;
    }

    return $html;
}

/**
 * Callback to display the modal content
 */
function dacdb_ri_theme_notice_content() {
    // No need for capability check here since this is informational content
    header('Content-Type: text/html; charset=utf-8');
    header('X-Frame-Options: SAMEORIGIN');
    nocache_headers();
    
    ?>
    <!DOCTYPE html>
    <html>
    <head>
        <meta charset="utf-8">
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <title>RI Theme Changes Notice</title>
        <style>
            body { 
                font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Oxygen-Sans, Ubuntu, Cantarell, "Helvetica Neue", sans-serif; 
                padding: 20px; 
                margin: 0; 
                line-height: 1.5;
                background: #fff;
                color: #1d2327;
            }
            h1 { 
                color: #2271b1; 
                margin-top: 0;
                font-size: 1.5em;
            }
            h2 { 
                color: #3c434a; 
                margin-top: 1.5em;
                font-size: 1.2em;
            }
            ul { 
                padding-left: 20px;
                margin: 1em 0;
            }
            a { 
                color: #2271b1; 
                text-decoration: none;
            }
            a:hover { 
                text-decoration: underline;
            }
            p {
                margin: 1em 0;
            }
        </style>
    </head>
    <body>
        <h1>Important Notice: RI Presidential Theme Changes</h1>
        
        <h2>What's Changing?</h2>
        <p>The Rotary International Presidential Theme and Logo are being discontinued starting in Rotary Year 2025-26. 
        This shortcode should be removed from your site.</p>
        
        <h2>What's Next?</h2>
        <p>The RI Board has approved Action Plan-led presidential messages to replace the annual presidential theme.</p>
        
        <h2>Additional Information</h2>
        <ul>
            <li><a href="https://www.rotary7390.org/stories/rotary-presidential-theme-to-be-eliminated-starting-in-2025-2026" 
                target="_blank">Rotary Presidential Theme Elimination Announcement</a></li>
            <li><a href="https://my.rotary.org/en/learning-reference/about-rotary/board-decisions" 
                target="_blank">RI Board Decisions</a></li>
        </ul>
    </body>
    </html>
    <?php
    exit;
}
?>