<?php

namespace App\Support\Providers\TargetScope;

final class ProviderIdentityContextMetadata
{
    public const string VISIBILITY_CONTEXTUAL_ONLY = 'contextual_only';

    public const string VISIBILITY_AUDIT_ONLY = 'audit_only';

    public const string VISIBILITY_TROUBLESHOOTING_ONLY = 'troubleshooting_only';

    public function __construct(
        public readonly string $provider,
        public readonly string $detailKey,
        public readonly string $detailLabel,
        public readonly string $detailValue,
        public readonly string $visibility = self::VISIBILITY_CONTEXTUAL_ONLY,
    ) {}

    public static function microsoftTenantId(?string $value, string $visibility = self::VISIBILITY_CONTEXTUAL_ONLY): ?self
    {
        $value = trim((string) $value);

        if ($value === '') {
            return null;
        }

        return new self(
            provider: 'microsoft',
            detailKey: 'microsoft_tenant_id',
            detailLabel: 'Microsoft tenant ID',
            detailValue: $value,
            visibility: $visibility,
        );
    }

    public static function authorityTenant(?string $value, string $visibility = self::VISIBILITY_TROUBLESHOOTING_ONLY): ?self
    {
        $value = trim((string) $value);

        if ($value === '') {
            return null;
        }

        return new self(
            provider: 'microsoft',
            detailKey: 'authority_tenant',
            detailLabel: 'Authority tenant',
            detailValue: $value,
            visibility: $visibility,
        );
    }

    public static function redirectUri(?string $value, string $visibility = self::VISIBILITY_TROUBLESHOOTING_ONLY): ?self
    {
        $value = trim((string) $value);

        if ($value === '') {
            return null;
        }

        return new self(
            provider: 'microsoft',
            detailKey: 'redirect_uri',
            detailLabel: 'Redirect URI',
            detailValue: $value,
            visibility: $visibility,
        );
    }

    /**
     * @return array{
     *     provider: string,
     *     detail_key: string,
     *     detail_label: string,
     *     detail_value: string,
     *     visibility: string
     * }
     */
    public function toArray(): array
    {
        return [
            'provider' => $this->provider,
            'detail_key' => $this->detailKey,
            'detail_label' => $this->detailLabel,
            'detail_value' => $this->detailValue,
            'visibility' => $this->visibility,
        ];
    }
}
