#ifndef GITJOB_H
#define GITJOB_H

#include <QDir>
#include <QThread>
extern "C" {
#include <git2.h>
}
#include <variant>

// Forward declarations for the different credential types.
struct HTTP {};
struct HTTPUserPass {
    QString pass; ///< Password for HTTP user authentication.
};
struct SSHKey {
    QString pub_key; ///< public key path.
    QString priv_key; ///< private key path.
    QString passphrase; ///< key passphrase.
};



/**
 * @brief Variant type to represent various types of credentials.
 *
 * This type is used to store one of the following credential types:
 * - HTTP
 * - HTTPUserPass
 * - SSHKey
 */
typedef std::variant<HTTP, HTTPUserPass, SSHKey> cred_type;

enum class ErrorCodeCB {
    None = 0,
    NoUsername,
    UrlTypeDoNotMatchCreds,
    InvalidCreds,
};


struct PayloadCB {
    bool called;
    cred_type creds;
    ErrorCodeCB err;
    PayloadCB(bool ca, cred_type cr): called(ca), creds(cr), err(ErrorCodeCB::None) {}
};

/**
 * @class GitJob
 * @brief A class that manages Git-related tasks using libgit2.
 *
 * The GitJob class is used abstraction class to perform Git operations, such as cloning repositories,
 * in a separate thread using libgit2 for interacting with Git repositories.
 */
class GitJob : public QThread
{
    Q_OBJECT

protected:
    cred_type m_cred; ///< The credentials used for Git operations.

    /**
     * @brief Callback function for handling Git credentials during cloning.
     *
     * This function is called by libgit2 to handle credentials for cloning a repository.
     * The callback is invoked when Git needs to authenticate with a remote repository.
     *
     * @param out Pointer to the credentials object that will be populated by the callback.
     * @param url The URL of the repository being cloned.
     * @param username_from_url The username extracted from the URL (if applicable).
     * @param allowed_types A bitmask of the allowed types of credentials that can be used.
     * @param payload User-defined data passed to the callback. This is typically the instance
     *        of the class providing the callback, or other user-defined data.
     *
     * @return A status code indicating success (0) or failure (non-zero).
     * @see git_cred
     * @see git_cred_type
     */
    static int credentialsCB(git_cred **out, const char *url, const char *username_from_url,
                             unsigned int allowed_types, void *payload);

public:
    /**
     * @brief Constructor for the GitJob class.
     *
     * Initializes the GitJob instance with the given credentials.
     *
     * @param cred The credentials to be used for the Git operation. This can be one of
     *        the following types: HTTP, HTTPUserPass, SSHPass, or SSHKey.
     */
    GitJob(cred_type cred);

    /**
     * @brief Destructor for the GitJob class.
     *
     * Cleans up any resources used by the GitJob.
     */
    ~GitJob();
};

#endif // GITJOB_H
