<?php

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

class AIEO_SSO_Buttons {

    
    public static function render($intent = 'login', array $args = array()) {
        
        
        
        if (!class_exists('AIEO_SSO_Buttons_Data')) { return ''; }

        $providers = AIEO_SSO_Buttons_Data::enabled_providers_for_intent($intent);
        if (empty($providers)) { return ''; }

        $defaults = array(
            'layout'  => 'auto',
            'intro'   => '',
            'compact' => false,
        );
        $args = array_merge($defaults, $args);
        if ($args['layout'] === 'auto') {
            $args['layout'] = (count($providers) > 2) ? 'stack' : 'inline';
        }
        if ($args['intro'] === '') {
            $settings    = AIEO_SSO_Buttons_Data::get_settings();
            $args['intro'] = (string) ($settings['intro_text'] ?? '');
        }

        $compact_cls = $args['compact'] ? ' aieo-sso-buttons--compact' : '';
        $layout_cls  = ' aieo-sso-buttons--' . sanitize_html_class($args['layout']);

        
        
        $redirect_to = self::current_redirect_to();

        ob_start();
        
        
        
        
        
        self::print_styles_once();
        ?>
        <div class="aieo-sso-buttons<?php echo esc_attr($layout_cls . $compact_cls); ?>" data-aieo-sso-intent="<?php echo esc_attr($intent); ?>">
            <?php if ($args['intro'] !== ''): ?>
                <p class="aieo-sso-buttons__intro"><?php echo wp_kses_post($args['intro']); ?></p>
            <?php endif; ?>
            <div class="aieo-sso-buttons__row">
                <?php foreach ($providers as $p):
                    
                    
                    
                    
                    
                    
                    if (!empty($p['modal']) && $p['code'] === 'magic_link') {
                        $href      = '#aieo-magic-link';
                        $extra_role = 'role="button"';
                    } else {
                        $href = add_query_arg('redirect_to', rawurlencode($redirect_to), $p['start_url']);
                        $extra_role = '';
                    }
                ?>
                    <a class="aieo-sso-btn aieo-sso-btn--<?php echo esc_attr($p['code']); ?>"
                       href="<?php echo esc_url($href); ?>"
                       data-provider="<?php echo esc_attr($p['code']); ?>"
                       <?php echo $extra_role; ?>>
                        <span class="aieo-sso-btn__label"><?php echo esc_html($p['button_text']); ?></span>
                        <span class="aieo-sso-btn__icon" aria-hidden="true"><?php echo self::icon_svg($p['code']); ?></span>
                    </a>
                <?php endforeach; ?>
            </div>
        </div>
        <?php
        return (string) ob_get_clean();
    }

    
    private static function print_styles_once() {
        static $printed = false;
        if ($printed) { return; }
        $printed = true;
        ?>
<style id="aieo-sso-buttons-css">
.aieo-sso-buttons{margin:14px 0;font-family:inherit}
.aieo-sso-buttons__intro{margin:0 0 10px;color:#4b5563;font-size:13px;line-height:1.5}
.aieo-sso-buttons__row{display:flex;gap:10px;flex-wrap:wrap}
.aieo-sso-buttons--inline .aieo-sso-buttons__row{flex-direction:row}
.aieo-sso-buttons--stack  .aieo-sso-buttons__row{flex-direction:column}
.aieo-sso-buttons--stack  .aieo-sso-btn{width:100%}
.aieo-sso-buttons--inline .aieo-sso-btn{flex:1 1 auto;min-width:160px}

.aieo-sso-btn{
  display:inline-flex;align-items:center;justify-content:space-between;
  gap:10px;padding:11px 16px;border-radius:999px;border:1px solid transparent;
  text-decoration:none;font-weight:600;font-size:13px;letter-spacing:.3px;
  text-transform:uppercase;line-height:1;color:#fff;
  transition:transform .12s ease, box-shadow .12s ease, opacity .12s ease;
  cursor:pointer;
}
.aieo-sso-btn:hover{transform:translateY(-1px);box-shadow:0 6px 14px rgba(0,0,0,.12);opacity:.96}
.aieo-sso-btn:focus-visible{outline:2px solid #2563eb;outline-offset:2px}
.aieo-sso-btn__label{white-space:nowrap;overflow:hidden;text-overflow:ellipsis}
.aieo-sso-btn__icon{display:inline-flex;width:22px;height:22px;flex:0 0 22px;align-items:center;justify-content:center;background:#fff;border-radius:50%}
.aieo-sso-btn__icon svg{width:14px;height:14px}

.aieo-sso-btn--facebook { background:#1877F2; color:#fff }
.aieo-sso-btn--google   { background:linear-gradient(135deg,#bcdbff 0%,#a7c7ed 100%); color:#1f2937 }
.aieo-sso-btn--apple    { background:#000; color:#fff }
.aieo-sso-btn--linkedin { background:#0a66c2; color:#fff }
.aieo-sso-btn--twitter  { background:#000; color:#fff }
.aieo-sso-btn--github   { background:#24292f; color:#fff }
.aieo-sso-btn--microsoft{ background:#2f2f2f; color:#fff }
.aieo-sso-btn--magic_link { background:#f3f4f6; color:#111827; border:1px solid #d1d5db }
.aieo-sso-btn--magic_link:hover { background:#e5e7eb }
.aieo-sso-btn--magic_link .aieo-sso-btn__icon { background:#1f2937 }
.aieo-sso-btn--magic_link .aieo-sso-btn__icon svg { fill:#fff !important }

/* Magic-link modal */
.aieo-ml-overlay{
  position:fixed;inset:0;background:rgba(15,23,42,.55);
  display:none;align-items:center;justify-content:center;
  z-index:99999;padding:16px;
}
.aieo-ml-overlay.is-open{display:flex}
.aieo-ml-dialog{
  background:#fff;border-radius:12px;max-width:420px;width:100%;
  box-shadow:0 20px 60px rgba(0,0,0,.25);padding:24px 24px 20px;
  position:relative;font-family:inherit;color:#1f2937;
}
.aieo-ml-close{
  position:absolute;top:8px;right:10px;background:none;border:0;
  font-size:24px;line-height:1;color:#6b7280;cursor:pointer;
  padding:4px 8px;border-radius:6px;
}
.aieo-ml-close:hover{background:#f3f4f6;color:#111827}
.aieo-ml-title{margin:0 0 8px;font-size:18px;font-weight:600;color:#111827}
.aieo-ml-intro{margin:0 0 16px;font-size:14px;line-height:1.55;color:#4b5563}
.aieo-ml-form{display:flex;flex-direction:column;gap:8px}
.aieo-ml-label{font-size:12px;color:#6b7280;font-weight:600}
.aieo-ml-input{
  width:100%;padding:11px 12px;font-size:14px;line-height:1.4;
  border:1px solid #d1d5db;border-radius:8px;color:#111827;background:#fff;
}
.aieo-ml-input:focus{outline:2px solid #2563eb;outline-offset:1px;border-color:#2563eb}
.aieo-ml-submit{
  margin-top:6px;padding:11px 18px;font-size:14px;font-weight:600;
  background:#1f2937;color:#fff;border:0;border-radius:999px;
  cursor:pointer;letter-spacing:.3px;
}
.aieo-ml-submit:hover{background:#111827}
.aieo-ml-submit:disabled{opacity:.6;cursor:not-allowed}
.aieo-ml-status{margin-top:12px;font-size:13px;line-height:1.5;min-height:18px}
.aieo-ml-status--info{color:#4b5563}
.aieo-ml-status--success{color:#166534;background:#dcfce7;padding:10px 12px;border-radius:8px}
.aieo-ml-status--error{color:#991b1b;background:#fee2e2;padding:10px 12px;border-radius:8px}

.aieo-sso-buttons--compact .aieo-sso-btn{padding:8px 12px;font-size:12px}
.aieo-sso-buttons--compact .aieo-sso-btn__icon{width:18px;height:18px;flex-basis:18px}
.aieo-sso-buttons--compact .aieo-sso-btn__icon svg{width:11px;height:11px}

@media (max-width:480px){
  .aieo-sso-buttons--inline .aieo-sso-buttons__row{flex-direction:column}
  .aieo-sso-buttons--inline .aieo-sso-btn{width:100%}
}
</style>
        <?php
    }

    
    private static function icon_svg($code) {
        switch ($code) {
            case 'facebook':
                return '<svg viewBox="0 0 24 24" fill="#1877F2"><path d="M22 12a10 10 0 10-11.6 9.9v-7H8v-3h2.4V9.7c0-2.4 1.4-3.7 3.5-3.7 1 0 2.1.2 2.1.2v2.3h-1.2c-1.2 0-1.5.7-1.5 1.5V12h2.6l-.4 3h-2.2v7A10 10 0 0022 12z"/></svg>';
            case 'google':
                return '<svg viewBox="0 0 24 24"><path fill="#4285F4" d="M22 12a10 10 0 00-.2-1.9H12v3.7h5.6a4.8 4.8 0 01-2 3.1v2.6h3.2A9.7 9.7 0 0022 12z"/><path fill="#34A853" d="M12 22a9.5 9.5 0 006.7-2.5l-3.2-2.6a6 6 0 01-9-3.1H3.2v2.7A10 10 0 0012 22z"/><path fill="#FBBC05" d="M6.5 13.8a6 6 0 010-3.6V7.5H3.2a10 10 0 000 9l3.3-2.7z"/><path fill="#EA4335" d="M12 6a5.4 5.4 0 013.8 1.5l2.9-2.9A9.7 9.7 0 0012 2a10 10 0 00-8.8 5.5l3.3 2.7A6 6 0 0112 6z"/></svg>';
            case 'apple':
                return '<svg viewBox="0 0 24 24" fill="#000"><path d="M16.4 12.7c0-2.4 2-3.5 2-3.6-1.1-1.5-2.7-1.7-3.3-1.8-1.4-.1-2.7.8-3.4.8-.7 0-1.8-.8-3-.8-1.5 0-2.9.9-3.7 2.3-1.6 2.7-.4 6.7 1.1 8.9.8 1.1 1.7 2.2 2.9 2.2 1.2-.1 1.7-.8 3.1-.8 1.5 0 1.9.7 3.2.7 1.3 0 2.1-1.1 2.9-2.1.9-1.2 1.3-2.4 1.3-2.4 0-.1-2.5-1-2.5-3.4M14.3 5.7c.7-.8 1.2-2 1.1-3-1 0-2.3.7-3 1.5-.6.7-1.2 1.9-1 3 1.1 0 2.3-.6 3-1.5"/></svg>';
            case 'linkedin':
                return '<svg viewBox="0 0 24 24" fill="#0a66c2"><path d="M19 3H5a2 2 0 00-2 2v14a2 2 0 002 2h14a2 2 0 002-2V5a2 2 0 00-2-2zM8.4 18H5.7V9.7h2.7V18zM7 8.5a1.6 1.6 0 110-3.1 1.6 1.6 0 010 3.1zM18.3 18h-2.7v-4.5c0-1.1-.4-1.8-1.4-1.8-.7 0-1.2.5-1.4 1-.1.2-.1.5-.1.7V18h-2.7s0-7.5 0-8.3h2.7v1.2c.4-.6 1-1.4 2.4-1.4 1.7 0 3.1 1.1 3.1 3.6V18z"/></svg>';
            case 'twitter':
                return '<svg viewBox="0 0 24 24" fill="#000"><path d="M14.3 10.7L21.7 2h-1.8l-6.4 7.5L8.4 2H2.5l7.7 11.3L2.5 22h1.8l6.7-7.9 5.4 7.9h5.9l-8-11.3zM12 13.4l-.8-1.1L4.9 3.4h2.7L12.7 11l.8 1.1 6.5 9.4h-2.7L12 13.4z"/></svg>';
            case 'github':
                return '<svg viewBox="0 0 24 24" fill="#24292f"><path d="M12 2a10 10 0 00-3.2 19.5c.5.1.7-.2.7-.5v-1.7c-2.8.6-3.4-1.4-3.4-1.4-.5-1.2-1.1-1.5-1.1-1.5-1-.6.1-.6.1-.6 1 .1 1.6 1 1.6 1 .9 1.6 2.4 1.1 3 .8.1-.7.4-1.1.7-1.4-2.2-.3-4.6-1.1-4.6-5 0-1.1.4-2 1-2.7-.1-.3-.4-1.3.1-2.7 0 0 .8-.3 2.7 1a9.4 9.4 0 015 0c1.9-1.3 2.7-1 2.7-1 .5 1.4.2 2.4.1 2.7.6.7 1 1.6 1 2.7 0 3.9-2.4 4.7-4.6 5 .4.3.7 1 .7 1.9v2.8c0 .3.2.6.7.5A10 10 0 0012 2z"/></svg>';
            case 'microsoft':
                return '<svg viewBox="0 0 24 24"><rect x="2" y="2" width="9" height="9" fill="#F25022"/><rect x="13" y="2" width="9" height="9" fill="#7FBA00"/><rect x="2" y="13" width="9" height="9" fill="#00A4EF"/><rect x="13" y="13" width="9" height="9" fill="#FFB900"/></svg>';
            case 'magic_link':
                
                
                
                return '<svg viewBox="0 0 24 24" fill="#1f2937"><path d="M3 6.5A2.5 2.5 0 015.5 4h13A2.5 2.5 0 0121 6.5v8a2.5 2.5 0 01-2.5 2.5H12l.6 1.4-1 .4-.7-1.8H5.5A2.5 2.5 0 013 14.5v-8zm2.5-.5a.5.5 0 00-.5.5v.3l7 4.7 7-4.7v-.3a.5.5 0 00-.5-.5h-13zm14 2.7l-6.6 4.4a1 1 0 01-1.1 0L5 8.7v5.8c0 .3.2.5.5.5h13a.5.5 0 00.5-.5V8.7zM18 18l1.4 3.2L21 19.6 18 18z"/></svg>';
        }
        return '';
    }

    
    private static function current_redirect_to() {
        if (function_exists('wc_get_page_permalink') && function_exists('is_account_page') && is_account_page()) {
            return wc_get_page_permalink('myaccount');
        }
        if (function_exists('wc_get_checkout_url') && function_exists('is_checkout') && is_checkout()) {
            return wc_get_checkout_url();
        }
        
        $ref = wp_get_referer();
        return $ref ? esc_url_raw($ref) : home_url('/');
    }
}
