#pragma once

#include <QMainWindow>
#include <QStackedWidget>
#include <QListWidget>
#include <QSplitter>
#include <QTreeView>
#include <QStatusBar>
#include <QProgressBar>
#include <QToolBar>
#include <QAction>

#include "ui/readerview.h"
#include "ui/maillistview.h"
#include "ui/composeview.h"
#include "ui/settingsview.h"
#include "ui/contactsview.h"
#include "ui/calendarview.h"
#include "ui/models/FolderListModel.h"
#include "ui/models/EmailListModel.h"
#include "services/accountservice.h"
#include "services/mailservice.h"

class MainMainWindow : public QMainWindow {
    Q_OBJECT

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

private slots:
    void onNavChanged(int index);
    void onFolderSelected(const QModelIndex &index);
    void onEmailSelected(int mailId);
    void onComposeRequested();
    void onReaderReplyRequested(const MailItem *item);
    void onNewMessage();
    void openMailInIndependentWindow(int mailId);
    void onAddAccountRequested();
    void onAccountEditRequested(int accountId);
    void onAccountDeleteRequested(int accountId);
    void onProgressChanged(int percent);
    void onStatusMessage(const QString &msg);

private:
    void setupUI();
    void setupSidebar();
    void setupMailPage();
    void connectModels();
    void createToolBar();
    void switchToPage(int pageIndex);

    // Navigation
    QListWidget *m_sidebar;
    QStackedWidget *m_stack;

    enum Page {
        PageMail = 0,
        PageCompose,
        PageSettings,
        PageContacts,
        PageCalendar
    };

    // Mail page widgets
    QWidget *m_mailPage;
    QSplitter *m_folderSplitter;
    QTreeView *m_folderTree;
    MailListView *m_mailListView;
    ReaderView *m_emailViewer;

    // Other pages
    ComposeView *m_composeView;
    SettingsView *m_settingsView;
    ContactsView *m_contactsView;
    CalendarView *m_calendarView;

    // Services & Models
    FolderListModel *m_folderModel;
    EmailListModel *m_emailModel;
    AccountService *m_accountService;
    MailService *m_mailService;

    QToolBar *m_toolBar;
    int m_currentFolderId;
    int m_currentMailId;

    QProgressBar *m_progressBar;
};