#include "thirddao.h"
#include "../mapplication.h"

bool ThirdDAO::create(const Data& data)
{
    dApp->Enterprise().open();
    QSqlQuery qry(dApp->Enterprise());
    qry.prepare("INSERT INTO THIRD (FORM, CIF, NAME, NICKNAME, FAMILY, INTRA_FC, STATE, "
                "CLIENT_STATE, CLIENT_CODE, CLIENT_ACCOUNT, "
                "SUPPLIER_STATE, SUPPLIER_CODE, SUPPLIER_ACCOUNT, "
                "ADDRESS, POSTCODE, CITY, PROVINCE, COUNTRY_ID, "
                "PHONE, MOBILE, FAX, EMAIL, WEBSIDE, "
                "PAYMENT_METHOD, PAYMENT_TYPE, LOGO, NOTE_PUBLIC, NOTE_PRIVATE, "
                "UPDATEDAT, CREATEDBY) "
                "VALUES (:FORM, :CIF, :NAME, :NICKNAME, :FAMILY, :INTRA_FC, :STATE, "
                ":CLIENT_STATE, :CLIENT_CODE, :CLIENT_ACCOUNT, "
                ":SUPPLIER_STATE, :SUPPLIER_CODE, :SUPPLIER_ACCOUNT, "
                ":ADDRESS, :POSTCODE, :CITY, :PROVINCE, :COUNTRY_ID, "
                ":PHONE, :MOBILE, :FAX, :EMAIL, :WEBSIDE, "
                ":PAYMENT_METHOD, :PAYMENT_TYPE, :LOGO, :NOTE_PUBLIC, :NOTE_PRIVATE, "
                ":UPDATEDAT, :CREATEDBY)");
    qry.bindValue(":FORM", data.form);
    qry.bindValue(":CIF", data.cif);
    qry.bindValue(":NAME", data.name);
    qry.bindValue(":NICKNAME", data.nickname);
    qry.bindValue(":FAMILY", data.family);
    qry.bindValue(":INTRA_FC", data.intraFc);
    qry.bindValue(":STATE", data.state);
    qry.bindValue(":CLIENT_STATE", data.clientState);
    qry.bindValue(":CLIENT_CODE", data.clientCode);
    qry.bindValue(":CLIENT_ACCOUNT", data.clientAccount);
    qry.bindValue(":SUPPLIER_STATE", data.supplierState);
    qry.bindValue(":SUPPLIER_CODE", data.supplierCode);
    qry.bindValue(":SUPPLIER_ACCOUNT", data.supplierAccount);
    qry.bindValue(":ADDRESS", data.address);
    qry.bindValue(":POSTCODE", data.postcode);
    qry.bindValue(":CITY", data.city);
    qry.bindValue(":PROVINCE", data.province);
    qry.bindValue(":COUNTRY_ID", data.countryId);
    qry.bindValue(":PHONE", data.phone);
    qry.bindValue(":MOBILE", data.mobile);
    qry.bindValue(":FAX", data.fax);
    qry.bindValue(":EMAIL", data.email);
    qry.bindValue(":WEBSIDE", data.website);
    qry.bindValue(":PAYMENT_METHOD", data.paymentMethod);
    qry.bindValue(":PAYMENT_TYPE", data.paymentType);
    qry.bindValue(":LOGO", data.logo);
    qry.bindValue(":NOTE_PUBLIC", data.notePublic);
    qry.bindValue(":NOTE_PRIVATE", data.notePrivate);
    qry.bindValue(":UPDATEDAT", data.updatedAt);
    qry.bindValue(":CREATEDBY", data.createdBy);

    bool ok = qry.exec();
    if (!ok) qDebug() << "ThirdDAO::create error:" << qry.lastError().text();
    dApp->Enterprise().close();
    return ok;
}

