#ifndef COMPOSE_VIEW_H
#define COMPOSE_VIEW_H

#include <QWidget>
#include <QTextEdit>
#include <QVBoxLayout>
#include <QToolBar>
#include <QPushButton>
#include <QLabel>
#include <QFrame>
#include <QMenu>
#include <QAction>
#include <QToolButton>
#include <QDateTimeEdit>
#include <QFontComboBox>
#include <QSpinBox>
#include <QComboBox>
#include <QListWidget>
#include "models/EmailCompositionModel.h"
#include "../../thirdparty/tags/include/tags_line_edit.hpp"
#include "services/accountservice.h"
#include "db/dao/templatedao.h"

class AccountService;

#include "richtexteditor.h"

class ComposeView : public QWidget {
    Q_OBJECT
public:
    explicit ComposeView(QWidget *parent = nullptr);
    ~ComposeView() override;

    void setAccountService(AccountService *service);

signals:
    void compositionFinished();
    void detachRequested(QWidget *widget);
    void sendRequested(const QString &to, const QString &cc, const QString &bcc,
                       const QString &subject, const QString &body,
                       const QDateTime &scheduleTime,
                       const QString &fromAddress,
                       const QStringList &attachmentPaths);
    void discardRequested();
    void statusMessageRequested(const QString& message);

public slots:
    void initializeComposition();
    void startNewEmail(const QString &initialRecipient);
    void setTo(const QString &to);
    void setSubject(const QString &subject);
    void setBody(const QString &body);
    void applyTemplate(const Template& tmpl);
    QString getBody() const { return m_bodyEditor->toHtml(); }

private slots:
    void onCcToggle();
    void onBccToggle();
    void onSendClicked();
    void onScheduleClicked();
    void onDetachClicked();
    void onAccountChanged(int index);
    void onSignatureComboChanged(int index);
    void onSignatureClicked();
    void onSignatureEditRequested();
    void onAddAttachmentClicked();
    void onRemoveAttachmentClicked();
    void onTemplateClicked();

private:
    void setupUI();
    void populateAccountCombo();
    void populateSignatureCombo(qint64 accountId);
    void loadSignatureForCurrentAccount();
    QString getDefaultSignatureForAccount(qint64 accountId);
    void statusBarMessage(const QString& msg);

    EmailCompositionModel *m_compositionModel;
    AccountService *m_accountService = nullptr;
    // UI components
    QLabel *m_fromLabel = nullptr;
    QComboBox *m_accountCombo = nullptr;
    QComboBox *m_signatureCombo = nullptr;
    everload_tags::TagsLineEdit *m_toField = nullptr;
    everload_tags::TagsLineEdit *m_ccField = nullptr;
    everload_tags::TagsLineEdit *m_bccField = nullptr;
    QLineEdit *m_subjectField = nullptr;
    RichTextEditor *m_bodyEditor = nullptr;
    QPushButton *m_detachButton = nullptr;
    QWidget *m_ccRow = nullptr;
    QWidget *m_bccRow = nullptr;
    QWidget *m_schedulePanel = nullptr;
    QPushButton *m_ccButton = nullptr;
    QPushButton *m_bccButton = nullptr;
    QPushButton *m_hideCcButton = nullptr;
    QPushButton *m_hideBccButton = nullptr;
    QDateTimeEdit *m_schedulePicker = nullptr;
    QPushButton *m_scheduleSendButton = nullptr;
    QPushButton *m_discardButton = nullptr;
    QMenu *m_sendMenu = nullptr;
    QAction *m_sendNowAction = nullptr;
    QAction *m_scheduleAction = nullptr;
    QToolButton *m_sendSplit = nullptr;
    bool m_ccVisible = false;
    bool m_bccVisible = false;
    int m_currentAccountId = -1;

    // Attachment UI
    QToolButton *m_attachButton = nullptr;
    QToolButton *m_templateButton = nullptr;
    QListWidget *m_attachmentList = nullptr;
    QToolButton *m_removeAttachmentButton = nullptr;
    QStringList m_attachmentFiles;
};
#endif // COMPOSE_VIEW_H
