<?php
/**
 * Google OAuth Social Login Provider.
 *
 * Implements the social provider interface for Google OAuth 2.0 authentication.
 * Wraps the league/oauth2-google package and normalizes user data.
 *
 * @package GuardDog
 * @since 1.9.30
 */

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

/**
 * Class Guard_Dog_Google_Provider
 *
 * Google OAuth 2.0 social login provider implementation.
 * Uses the league/oauth2-google package for the underlying OAuth flow
 * and normalizes the Google user profile into a standard format.
 *
 * @since 1.9.30
 */
class Guard_Dog_Google_Provider implements Guard_Dog_Social_Provider_Interface {

    /**
     * Get the unique provider identifier.
     *
     * @since 1.9.30
     * @return string Provider identifier.
     */
    public function get_id(): string {
        return 'google';
    }

    /**
     * Get the human-readable provider name.
     *
     * @since 1.9.30
     * @return string Translated provider display name.
     */
    public function get_name(): string {
        return __( 'Google', 'guard-dog' );
    }

    /**
     * Get the Google brand icon as an inline SVG string.
     *
     * Uses the official Google multi-color "G" logo.
     *
     * @since 1.9.30
     * @return string SVG markup for the Google brand icon.
     */
    public function get_icon_svg(): string {
        return '<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 48 48" width="20" height="20">'
            . '<path fill="#EA4335" d="M24 9.5c3.54 0 6.71 1.22 9.21 3.6l6.85-6.85C35.9 2.38 30.47 0 24 0 14.62 0 6.51 5.38 2.56 13.22l7.98 6.19C12.43 13.72 17.74 9.5 24 9.5z"/>'
            . '<path fill="#4285F4" d="M46.98 24.55c0-1.57-.15-3.09-.38-4.55H24v9.02h12.94c-.58 2.96-2.26 5.48-4.78 7.18l7.73 6c4.51-4.18 7.09-10.36 7.09-17.65z"/>'
            . '<path fill="#FBBC05" d="M10.53 28.59a14.5 14.5 0 0 1 0-9.18l-7.98-6.19a24.01 24.01 0 0 0 0 21.56l7.98-6.19z"/>'
            . '<path fill="#34A853" d="M24 48c6.48 0 11.93-2.13 15.89-5.81l-7.73-6c-2.15 1.45-4.92 2.3-8.16 2.3-6.26 0-11.57-4.22-13.47-9.91l-7.98 6.19C6.51 42.62 14.62 48 24 48z"/>'
            . '<path fill="none" d="M0 0h48v48H0z"/>'
            . '</svg>';
    }

    /**
     * Get the Google primary brand color.
     *
     * @since 1.9.30
     * @return string CSS hex color value.
     */
    public function get_color(): string {
        return '#4285F4';
    }

    /**
     * Get the OAuth scopes required for Google authentication.
     *
     * Requests OpenID Connect scopes for identity, email, and profile data.
     *
     * @since 1.9.30
     * @return array List of Google OAuth scope strings.
     */
    public function get_scopes(): array {
        return array( 'openid', 'email', 'profile' );
    }

    /**
     * Create the League OAuth2 Google provider instance.
     *
     * @since 1.9.30
     *
     * @param array $config {
     *     Provider configuration.
     *
     *     @type string $client_id     Google OAuth client ID.
     *     @type string $client_secret Google OAuth client secret.
     *     @type string $redirect_uri  OAuth callback URL.
     * }
     * @return \GuardDog\Vendor\League\OAuth2\Client\Provider\AbstractProvider Configured Google provider.
     */
    public function create_league_provider( array $config ): \GuardDog\Vendor\League\OAuth2\Client\Provider\AbstractProvider {
        return new \GuardDog\Vendor\League\OAuth2\Client\Provider\Google(
            array(
                'clientId'     => $config['client_id'] ?? '',
                'clientSecret' => $config['client_secret'] ?? '',
                'redirectUri'  => $config['redirect_uri'] ?? '',
                'accessType'   => 'offline',
            )
        );
    }

    /**
     * Normalize a Google resource owner into a standard user data array.
     *
     * Maps Google-specific fields to the common format:
     * - getId()       -> provider_user_id
     * - getEmail()    -> email
     * - getName()     -> name
     * - getFirstName() -> first_name
     * - getLastName()  -> last_name
     * - getAvatar()   -> avatar_url
     *
     * @since 1.9.30
     *
     * @param \GuardDog\Vendor\League\OAuth2\Client\Provider\ResourceOwnerInterface $resource_owner The Google user resource owner.
     * @return array Normalized user data array.
     */
    public function normalize_resource_owner( \GuardDog\Vendor\League\OAuth2\Client\Provider\ResourceOwnerInterface $resource_owner ): array {
        /** @var \GuardDog\Vendor\League\OAuth2\Client\Provider\GoogleUser $resource_owner */
        $data = method_exists( $resource_owner, 'toArray' ) ? $resource_owner->toArray() : array();

        return array(
            'provider_user_id' => (string) $resource_owner->getId(),
            'email'            => $resource_owner->getEmail(),
            'email_verified'   => $data['verified_email'] ?? ( $data['email_verified'] ?? false ),
            'name'             => $resource_owner->getName(),
            'first_name'       => $resource_owner->getFirstName(),
            'last_name'        => $resource_owner->getLastName(),
            'avatar_url'       => $resource_owner->getAvatar(),
        );
    }

    /**
     * Check if this provider has all required configuration.
     *
     * Google requires a Client ID and Client Secret.
     *
     * @since 1.9.30
     * @return bool True if client_id and client_secret are both set.
     */
    public function is_configured(): bool {
        $client_id     = get_option( 'guard_dog_social_google_client_id', '' );
        $client_secret = get_option( 'guard_dog_social_google_client_secret', '' );

        return ! empty( $client_id ) && ! empty( $client_secret );
    }

    /**
     * Get the admin settings fields for Google OAuth configuration.
     *
     * Defines the Client ID and Client Secret fields needed to configure
     * Google OAuth in the Guard Dog admin settings.
     *
     * @since 1.9.30
     *
     * @return array List of settings field definitions.
     */
    public function get_settings_fields(): array {
        return array(
            array(
                'id'          => 'client_id',
                'label'       => __( 'Client ID', 'guard-dog' ),
                'type'        => 'text',
                'required'    => true,
                'description' => __( 'The OAuth 2.0 Client ID from the Google Cloud Console.', 'guard-dog' ),
            ),
            array(
                'id'          => 'client_secret',
                'label'       => __( 'Client Secret', 'guard-dog' ),
                'type'        => 'password',
                'required'    => true,
                'description' => __( 'The OAuth 2.0 Client Secret from the Google Cloud Console.', 'guard-dog' ),
            ),
        );
    }
}
