<?php

declare(strict_types=1);

/*
 * AuthenticationValidator.php
 * Copyright (c) 2025 james@firefly-iii.org
 *
 * This file is part of Firefly III (https://github.com/firefly-iii).
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
 */

namespace App\Services\Sophtron;

use App\Services\Enums\AuthenticationStatus;
use App\Services\Shared\Authentication\AuthenticationValidatorInterface;
use App\Services\Shared\Authentication\SecretManager;
use Illuminate\Support\Facades\Log;

final class AuthenticationValidator implements AuthenticationValidatorInterface
{
    public function validate(): AuthenticationStatus
    {
        Log::debug(sprintf('Now at %s', __METHOD__));

        $userId    = SecretManager::getSophtronUserId(null);
        $accessKey = SecretManager::getSophtronAccessKey(null);

        if ('' === $userId || '' === $accessKey) {
            return AuthenticationStatus::NODATA;
        }

        return AuthenticationStatus::AUTHENTICATED;
    }

    /**
     * FIXME needs to point to a generic method that is somewhere inside a generic AuthenticationValidator.
     */
    public function getData(): array
    {
        return ['user_id' => SecretManager::getSophtronUserId(null), 'access_key' => SecretManager::getSophtronAccessKey(null)];
    }

    public function setData(array $data): void
    {
        // FIXME needs to use constants.
        SecretManager::saveValueInSession('user_id', $data['user_id']);
        SecretManager::saveValueInSession('access_key', $data['access_key']);
    }
}
