<?php

namespace EasyLinkAutomations\Auth\Platforms;

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

use EasyLinkAutomations\Auth\AuthType;
use EasyLinkAutomations\Auth\Contracts\PlatformAuthInterface;

/**
 * Generic Bearer Token authentication.
 *
 * Sends a token as Authorization: Bearer {token}.
 */
final class GenericBearerTokenAuth implements PlatformAuthInterface
{
    public static function getSlug(): string
    {
        return 'generic_bearer_token';
    }

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

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

    public static function getFields(): array
    {
        return [
            'access_token' => [
                'label'       => 'Access Token',
                'type'        => 'text',
                'password'    => true,
                'required'    => true,
                'placeholder' => 'Enter your access token',
            ],
        ];
    }

    public static function getTestRequest(): ?array
    {
        return null;
    }

    public static function authenticate(array $credentials): array
    {
        $token = trim((string) ($credentials['access_token'] ?? ''));

        return ['Authorization' => 'Bearer ' . $token];
    }

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