#pragma once

#include <QDialog>
#include "db/dao/ruledao.h"
#include <QTableWidget>
#include <QLineEdit>
#include <QTextEdit>
#include <QSpinBox>
#include <QCheckBox>
#include <QComboBox>
#include <QLabel>

class RuleEditorDialog : public QDialog
{
    Q_OBJECT

public:
    explicit RuleEditorDialog(const Rule& rule = Rule(), qint64 accountId = -1, QWidget *parent = nullptr);
    Rule getRule() const { return m_rule; }

signals:
    void ruleSaved(const Rule& rule);

private slots:
    void onAddCondition();
    void onRemoveCondition();
    void onAddAction();
    void onRemoveAction();
    void onConditionFieldChanged(int row);
    void onActionTypeChanged(int row);
    void onAccept();
    void loadAccounts();

private:
    Rule m_rule;
    qint64 m_accountId;
    QTableWidget* m_conditionsTable;
    QTableWidget* m_actionsTable;
    QLineEdit* m_nameEdit;
    QTextEdit* m_descEdit;
    QSpinBox* m_prioritySpin;
    QCheckBox* m_enabledCheck;
    QCheckBox* m_matchAllCheck;
    QComboBox* m_accountCombo;
    
    void setupUI();
    void populateFromRule();
    void setupConditionRow(int row, const RuleCondition& cond = RuleCondition());
    void setupActionRow(int row, const RuleAction& act = RuleAction());
    RuleCondition conditionFromRow(int row) const;
    RuleAction actionFromRow(int row) const;
    QWidget* createParameterWidget(const RuleAction& act);
    void updateActionDetails(const RuleAction& act, QLabel* label);
};