#include <QtTest>
#include <QSignalSpy>
#include "../../../src/model/shortcut-model.h"
#include "../../../src/model/shortcut-filter-model.h"
#include "../../../src/model/edit-shortcut-model.h"
#include "../../../src/model/plugin-state.h"

using namespace UkuiShortcut;

class shortcutModel_test : public QObject
{
    Q_OBJECT

private slots:
    void initTestCase();
    void cleanupTestCase();

    // Role enum value tests (no construction needed)
    void test_role_disabled_isZero();
    void test_role_name_isOne();
    void test_role_statusName_isTwo();
    void test_role_widgetId_isThree();
    void test_role_icon_isFour();
    void test_role_tooltip_isFive();
    void test_role_color_isSix();
    void test_role_value_isSeven();
    void test_role_maxValue_isEight();
    void test_role_menu_isNine();
    void test_role_row_isTen();
    void test_role_column_isEleven();
    void test_role_itemType_isTwelve();
    void test_role_isEnable_isThirteen();
    void test_role_isVisible_isFourteen();
    void test_role_addState_isFifteen();
    void test_role_pluginIndex_isSixteen();
    void test_role_pluginId_isSeventeen();
    void test_role_fixState_isEighteen();

    // Construction and API tests
    void test_construction_notNull();
    void test_rowCount_validIndex();
    void test_rowCount_invalidIndex();
    void test_roleNames_notEmpty();
    void test_roleNames_containsDisabled();
    void test_roleNames_containsName();
    void test_roleNames_containsIcon();
    void test_disableValueUpdate_true();
    void test_disableValueUpdate_false();
    void test_disableValueUpdate_toggle();
    void test_getModel_addReturnsNonNull();
    void test_getModel_notAddReturnsNonNull();
    void test_prosessBarModel_notNull();
    void test_editIconName_notEmpty();

private:
    ShortcutModel *m_model = nullptr;
};

void shortcutModel_test::initTestCase()
{
    m_model = new ShortcutModel(this);
}

void shortcutModel_test::cleanupTestCase()
{
    delete m_model;
    m_model = nullptr;
}

// --- Role enum value tests ---

void shortcutModel_test::test_role_disabled_isZero()
{
    QCOMPARE(static_cast<int>(ShortcutModel::Disabled), 0);
}

void shortcutModel_test::test_role_name_isOne()
{
    QCOMPARE(static_cast<int>(ShortcutModel::Name), 1);
}

void shortcutModel_test::test_role_statusName_isTwo()
{
    QCOMPARE(static_cast<int>(ShortcutModel::StatusName), 2);
}

void shortcutModel_test::test_role_widgetId_isThree()
{
    QCOMPARE(static_cast<int>(ShortcutModel::WidgetId), 3);
}

void shortcutModel_test::test_role_icon_isFour()
{
    QCOMPARE(static_cast<int>(ShortcutModel::Icon), 4);
}

void shortcutModel_test::test_role_tooltip_isFive()
{
    QCOMPARE(static_cast<int>(ShortcutModel::ToolTip), 5);
}

void shortcutModel_test::test_role_color_isSix()
{
    QCOMPARE(static_cast<int>(ShortcutModel::Color), 6);
}

void shortcutModel_test::test_role_value_isSeven()
{
    QCOMPARE(static_cast<int>(ShortcutModel::Value), 7);
}

void shortcutModel_test::test_role_maxValue_isEight()
{
    QCOMPARE(static_cast<int>(ShortcutModel::MaxValue), 8);
}

void shortcutModel_test::test_role_menu_isNine()
{
    QCOMPARE(static_cast<int>(ShortcutModel::Menu), 9);
}

void shortcutModel_test::test_role_row_isTen()
{
    QCOMPARE(static_cast<int>(ShortcutModel::Row), 10);
}

void shortcutModel_test::test_role_column_isEleven()
{
    QCOMPARE(static_cast<int>(ShortcutModel::Column), 11);
}