bool ThirdDAO::read(int id, Data& data)
{
    dApp->Enterprise().open();
    QSqlQuery qry(dApp->Enterprise());
    qry.prepare("SELECT * FROM THIRD WHERE ID = :ID");
    qry.bindValue(":ID", id);

    bool ok = qry.exec();
    if (ok && qry.next()) {
        data.id = qry.value("ID").toInt();
        data.form = qry.value("FORM").toString();
        data.cif = qry.value("CIF").toString();
        data.name = qry.value("NAME").toString();
        data.nickname = qry.value("NICKNAME").toString();
        data.family = qry.value("FAMILY").toString();
        data.intraFc = qry.value("INTRA_FC").toString();
        data.state = qry.value("STATE").toInt();
        data.clientState = qry.value("CLIENT_STATE").toInt();
        data.clientCode = qry.value("CLIENT_CODE").toString();
        data.clientAccount = qry.value("CLIENT_ACCOUNT").toString();
        data.supplierState = qry.value("SUPPLIER_STATE").toInt();
        data.supplierCode = qry.value("SUPPLIER_CODE").toString();
        data.supplierAccount = qry.value("SUPPLIER_ACCOUNT").toString();
        data.address = qry.value("ADDRESS").toString();
        data.postcode = qry.value("POSTCODE").toString();
        data.city = qry.value("CITY").toString();
        data.province = qry.value("PROVINCE").toString();
        data.countryId = qry.value("COUNTRY_ID").toString();
        data.phone = qry.value("PHONE").toString();
        data.mobile = qry.value("MOBILE").toString();
        data.fax = qry.value("FAX").toString();
        data.email = qry.value("EMAIL").toString();
        data.website = qry.value("WEBSIDE").toString();
        data.paymentMethod = qry.value("PAYMENT_METHOD").toString();
        data.paymentType = qry.value("PAYMENT_TYPE").toString();
        data.logo = qry.value("LOGO").toByteArray();
        data.notePublic = qry.value("NOTE_PUBLIC").toString();
        data.notePrivate = qry.value("NOTE_PRIVATE").toString();
        data.updatedAt = qry.value("UPDATEDAT").toDate();
        data.createdAt = qry.value("CREATEDAT").toDate();
        data.createdBy = qry.value("CREATEDBY").toString();
    } else {
        ok = false;
        qDebug() << "ThirdDAO::read error:" << qry.lastError().text();
    }

    dApp->Enterprise().close();
    return ok;
}

bool ThirdDAO::readByCif(const QString& cif, Data& data)
{
    dApp->Enterprise().open();
    QSqlQuery qry(dApp->Enterprise());
    qry.prepare("SELECT * FROM THIRD WHERE CIF = :CIF");
    qry.bindValue(":CIF", cif);

    bool ok = qry.exec();
    if (ok && qry.next()) {
        data.id = qry.value("ID").toInt();
        data.cif = cif;
        data.name = qry.value("NAME").toString();
        // ... mismo mapeo que read
        read(data.id, data); // reusa el otro método
    } else {
        ok = false;
    }
    dApp->Enterprise().close();
    return ok;
}

