<?php

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

class AIEO_SSO_Buttons_Data {

    const OPTION_KEY = 'aieo_social_login_settings';

    
    private static $cache = null;

    
    public static function get_settings() {
        if (self::$cache !== null) { return self::$cache; }
        $opt = get_option(self::OPTION_KEY, array());
        if (!is_array($opt)) { $opt = array(); }
        $defaults = array(
            'providers' => array(),
            'intents'   => array(
                'login'      => true,
                'register'   => true,
                'checkout'   => true,
                'my_account' => true,
                'newsletter' => false,
            ),
            'force_ssl_callback' => false,
            'intro_text'         => '',
        );
        self::$cache = array_replace_recursive($defaults, $opt);
        return self::$cache;
    }

    
    public static function save_settings(array $value) {
        self::$cache = null;
        return update_option(self::OPTION_KEY, $value, false);
    }

    
    public static function enabled_providers_for_intent($intent) {
        $settings = self::get_settings();
        $intents  = is_array($settings['intents']) ? $settings['intents'] : array();
        
        
        
        $settings_key = ($intent === 'subscribe') ? 'newsletter' : $intent;
        if (empty($intents[$settings_key])) { return array(); }

        $out = array();
        $order = array('google', 'facebook', 'apple', 'linkedin', 'twitter', 'github', 'microsoft', 'magic_link');
        foreach ($order as $code) {
            $row = isset($settings['providers'][$code]) ? $settings['providers'][$code] : array();
            if (empty($row['enabled']))      { continue; }
            
            
            if ($code !== 'magic_link') {
                if (empty($row['client_id']))    { continue; }
                if (empty($row['client_secret'])) { continue; }
            }
            $button_text = isset($row['button_text']) && (string) $row['button_text'] !== ''
                ? (string) $row['button_text']
                : self::default_button_text($code);
            $entry = array(
                'code'        => $code,
                'label'       => self::default_label($code),
                'button_text' => $button_text,
                'start_url'   => add_query_arg(array(
                    'intent' => $intent,
                ), rest_url('aieo/v1/social-login/' . $code . '/start')),
            );
            if ($code === 'magic_link') {
                
                
                
                
                
                
                $entry['start_url'] = '#aieo-magic-link';
                $entry['modal']     = true;
            }
            $out[] = $entry;
        }
        return $out;
    }

    public static function default_label($code) {
        $map = array(
            'google'     => 'Google',
            'facebook'   => 'Facebook',
            'apple'      => 'Apple',
            'linkedin'   => 'LinkedIn',
            'twitter'    => 'X.com',
            'github'     => 'GitHub',
            'microsoft'  => 'Microsoft',
            'magic_link' => 'Magic Link',
        );
        return isset($map[$code]) ? $map[$code] : ucfirst($code);
    }

    public static function default_button_text($code) {
        
        $map = array(
            'google'     => 'Συνδεθείτε με Google',
            'facebook'   => 'Συνδεθείτε με Facebook',
            'apple'      => 'Συνδεθείτε με Apple',
            'linkedin'   => 'Συνδεθείτε με LinkedIn',
            'twitter'    => 'Συνδεθείτε με X',
            'github'     => 'Συνδεθείτε με GitHub',
            'microsoft'  => 'Συνδεθείτε με Microsoft',
            'magic_link' => 'Σύνδεση με email',
        );
        return isset($map[$code]) ? $map[$code] : ('Sign in with ' . self::default_label($code));
    }

    
    public static function callback_url($provider) {
        $settings = self::get_settings();
        $url = rest_url('aieo/v1/social-login/' . $provider . '/callback');
        if (!empty($settings['force_ssl_callback']) && stripos($url, 'http://') === 0) {
            $url = 'https://' . substr($url, 7);
        }
        return $url;
    }
}
