#include <QSqlQuery>
#include <QSqlError>
#include <QVariant>
#include "ApplicationDAO.h"
#include "DatabaseManager.h"
#include "db/ApplicationInfo.h"

bool ApplicationDAO::upsertApplication(const ApplicationInfo &info) {
    QSqlQuery query(DatabaseManager::instance().database());

    query.prepare(R"(
        INSERT INTO applications (id, device_id, pin_code, name, package, status, comment, image)
        VALUES (:id, :device_id, :pin_code, :name, :package, :status, :comment, :image)
        ON CONFLICT(device_id, name)
        DO UPDATE SET
            device_id = excluded.device_id,
            pin_code = excluded.pin_code,
            name = excluded.name,
            package = excluded.package,
            status = excluded.status,
            comment = excluded.comment,
            image = excluded.image
    )");

    query.bindValue(":id", info.id);
    query.bindValue(":device_id", info.deviceId);
    query.bindValue(":pin_code", info.pinCode);
    query.bindValue(":name", info.name);
    query.bindValue(":package", info.package);
    query.bindValue(":status", info.status);
    query.bindValue(":comment", info.comment);
    query.bindValue(":image", info.image);

    if (!query.exec()) {
        qWarning() << "Ошибка при вставке/обновлении application:" << query.lastError().text();
        return false;
    }

    return true;
}

bool ApplicationDAO::updatePinCode(const QString &appId, const QString &pinCode) {
    QSqlQuery query(DatabaseManager::instance().database());

    query.prepare(R"(
        UPDATE applications
        SET pin_code = :pin_code
        WHERE id = :id
    )");

    query.bindValue(":pin_code", pinCode);
    query.bindValue(":id", appId);

    if (!query.exec()) {
        qWarning() << "Ошибка при обновлении PIN-кода приложения:" << query.lastError().text();
        return false;
    }

    return true;
}

ApplicationInfo ApplicationDAO::getApplicationsByDeviceId(const QString &deviceId, const QString &appName) {
    QSqlQuery query(DatabaseManager::instance().database());

    query.prepare(R"(
        SELECT id, device_id, pin_code, name, package, status, comment, image
        FROM applications
        WHERE device_id = :device_id AND name = :name
        LIMIT 1
    )");

    query.bindValue(":device_id", deviceId);
    query.bindValue(":name", appName);

    if (!query.exec()) {
        qWarning() << "Ошибка при получении приложений по deviceId:" << query.lastError().text();
        return {};
    }

    while (query.next()) {
        ApplicationInfo app;
        app.id = query.value("id").toString();
        app.deviceId = query.value("device_id").toString();
        app.pinCode = query.value("pin_code").toString();
        app.name = query.value("name").toString();
        app.package = query.value("package").toString();
        app.status = query.value("status").toString();
        app.comment = query.value("comment").toString();
        app.image = query.value("image").toByteArray();
        return app;
    }

    return {};
}
