#pragma once

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

class TemplateEditorDialog : public QDialog
{
    Q_OBJECT

public:
    explicit TemplateEditorDialog(const Template& tmpl = Template(), qint64 accountId = -1, QWidget *parent = nullptr);
    Template getTemplate() const { return m_template; }

signals:
    void templateSaved(const Template& tmpl);

private slots:
    void onAddVariable();
    void onRemoveVariable();
    void onVariableTypeChanged(int row);
    void onAccept();
    void onPreview();

private:
    Template m_template;
    qint64 m_accountId;
    QLineEdit* m_nameEdit;
    QLineEdit* m_subjectEdit;
    QTextEdit* m_bodyHtmlEdit;
    QTextEdit* m_bodyTextEdit;
    QTableWidget* m_variablesTable;
    QCheckBox* m_isDefaultCheck;
    
    void setupUI();
    void populateFromTemplate();
    void setupVariableRow(int row, const TemplateVariable& var = TemplateVariable());
    TemplateVariable variableFromRow(int row) const;
};