#pragma once

#include <QString>
#include <QSqlDatabase>
#include <QMap>
#include <QDateTime>

class ChartCandle;

struct OrderRecord {
    qlonglong id;
    int client;
    QString strategy;
    int currency;
    int timeFrame;
    double profit;
    int digits;
    QDateTime created;
};

struct EventRecord {
    qlonglong id;
    qlonglong trade;
    int event;
    int ticket;
    double openPrice;
    double closePrice;
    double lots;
    double stopLoss;
    double takeProfit;
    double profit;
    QDateTime time;
};

class Database
{
public:
    Database(const QString& connectionName);
    ~Database();

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

    void disconnect();
    bool hasError() const;
    QString lastError() const;

    QMap<int, QString> enumeration(const QString& enumName);
    QStringList strategyList();

    QList<OrderRecord> searchOrder(
        const QString& strategy, int currency, int timeFrame,
        const QDateTime& from, const QDateTime& to);

    QList<EventRecord> eventList(qlonglong tradeId);
    QList<ChartCandle> priceData(qlonglong tradeId);
    double orderProfit(qlonglong tradeId);

private:
    QSqlDatabase sql_;
};