void shortcutModel_test::test_role_itemType_isTwelve()
{
    QCOMPARE(static_cast<int>(ShortcutModel::ItemType), 12);
}

void shortcutModel_test::test_role_isEnable_isThirteen()
{
    QCOMPARE(static_cast<int>(ShortcutModel::IsEnable), 13);
}

void shortcutModel_test::test_role_isVisible_isFourteen()
{
    QCOMPARE(static_cast<int>(ShortcutModel::IsVisible), 14);
}

void shortcutModel_test::test_role_addState_isFifteen()
{
    QCOMPARE(static_cast<int>(ShortcutModel::AddState), 15);
}

void shortcutModel_test::test_role_pluginIndex_isSixteen()
{
    QCOMPARE(static_cast<int>(ShortcutModel::PluginIndex), 16);
}

void shortcutModel_test::test_role_pluginId_isSeventeen()
{
    QCOMPARE(static_cast<int>(ShortcutModel::PluginId), 17);
}

void shortcutModel_test::test_role_fixState_isEighteen()
{
    QCOMPARE(static_cast<int>(ShortcutModel::FixState), 18);
}

// --- Construction and API tests ---

void shortcutModel_test::test_construction_notNull()
{
    QVERIFY(m_model != nullptr);
}

void shortcutModel_test::test_rowCount_validIndex()
{
    int count = m_model->rowCount(QModelIndex());
    QVERIFY(count >= 0);
}

void shortcutModel_test::test_rowCount_invalidIndex()
{
    // ShortcutModel does not restrict rowCount by parent validity;
    // calling with a valid child index still returns the total item count.
    QModelIndex child = m_model->index(0, 0);
    int count = m_model->rowCount(child);
    QVERIFY(count >= 0);
}

void shortcutModel_test::test_roleNames_notEmpty()
{
    QHash<int, QByteArray> names = m_model->roleNames();
    QVERIFY(!names.isEmpty());
}

void shortcutModel_test::test_roleNames_containsDisabled()
{
    QHash<int, QByteArray> names = m_model->roleNames();
    QVERIFY(names.contains(ShortcutModel::Disabled));
}

void shortcutModel_test::test_roleNames_containsName()
{
    QHash<int, QByteArray> names = m_model->roleNames();
    QVERIFY(names.contains(ShortcutModel::Name));
}

void shortcutModel_test::test_roleNames_containsIcon()
{
    QHash<int, QByteArray> names = m_model->roleNames();
    QVERIFY(names.contains(ShortcutModel::Icon));
}

void shortcutModel_test::test_disableValueUpdate_true()
{
    m_model->disableValueUpdate(true);
    QVERIFY(true); // no crash
}

void shortcutModel_test::test_disableValueUpdate_false()
{
    m_model->disableValueUpdate(false);
    QVERIFY(true); // no crash
}

void shortcutModel_test::test_disableValueUpdate_toggle()
{
    m_model->disableValueUpdate(true);
    m_model->disableValueUpdate(false);
    m_model->disableValueUpdate(true);
    QVERIFY(true); // no crash
}

void shortcutModel_test::test_getModel_addReturnsNonNull()
{
    EditShortcutModel *model = m_model->getModel(PluginStateType::Add);
    QVERIFY(model != nullptr);
}

void shortcutModel_test::test_getModel_notAddReturnsNonNull()
{
    EditShortcutModel *model = m_model->getModel(PluginStateType::NotAdd);
    QVERIFY(model != nullptr);
}

void shortcutModel_test::test_prosessBarModel_notNull()
{
    ShortcutFilterModel *filterModel = m_model->prosessBarModel();
    QVERIFY(filterModel != nullptr);
}

void shortcutModel_test::test_editIconName_notEmpty()
{
    QString iconName = m_model->editIconName();
    // The icon name should be a non-null string (may be empty if not configured)
    QVERIFY(!iconName.isNull());
}

QTEST_MAIN(shortcutModel_test)
#include "shortcut-model-test.moc"
