#pragma once

#include "../TradeCore/trade_data.hh"
#include <QString>
#include <QtSql/QSqlDatabase>
#include <QMap>

/**
 * @brief Used for logging into database
 */
struct ClientInfo {
    ClientInfo():id(-1){}
    int id;
    std::string strategy;
    std::string currency;
    std::string timeframe;
    int digits;
};

/**
 * @brief Connection and logging into database
 */
class Database
{
public:
    Database(const QString& connectionName);
    ~Database();

    bool connect(
        const QString& hostname, const QString& database,
        const QString& username, const QString& password);

    QString lastError() const;
    QMap<std::string, int> enumValues(const QString& enumName);

    void logTrade(
        const OrderVector& orders, const CandleVector& price,
        CandleVector::const_iterator from, const ChartCandle& last,
        const ClientInfo& client);

private:
    qlonglong logTrade(const ClientInfo& client);
    void logEvents(qlonglong tradeId, const OrderVector& orders);
    bool logCandle(qlonglong tradeId, const ChartCandle& candle);

    void logPriceHistory(
        qlonglong tradeId, const CandleVector& price,
        CandleVector::const_iterator from, const ChartCandle& last);

private:
    QSqlDatabase sql_;
};

