<?php

namespace EasyLinkAutomations\Auth\Platforms;

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

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

/**
 * ActiveCampaign API Key authentication.
 *
 * Uses a per-account API URL and an API key sent via the custom Api-Token header.
 * Each ActiveCampaign account has a unique URL like https://youraccountname.api-us1.com.
 */
final class ActiveCampaignAuth implements PlatformAuthInterface
{
    public static function getSlug(): string
    {
        return 'active_campaign';
    }

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

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

    public static function getFields(): array
    {
        return [
            'api_url' => [
                'label'       => 'API URL',
                'type'        => 'text',
                'required'    => true,
                'placeholder' => 'https://youraccountname.api-us1.com',
            ],
            'api_key' => [
                'label'       => 'API Key',
                'type'        => 'text',
                'password'    => true,
                'required'    => true,
                'placeholder' => 'Your ActiveCampaign API Key',
            ],
        ];
    }

    public static function getTestRequest(): ?array
    {
        return [
            'url'    => '{api_url}/api/3/fields',
            'method' => 'GET',
        ];
    }

    public static function authenticate(array $credentials): array
    {
        return ['Api-Token' => $credentials['api_key'] ?? ''];
    }

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