#ifndef LICENSEMANAGER_LICENSEMODEL_H
#define LICENSEMANAGER_LICENSEMODEL_H

// Qt
#include <QAbstractItemModel>
#include <QFutureWatcher>
#include <QtSql/QSqlDatabase>

class LicenseModel : public QAbstractItemModel
{
    Q_OBJECT
public:

    enum class Status
    {
        None = 0,
        Ok,
        DbStructError,
        DbExistError,
        Working,
    };

    struct LicenseItem
    {
        QString id;
        QString city;
        QString email;
        QString firstName;
        QString hardwareHash;
        QString lastName;
        QString patronymic;
        QString phone;
        QString yourCompany;
        QString createdAtUtc;
        QString comment;
    };

    explicit LicenseModel(QObject* parent = nullptr);
    ~LicenseModel();

    int rowCount(const QModelIndex &parent = QModelIndex()) const override;
    int columnCount(const QModelIndex &parent = QModelIndex()) const override;
    QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override;
    QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const override;
    QModelIndex parent(const QModelIndex &index) const override;
    QModelIndex index(int row, int column, const QModelIndex &parent = QModelIndex()) const override;
    Status getStatus();
    QString getStatusText();
    bool prepareDatabase();
    void addClient(const LicenseItem &item);
    void deleteClient(const QList<int> &rows);
    void editClient(const LicenseItem &item, int index);

    void updateModel();
    LicenseItem getItem(int index) const;
signals:
    void statusChanged();
private:
    bool checkTables();
private:
    QList<LicenseItem> m_data;
    Status m_status = Status::None;
    QStringList m_errors;
    QSqlDatabase m_db;
};

#endif // LICENSEMANAGER_LICENSEMODEL_H
