/*
 * Copyright (C) 2016-2026 Matthias Klumpp <matthias@tenstral.net>
 *
 * Licensed under the GNU Lesser General Public License Version 3
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the license, or
 * (at your option) any later version.
 *
 * This software 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 Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this software.  If not, see <http://www.gnu.org/licenses/>.
 */

#pragma once

#include <string>
#include <vector>
#include <unordered_map>
#include <variant>
#include <mutex>
#include <cstddef>
#include <cstdint>
#include <ctime>
#include <lmdb.h>

#include "logging.h"

namespace ASGenerator
{

class Config;

/**
 * Type alias for generic metadata values stored in our databases.
 */
using MetaValue = std::variant<std::int64_t, std::string, double>;

/**
 * Statistics entry
 */
struct StatisticsEntry {
    std::time_t time{0};
    std::unordered_map<std::string, MetaValue> data;
};

/**
 * Serialize the data of a statistics entry for storage in a database.
 */
std::vector<std::byte> serializeStatsEntryData(const StatisticsEntry &entry);

/**
 * Read a statistics entry that was stored for @timestamp back from its serialized form.
 */
StatisticsEntry deserializeStatsEntry(std::time_t timestamp, const std::vector<std::byte> &data);

/**
 * Contains statistical data about the metadata we generated, recorded over time.
 *
 * This lives in its own database, separate from the main data store: statistics are only
 * needed when reports are written, and - unlike everything else we keep - they can not be
 * regenerated by rescanning the archive. Keeping them apart means the caches can be wiped
 * and rebuilt without losing any history.
 */
class StatsStore
{
public:
    StatsStore();
    ~StatsStore();

    // Delete copy constructor and assignment operator
    StatsStore(const StatsStore &) = delete;
    StatsStore &operator=(const StatsStore &) = delete;

    /**
     * Open the statistics database in @dir
     */
    void open(const std::string &dir);

    /**
     * Open the statistics database using configuration
     */
    void open(const Config &conf);

    /**
     * Close the database
     */
    void close();

    /**
     * Get the statistics entries recorded at or after @since.
     */
    std::vector<StatisticsEntry> getStatistics(std::time_t since = 0);

    /**
     * Add statistics entry
     */
    void addStatistics(const StatisticsEntry &stats);

    /**
     * Add statistics entry from key-value data, recorded for the current time
     */
    void addStatistics(const std::unordered_map<std::string, MetaValue> &statsData);

    /**
     * Remove statistics entry for given time
     */
    void removeStatistics(std::time_t time);

private:
    quill::Logger *m_log;
    MDB_env *m_dbEnv;
    MDB_dbi m_dbStats;

    bool m_opened;
    std::mutex m_mutex;

    void checkError(int rc, const std::string &msg);
    MDB_txn *newTransaction(unsigned int flags = 0);
    void commitTransaction(MDB_txn *txn);
    void quitTransaction(MDB_txn *txn);
};

} // namespace ASGenerator
