#pragma once

#include <QWidget>
#include <QTabWidget>
#include <QVBoxLayout>
#include <QHBoxLayout>
#include <QPushButton>
#include <QLabel>
#include <QListWidget>
#include <QComboBox>
#include <QCheckBox>
#include <QRadioButton>
#include <QButtonGroup>
#include <QVariant>
#include <QTreeWidget>
#include <QLineEdit>
#include <QDialog>
#include <QColorDialog>
#include "services/accountservice.h"
#include "db/dao/common_structs.h"

class SettingsView : public QWidget {
    Q_OBJECT

public:
    explicit SettingsView(QWidget *parent = nullptr);
    ~SettingsView() override = default;

    void setAccountService(AccountService *service);

signals:
    void accountAddRequested();
    void accountEditRequested(int accountId);
    void accountDeleteRequested(int accountId);
    void themeChanged(const QString &theme);
    void settingChanged(const QString &key, const QVariant &value);
    
    // Categories
    void categoryAddRequested(const Category &cat);
    void categoryEditRequested(const Category &cat);
    void categoryDeleteRequested(qint64 categoryId);
    
    // Rules
    void ruleAddRequested(const Rule &rule);
    void ruleEditRequested(const Rule &rule);
    void ruleDeleteRequested(qint64 ruleId);
    
    // Templates
    void templateAddRequested(const Template &tmpl);
    void templateEditRequested(const Template &tmpl);
    void templateDeleteRequested(qint64 templateId);
    
    // Signatures
    void signatureAddRequested(const Signature &sig);
    void signatureEditRequested(const Signature &sig);
    void signatureDeleteRequested(qint64 signatureId);

private slots:
    void onAccountListChanged();
    void loadAccounts();
    void onEditClicked();
    void onDeleteClicked();
    void onAccountSelectionChanged();
    
    // Categories
    void onCategorySelectionChanged();
    void onCategoryAddClicked();
    void onCategoryEditClicked();
    void onCategoryDeleteClicked();
    
    // Rules
    void onRuleSelectionChanged();
    void onRuleAddClicked();
    void onRuleEditClicked();
    void onRuleDeleteClicked();
    void onRuleToggleEnabled();
    
    // Templates
    void onTemplateSelectionChanged();
    void onTemplateAddClicked();
    void onTemplateEditClicked();
    void onTemplateDeleteClicked();
    
    // Signatures
    void onSignatureSelectionChanged();
    void onSignatureAddClicked();
    void onSignatureEditClicked();
    void onSignatureDeleteClicked();

private:
    void setupUI();
    QWidget* createAccountsTab();
    QWidget* createGeneralTab();
    QWidget* createAppearanceTab();
    QWidget* createCategoriesTab();
    QWidget* createRulesTab();
    QWidget* createTemplatesTab();
    QWidget* createSignaturesTab();
    
    // Category helpers
    void loadCategories();
    void refreshCategoryTree();
    QTreeWidgetItem* createCategoryTreeItem(const Category &cat, QTreeWidgetItem *parent = nullptr);
    
    // Rule helpers
    void loadRules();
    QString ruleToString(const Rule &rule) const;
    
    // Template helpers
    void loadTemplates();
    QString templateToString(const Template &tmpl) const;
    
    // Signature helpers
    void loadSignatures();
    QString signatureToString(const Signature &sig) const;

    QTabWidget *m_tabWidget;
    QListWidget *m_accountList;
    QComboBox *m_syncIntervalCombo;
    QButtonGroup *m_themeGroup;
    QPushButton *m_editBtn;
    QPushButton *m_addBtn;
    QPushButton *m_deleteBtn;

    AccountService *m_accountService;
    int m_selectedAccountId;

    // Categories
    QTreeWidget *m_categoryTree;
    QPushButton *m_categoryAddBtn;
    QPushButton *m_categoryEditBtn;
    QPushButton *m_categoryDeleteBtn;
    
    // Rules
    QListWidget *m_ruleList;
    QPushButton *m_ruleAddBtn;
    QPushButton *m_ruleEditBtn;
    QPushButton *m_ruleDeleteBtn;
    QPushButton *m_ruleToggleBtn;
    
    // Templates
    QListWidget *m_templateList;
    QPushButton *m_templateAddBtn;
    QPushButton *m_templateEditBtn;
    QPushButton *m_templateDeleteBtn;
    
    // Signatures
    QListWidget *m_signatureList;
    QPushButton *m_signatureAddBtn;
    QPushButton *m_signatureEditBtn;
    QPushButton *m_signatureDeleteBtn;
};