<?php

namespace EasyLinkAutomations\Auth\Platforms;

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

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

/**
 * Discord OAuth2 + Bot Token authentication.
 *
 * Discord requires both an OAuth2 flow (to authorize the bot for specific
 * guilds/servers) and a Bot Token (for API calls). This mirrors n8n's
 * DiscordOAuth2Api credential which combines both.
 *
 * - OAuth2 flow uses client_id + client_secret to install the bot to servers.
 * - Bot Token is used for all API requests via "Authorization: Bot {token}".
 *
 * Test endpoint: GET https://discord.com/api/v10/users/@me/guilds
 *
 * Migrated from n8n: DiscordOAuth2Api credentials
 */
final class DiscordAuth implements OAuth2PlatformAuthInterface
{
    public static function getSlug(): string
    {
        return 'discord';
    }

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

    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 Discord App Client ID',
                'description' => 'Client ID from the Discord Developer Portal (OAuth2 section)',
            ],
            'client_secret' => [
                'label'       => 'Client Secret',
                'type'        => 'text',
                'password'    => true,
                'required'    => true,
                'placeholder' => 'Your Discord App Client Secret',
                'description' => 'Client Secret from the Discord Developer Portal (OAuth2 section)',
            ],
            'bot_token' => [
                'label'       => 'Bot Token',
                'type'        => 'text',
                'password'    => true,
                'required'    => true,
                'placeholder' => 'MTM0NTY3ODkwMTIzNDU2Nzg5.Xxx_xx.xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
                'description' => 'Bot Token from the Discord Developer Portal (Bot section)',
            ],
        ];
    }

    public static function getOAuth2Config(): array
    {
        return [
            'auth_url'    => 'https://discord.com/api/oauth2/authorize',
            'token_url'   => 'https://discord.com/api/oauth2/token',
            'scope'       => 'identify guilds guilds.join bot',
            'auth_params' => [
                'permissions' => '1642758929655',
            ],
        ];
    }

    public static function getTestRequest(): ?array
    {
        return [
            'url'    => 'https://discord.com/api/v10/users/@me/guilds',
            'method' => 'GET',
        ];
    }

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

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