<?php

namespace EasyLinkAutomations\Auth\Platforms;

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

use EasyLinkAutomations\Auth\AuthType;
use EasyLinkAutomations\Auth\Contracts\OAuth2PlatformAuthInterface;

/**
 * CleverReach OAuth2 authentication.
 *
 * CleverReach issues OAuth credentials per account: the site owner registers
 * their own application under Account → Extras → REST API and supplies the
 * resulting Client ID and Client Secret here. The authorization code flow is
 * the officially documented grant; access tokens are refreshed through the
 * refresh token returned by the token endpoint.
 *
 * Token endpoint: https://rest.cleverreach.com/oauth/token.php
 * Test endpoint:  https://rest.cleverreach.com/v3/groups
 */
final class CleverReachAuth implements OAuth2PlatformAuthInterface
{
    /**
     * Base URL for the CleverReach REST API v3.
     */
    public const API_BASE = 'https://rest.cleverreach.com/v3';

    public static function getSlug(): string
    {
        return 'cleverreach';
    }

    public static function getName(): string
    {
        return 'CleverReach';
    }

    public static function getAuthType(): string
    {
        return AuthType::OAUTH2;
    }

    public static function getFields(): array
    {
        return [
            'client_id' => [
                'label'       => 'Client ID',
                'type'        => 'text',
                'required'    => true,
                'placeholder' => 'Your CleverReach OAuth Client ID',
                'description' => 'Create an application under Account → Extras → REST API in CleverReach, then copy its Client ID.',
            ],
            'client_secret' => [
                'label'       => 'Client Secret',
                'type'        => 'text',
                'password'    => true,
                'required'    => true,
                'placeholder' => 'Your CleverReach OAuth Client Secret',
                'description' => 'The Client Secret shown for the same CleverReach application. Register this site’s callback URL as the application’s redirect URI before connecting.',
            ],
        ];
    }

    public static function getOAuth2Config(): array
    {
        return [
            'auth_url'  => 'https://rest.cleverreach.com/oauth/authorize.php',
            'token_url' => 'https://rest.cleverreach.com/oauth/token.php',
            'scope'     => '',
        ];
    }

    public static function getTestRequest(): ?array
    {
        return [
            'url'    => self::API_BASE . '/groups',
            'method' => 'GET',
        ];
    }

    public static function authenticate(array $credentials): array
    {
        return [
            'Authorization' => 'Bearer ' . trim((string) ($credentials['access_token'] ?? '')),
        ];
    }

    public static function toArray(): array
    {
        return [
            'slug'          => static::getSlug(),
            'name'          => static::getName(),
            'auth_type'     => static::getAuthType(),
            'fields'        => static::getFields(),
            'oauth2_config' => static::getOAuth2Config(),
        ];
    }
}
