<?php

namespace EasyLinkAutomations\Auth\Platforms;

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

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

final class MicrosoftTeamsAuth implements OAuth2PlatformAuthInterface
{
    public static function getSlug(): string
    {
        return 'microsoft_teams';
    }

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

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

    public static function getFields(): array
    {
        return [
            'client_id' => [
                'label' => 'Application (Client) ID',
                'type' => 'text',
                'required' => true,
                'placeholder' => '00000000-0000-0000-0000-000000000000',
            ],
            'client_secret' => [
                'label' => 'Client Secret',
                'type' => 'text',
                'password' => true,
                'required' => true,
                'placeholder' => 'Your Microsoft Entra application client secret',
            ],
            'tenant_id' => [
                'label' => 'Directory (Tenant) ID',
                'type' => 'text',
                'required' => true,
                'default' => 'organizations',
                'placeholder' => 'organizations or your tenant GUID',
                'description' => 'Use organizations for a multi-tenant app, or enter a tenant GUID for a single-tenant app.',
            ],
        ];
    }

    public static function getOAuth2Config(): array
    {
        $scopes = implode(' ', [
            'openid',
            'offline_access',
            'User.Read',
            'User.ReadBasic.All',
            'Team.ReadBasic.All',
            'Channel.ReadBasic.All',
            'Channel.Create',
            'Channel.Delete.All',
            'ChannelSettings.ReadWrite.All',
            'ChannelMessage.Send',
            'ChannelMessage.Read.All',
            'Chat.ReadWrite',
            'ChatMessage.Send',
            'Tasks.ReadWrite',
            'Group.Read.All',
        ]);

        return [
            'auth_url' => 'https://login.microsoftonline.com/{tenant_id}/oauth2/v2.0/authorize',
            'token_url' => 'https://login.microsoftonline.com/{tenant_id}/oauth2/v2.0/token',
            'scope' => $scopes,
            'include_scope_in_token_request' => true,
            'auth_params' => ['response_mode' => 'query', 'prompt' => 'select_account'],
        ];
    }

    public static function getTestRequest(): ?array
    {
        return ['url' => 'https://graph.microsoft.com/v1.0/me', 'method' => 'GET'];
    }

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

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