#include <QtTest/QtTest>
#include "../../src/db/dao/mailitemdao.h"
#include "../../src/db/databasemanager.h"
#include "../../src/core/models/mailitem.h"
#include "../../src/core/models/account.h"
#include "../../src/core/models/folder.h"

class TestMailItemDao : public QObject
{
    Q_OBJECT
private slots:
    void initTestCase();
    void cleanupTestCase();
    void testInsertAndFind();
    void testUpdate();
    void testRemove();
};

void TestMailItemDao::initTestCase()
{
    DatabaseManager::instance().openDatabase(":memory:");
    DatabaseManager::instance().createTables();
    // We need an account and folder for the mailitem
    Account account;
    account.setEmail("test@example.com");
    account.setProvider("IMAP");
    account.setHost("imap.example.com");
    account.setPort(993);
    account.setUsername("testuser");
    account.setPassword("testpass");
    account.setUseSsl(true);
    bool accountInserted = AccountDao::insert(account);
    QVERIFY(accountInserted);

    Folder folder;
    folder.setAccountId(account.id());
    folder.setName("Inbox");
    folder.setPath("INBOX");
    folder.setIsInbox(true);
    bool folderInserted = FolderDao::insert(folder);
    QVERIFY(folderInserted);
}

void TestMailItemDao::cleanupTestCase()
{
    DatabaseManager::instance().closeDatabase();
}

void TestMailItemDao::testInsertAndFind()
{
    MailItem item;
    item.setAccountId(1); // from account inserted in init
    item.setFolderId(1);  // from folder inserted in init
    item.setSubject("Test Subject");
    item.setFrom("sender@example.com");
    item.setTo("receiver@example.com");
    item.setDate(QDateTime::currentDateTimeUtc());
    item.setContent("Test content");
    item.setUid("12345");

    bool inserted = MailItemDao::insert(item);
    QVERIFY(inserted);
    QVERIFY(item.id() > 0);

    MailItem found = MailItemDao::findById(item.id());
    QVERIFY(found.isValid());
    QCOMPARE(found.accountId(), 1);
    QCOMPARE(found.folderId(), 1);
    QCOMPARE(found.subject(), QString("Test Subject"));
    QCOMPARE(found.from(), QString("sender@example.com"));
    QCOMPARE(found.to(), QString("receiver@example.com"));
    QCOMPARE(found.content(), QString("Test content"));
    QCOMPARE(found.uid(), QString("12345"));
}

void TestMailItemDao::testUpdate()
{
    MailItem item;
    item.setAccountId(1);
    item.setFolderId(1);
    item.setSubject("Original Subject");
    item.setFrom("sender@example.com");
    item.setTo("receiver@example.com");
    item.setDate(QDateTime::currentDateTimeUtc());
    item.setContent("Original content");
    item.setUid("12345");

    bool inserted = MailItemDao::insert(item);
    QVERIFY(inserted);
    qint64 id = item.id();

    item.setSubject("Updated Subject");
    item.setContent("Updated content");

    bool updated = MailItemDao::update(item);
    QVERIFY(updated);

    MailItem found = MailItemDao::findById(id);
    QVERIFY(found.isValid());
    QCOMPARE(found.subject(), QString("Updated Subject"));
    QCOMPARE(found.content(), QString("Updated content"));
}

void TestMailItemDao::testRemove()
{
    MailItem item;
    item.setAccountId(1);
    item.setFolderId(1);
    item.setSubject("To Delete");
    item.setFrom("sender@example.com");
    item.setTo("receiver@example.com");
    item.setDate(QDateTime::currentDateTimeUtc());
    item.setContent("To be deleted");
    item.setUid("12345");

    bool inserted = MailItemDao::insert(item);
    QVERIFY(inserted);
    qint64 id = item.id();

    bool removed = MailItemDao::remove(id);
    QVERIFY(removed);

    MailItem found = MailItemDao::findById(id);
    QVERIFY(!found.isValid());
}

QTEST_MAIN(TestMailItemDao)
#include "test_mailitemdao.moc"
