#ifndef ACCOUNTSETUPDIALOG_H
#define ACCOUNTSETUPDIALOG_H

#include <QDialog>
#include <QStackedWidget>
#include <QLabel>
#include <QLineEdit>
#include <QPushButton>
#include <QRadioButton>
#include <QCheckBox>
#include <QButtonGroup>
#include <QVBoxLayout>
#include <QHBoxLayout>
#include <QFormLayout>
#include <QIntValidator>
#include <QTimer>
#include "core/models/account.h"
#include "services/accountservice.h"

class AccountSetupDialog : public QDialog {
    Q_OBJECT

public:
    explicit AccountSetupDialog(AccountService *accountService, QWidget *parent = nullptr);
    ~AccountSetupDialog() override = default;

signals:
    void accountCreated(const Account &account);

private slots:
    void onNextClicked();
    void onBackClicked();
    void onCancelClicked();
    void onProviderSelected(int id);
    void startOAuthAuthentication();
    void onAccountAdded(const Account &account);
    void onAuthTimeout();

private:
    void setupUI();
    QWidget* createProviderPage();
    QWidget* createOAuthPage();
    QWidget* createImapPage();
    QWidget* createProgressPage();

    void goToPage(int page);
    void updateNavButtons();
    void showError(const QString &message);
    void showSuccess(const QString &message);
    void submitImapAccount();

    // Services
    AccountService *m_accountService;
    QTimer *m_authTimeoutTimer;

    // Navigation
    QStackedWidget *m_stack;
    QPushButton *m_btnBack;
    QPushButton *m_btnNext;
    QPushButton *m_btnCancel;

    enum Pages {
        PageProvider = 0,
        PageOAuth,
        PageImap,
        PageProgress
    };

    // Page 0: Provider selection
    QButtonGroup *m_providerGroup;

    // Page 1: OAuth
    QLineEdit *m_oauthEmailEdit;
    QPushButton *m_btnOAuthStart;
    QLabel *m_oauthStatusLabel;

    // Page 2: IMAP manual
    QLineEdit *m_imapEmailEdit;
    QLineEdit *m_imapNameEdit;
    QLineEdit *m_imapPasswordEdit;
    QLineEdit *m_imapHostEdit;
    QLineEdit *m_imapPortEdit;
    QLineEdit *m_smtpHostEdit;
    QLineEdit *m_smtpPortEdit;
    QCheckBox *m_sslCheckbox;

    // Page 3: Progress / Result
    QLabel *m_progressIcon;
    QLabel *m_progressText;
    QLabel *m_progressDetail;

    int m_selectedProvider; // 0=Gmail, 1=Outlook, 2=IMAP
    bool m_accountCreatedOk;
};

#endif // ACCOUNTSETUPDIALOG_H
