<?php

namespace App\Base\Config;

use App\Base\Config\Auth\OidcConfig;
use Symfony\Component\DependencyInjection\Attribute\Autowire;

class AuthConfig implements ConfigInterface
{
    const AUTH_METHODS = ['form_login', 'oidc'];

    private array $messages = [];

    public function __construct(
        #[Autowire(param: 'auth.method')]
        private readonly ?string $authMethod,
        private readonly OidcConfig $oidcConfig,
    ) {}

    public function isMethod(string $method): bool
    {
        return $this->getAuthMethod() === strtolower($method);
    }

    public function getAuthMethod(): string
    {
        return strtolower($this->authMethod);
    }

    public function getOidcConfig(): OidcConfig
    {
        return $this->oidcConfig;
    }

    public function isValid(): bool
    {
        $valid = true;
        if (null === $this->getAuthMethod() || "" === $this->getAuthMethod()) {
            $this->messages[] = "Your auth method is missing. Please set the AUTH_METHOD environment variable to your desired value. Valid options: [form_login, oidc].";
            return false;
        }

        if (!in_array($this->getAuthMethod(), self::AUTH_METHODS, true)) {
            $this->messages[] = "Your auth method is incorrect. Please set the AUTH_METHOD environment variable to your desired value. Valid options: [form_login, oidc].";
            return false;
        }

        if ("oidc" === $this->getAuthMethod()) {
            if (false === $this->oidcConfig->isValid()) {
                $this->messages += $this->oidcConfig->getMessages();
                $valid = false;
            }
        }

        return $valid;
    }

    public function getMessages(): array
    {
        return $this->messages;
    }
}
