<?php

declare(strict_types=1);

namespace App\Modules\Identity\Controllers;

use App\Modules\Identity\Support\IdentityPolicy;
use App\Modules\Identity\Services\OidcProviderService;

class OidcController
{
    public function __construct(
        private readonly OidcProviderService $providerService,
        private readonly IdentityPolicy $identityPolicy = new IdentityPolicy()
    ) {
    }

    public function providers(): array
    {
        return [
            'providers' => $this->providerService->configuredProviders(),
            'identity_policy' => $this->identityPolicy->toArray(),
        ];
    }

    public function start(string $providerKey): array
    {
        $provider = $this->providerService->findProvider($providerKey);

        return [
            'status' => $provider !== null ? 'redirect-placeholder' : 'provider-not-found',
            'provider' => $providerKey,
            'provider_config' => $provider?->toArray(),
            'identity_policy' => $this->identityPolicy->toArray(),
        ];
    }

    public function callback(string $providerKey, array $claims = []): array
    {
        $provider = $this->providerService->findProvider($providerKey);

        return [
            'status' => $provider !== null ? 'callback-placeholder' : 'provider-not-found',
            'provider' => $providerKey,
            'provider_config' => $provider?->toArray(),
            'identity_policy' => $this->identityPolicy->toArray(),
            'claims' => $claims,
        ];
    }
}
