#pragma once

#include <string>
#include <unordered_map>
#include <mutex>

struct x509_st;
struct ssl_st;
struct ssl_ctx_st;
struct evp_pkey_st;

typedef struct x509_st X509;
typedef struct ssl_st SSL;
typedef struct ssl_ctx_st SSL_CTX;
typedef struct evp_pkey_st EVP_PKEY;

class CertManager
{
    public:
        CertManager();
        ~CertManager();

        bool init();
        SSL_CTX* createHostContext(const std::string& host);

    private:
        bool generateCA();
        bool loadCA();

        void installCert(X509* cert);

        EVP_PKEY* _caPkey = nullptr;
        X509* _caCert = nullptr;
        EVP_PKEY* _sessionPkey = nullptr;

        std::mutex _mutex;
        std::unordered_map<std::string, SSL_CTX*> _hostContexts;
};
