<?php

namespace EasyLinkAutomations\Auth\Platforms;

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

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

final class FreshserviceAuth implements PlatformAuthInterface
{
    public static function getSlug(): string { return 'freshservice'; }
    public static function getName(): string { return 'Freshservice'; }
    public static function getAuthType(): string { return AuthType::API_KEY; }

    public static function getFields(): array
    {
        return [
            'api_key' => [
                'label' => 'API Key', 'type' => 'text', 'required' => true, 'password' => true,
            ],
            'domain' => [
                'label' => 'Domain', 'type' => 'text', 'required' => true,
                'placeholder' => 'mycompany',
                'description' => 'The tenant name from https://mycompany.freshservice.com.',
            ],
        ];
    }

    public static function authenticate(array $credentials): array
    {
        return [
            'Authorization' => 'Basic ' . base64_encode(($credentials['api_key'] ?? '') . ':X'),
            'Content-Type'  => 'application/json',
        ];
    }

    public static function getTestRequest(): array
    {
        return ['url' => 'https://{domain}.freshservice.com/api/v2/agents/me', 'method' => 'GET'];
    }

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