bool ThirdDAO::update(const Data& data)
{
    dApp->Enterprise().open();
    QSqlQuery qry(dApp->Enterprise());
    qry.prepare("UPDATE THIRD SET "
                "FORM = :FORM, CIF = :CIF, NAME = :NAME, NICKNAME = :NICKNAME, "
                "FAMILY = :FAMILY, INTRA_FC = :INTRA_FC, STATE = :STATE, "
                "CLIENT_STATE = :CLIENT_STATE, CLIENT_CODE = :CLIENT_CODE, CLIENT_ACCOUNT = :CLIENT_ACCOUNT, "
                "SUPPLIER_STATE = :SUPPLIER_STATE, SUPPLIER_CODE = :SUPPLIER_CODE, SUPPLIER_ACCOUNT = :SUPPLIER_ACCOUNT, "
                "ADDRESS = :ADDRESS, POSTCODE = :POSTCODE, CITY = :CITY, PROVINCE = :PROVINCE, COUNTRY_ID = :COUNTRY_ID, "
                "PHONE = :PHONE, MOBILE = :MOBILE, FAX = :FAX, EMAIL = :EMAIL, WEBSIDE = :WEBSIDE, "
                "PAYMENT_METHOD = :PAYMENT_METHOD, PAYMENT_TYPE = :PAYMENT_TYPE, "
                "LOGO = :LOGO, NOTE_PUBLIC = :NOTE_PUBLIC, NOTE_PRIVATE = :NOTE_PRIVATE, "
                "UPDATEDAT = :UPDATEDAT "
                "WHERE ID = :ID");
    qry.bindValue(":ID", data.id);
    qry.bindValue(":FORM", data.form);
    qry.bindValue(":CIF", data.cif);
    qry.bindValue(":NAME", data.name);
    qry.bindValue(":NICKNAME", data.nickname);
    qry.bindValue(":FAMILY", data.family);
    qry.bindValue(":INTRA_FC", data.intraFc);
    qry.bindValue(":STATE", data.state);
    qry.bindValue(":CLIENT_STATE", data.clientState);
    qry.bindValue(":CLIENT_CODE", data.clientCode);
    qry.bindValue(":CLIENT_ACCOUNT", data.clientAccount);
    qry.bindValue(":SUPPLIER_STATE", data.supplierState);
    qry.bindValue(":SUPPLIER_CODE", data.supplierCode);
    qry.bindValue(":SUPPLIER_ACCOUNT", data.supplierAccount);
    qry.bindValue(":ADDRESS", data.address);
    qry.bindValue(":POSTCODE", data.postcode);
    qry.bindValue(":CITY", data.city);
    qry.bindValue(":PROVINCE", data.province);
    qry.bindValue(":COUNTRY_ID", data.countryId);
    qry.bindValue(":PHONE", data.phone);
    qry.bindValue(":MOBILE", data.mobile);
    qry.bindValue(":FAX", data.fax);
    qry.bindValue(":EMAIL", data.email);
    qry.bindValue(":WEBSIDE", data.website);
    qry.bindValue(":PAYMENT_METHOD", data.paymentMethod);
    qry.bindValue(":PAYMENT_TYPE", data.paymentType);
    qry.bindValue(":LOGO", data.logo);
    qry.bindValue(":NOTE_PUBLIC", data.notePublic);
    qry.bindValue(":NOTE_PRIVATE", data.notePrivate);
    qry.bindValue(":UPDATEDAT", data.updatedAt);

    bool ok = qry.exec();
    if (!ok) qDebug() << "ThirdDAO::update error:" << qry.lastError().text();
    dApp->Enterprise().close();
    return ok;
}

bool ThirdDAO::remove(int id)
{
    dApp->Enterprise().open();
    QSqlQuery qry(dApp->Enterprise());
    qry.prepare("DELETE FROM THIRD WHERE ID = :ID");
    qry.bindValue(":ID", id);
    bool ok = qry.exec();
    if (!ok) qDebug() << "ThirdDAO::remove error:" << qry.lastError().text();
    dApp->Enterprise().close();
    return ok;
}

bool ThirdDAO::exists(int id)
{
    dApp->Enterprise().open();
    QSqlQuery qry(dApp->Enterprise());
    qry.prepare("SELECT COUNT(*) FROM THIRD WHERE ID = :ID");
    qry.bindValue(":ID", id);
    bool exists = false;
    if (qry.exec() && qry.next())
        exists = qry.value(0).toInt() > 0;
    dApp->Enterprise().close();
    return exists;
}

QVector<int> ThirdDAO::getAllIds()
{
    QVector<int> ids;
    dApp->Enterprise().open();
    QSqlQuery qry(dApp->Enterprise());
    qry.prepare("SELECT ID FROM THIRD ORDER BY NAME");
    if (qry.exec()) {
        while (qry.next())
            ids.append(qry.value(0).toInt());
    }
    dApp->Enterprise().close();
    return ids;
}

QVector<ThirdDAO::Data> ThirdDAO::getAll()
{
    QVector<Data> list;
    dApp->Enterprise().open();
    QSqlQuery qry(dApp->Enterprise());
    qry.prepare("SELECT ID, CIF, NAME, NICKNAME FROM THIRD ORDER BY NAME");
    if (qry.exec()) {
        while (qry.next()) {
            Data d;
            d.id = qry.value(0).toInt();
            d.cif = qry.value(1).toString();
            d.name = qry.value(2).toString();
            d.nickname = qry.value(3).toString();
            list.append(d);
        }
    }
    dApp->Enterprise().close();
    return list;
}