// SPDX-License-Identifier: GPL-3.0-only
/*
 *  Luna Launcher - Minecraft Launcher
 *  Copyright (C) 2025 AndreaFrederica <andreafrederica@outlook.com>
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, version 3.
 *
 *  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 General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <https://www.gnu.org/licenses/>.
 */

#pragma once

#include <QNetworkReply>
#include <QObject>

#include "minecraft/auth/AuthStep.h"
#include "minecraft/auth/AccountData.h"
#include "net/NetJob.h"
#include "net/Upload.h"
#include "net/RawHeaderProxy.h"

class YggdrasilAuthStep : public AuthStep {
    Q_OBJECT

   public:
    enum class RequestType { Authenticate, Refresh, Validate };

    explicit YggdrasilAuthStep(AccountData* data, RequestType type = RequestType::Authenticate);
    virtual ~YggdrasilAuthStep() noexcept = default;

    void perform() override;
    QString describe() override;

   private slots:
    void onRequestDone();

   private:
    QUrl getAuthServerEndpoint(const QString& endpoint) const;
    QUrl getSessionServerEndpoint(const QString& endpoint) const;
    void processAuthenticateResponse(QByteArray& data);
    void processRefreshResponse(QByteArray& data);

    RequestType m_requestType;
    std::shared_ptr<QByteArray> m_response;
    Net::Upload::Ptr m_request;
    NetJob::Ptr m_task;
};
