<?php

namespace EasyLinkAutomations\Auth\Platforms;

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

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

/**
 * MailerLite API Key authentication.
 *
 * Uses the API key as a Bearer token via the Authorization header.
 * Test endpoint: https://connect.mailerlite.com/api/groups
 */
final class MailerLiteAuth implements PlatformAuthInterface
{
    public static function getSlug(): string
    {
        return 'mailerlite';
    }

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

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

    public static function getFields(): array
    {
        return [
            'api_key' => [
                'label'       => 'API Key',
                'type'        => 'text',
                'password'    => true,
                'required'    => true,
                'placeholder' => 'Your MailerLite API Key',
                'description' => 'Find your API key in MailerLite → Integrations → API.',
            ],
        ];
    }

    public static function getTestRequest(): ?array
    {
        return [
            'url'    => 'https://connect.mailerlite.com/api/groups',
            'method' => 'GET',
        ];
    }

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

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