#include "ui/composeview.h"
#include <QFrame>
#include <QFileDialog>
#include <QInputDialog>
#include <QTextList>
#include <QTextTable>
#include <QTextCursor>
#include <QTextBlockFormat>
#include <QTextCharFormat>

// ===================== RichTextEditor =====================

RichTextEditor::RichTextEditor(QWidget *parent) : QTextEdit(parent) {
    setAcceptRichText(true);
    setPlaceholderText("Write your message here...");
}

void RichTextEditor::setupToolbar(QVBoxLayout *layout) {
    m_toolbar = new QToolBar("Formatting");
    m_toolbar->setIconSize(QSize(16, 16));
    m_toolbar->setStyleSheet(
        "QToolBar { background: #f5f5f7; border: 1px solid #d1d1d6; border-radius: 4px; spacing: 2px; padding: 2px; }"
        "QToolButton { padding: 4px 6px; border-radius: 3px; }"
        "QToolButton:hover { background: #e0e0e0; }"
        "QToolButton:checked { background: #bbdefb; }"
    );

    // Font family combo
    m_fontCombo = new QFontComboBox();
    m_fontCombo->setFixedWidth(150);
    connect(m_fontCombo, &QFontComboBox::currentFontChanged, this, &RichTextEditor::onFontChanged);
    m_toolbar->addWidget(m_fontCombo);

    // Font size spin
    m_fontSizeSpin = new QSpinBox();
    m_fontSizeSpin->setRange(8, 72);
    m_fontSizeSpin->setValue(14);
    m_fontSizeSpin->setFixedWidth(50);
    connect(m_fontSizeSpin, QOverload<int>::of(&QSpinBox::valueChanged), this, &RichTextEditor::onFontSizeChanged);
    m_toolbar->addWidget(m_fontSizeSpin);

    m_toolbar->addSeparator();

    // Bold / Italic / Underline
    QAction *boldAct = m_toolbar->addAction("B");
    boldAct->setCheckable(true);
    QFont boldFont = boldAct->font(); boldFont.setBold(true); boldAct->setFont(boldFont);
    connect(boldAct, &QAction::triggered, this, &RichTextEditor::onBold);

    QAction *italicAct = m_toolbar->addAction("I");
    italicAct->setCheckable(true);
    QFont italicFont = italicAct->font(); italicFont.setItalic(true); italicAct->setFont(italicFont);
    connect(italicAct, &QAction::triggered, this, &RichTextEditor::onItalic);

    QAction *underlineAct = m_toolbar->addAction("U");
    underlineAct->setCheckable(true);
    QFont uFont = underlineAct->font(); uFont.setUnderline(true); underlineAct->setFont(uFont);
    connect(underlineAct, &QAction::triggered, this, &RichTextEditor::onUnderline);

    m_toolbar->addSeparator();

    // Alignment
    QAction *alignLeft = m_toolbar->addAction("L");
    connect(alignLeft, &QAction::triggered, this, &RichTextEditor::onAlignLeft);

    QAction *alignCenter = m_toolbar->addAction("C");
    connect(alignCenter, &QAction::triggered, this, &RichTextEditor::onAlignCenter);

    QAction *alignRight = m_toolbar->addAction("R");
    connect(alignRight, &QAction::triggered, this, &RichTextEditor::onAlignRight);

    QAction *alignJustify = m_toolbar->addAction("J");
    connect(alignJustify, &QAction::triggered, this, &RichTextEditor::onAlignJustify);

    m_toolbar->addSeparator();

    // Lists
    QAction *bulletAct = m_toolbar->addAction("Bullets");
    connect(bulletAct, &QAction::triggered, this, &RichTextEditor::onBulletList);

    QAction *numAct = m_toolbar->addAction("1. List");
    connect(numAct, &QAction::triggered, this, &RichTextEditor::onNumberedList);

    m_toolbar->addSeparator();

    // Indent / Outdent
    QAction *indentAct = m_toolbar->addAction("Indent");
    connect(indentAct, &QAction::triggered, this, &RichTextEditor::onIndent);

    QAction *outdentAct = m_toolbar->addAction("Outdent");
    connect(outdentAct, &QAction::triggered, this, &RichTextEditor::onOutdent);

    m_toolbar->addSeparator();

    // Insert image
        QAction *imgAct = m_toolbar->addAction("Img");
        connect(imgAct, &QAction::triggered, this, &RichTextEditor::onInsertImage);

        // Insert table
        QAction *tableAct = m_toolbar->addAction("Tbl");
        connect(tableAct, &QAction::triggered, this, &RichTextEditor::onInsertTable);

        // Signature button
            m_signatureButton = new QToolButton();
            m_signatureButton->setText("Signature");
            m_signatureButton->setToolButtonStyle(Qt::ToolButtonTextOnly);
            m_signatureButton->setPopupMode(QToolButton::MenuButtonPopup);
            m_signatureMenu = new QMenu(m_signatureButton);
            m_signatureButton->setMenu(m_signatureMenu);
            m_signatureButton->setStyleSheet(
                "QToolButton { background: #f5f5f7; border: 1px solid #d1d1d6; border-radius: 3px; padding: 4px 6px; }"
                "QToolButton:hover { background: #e0e0e0; }"
                "QToolButton::menu-button { border-left: 1px solid rgba(0,0,0,0.1); width: 12px; }"
            );
            connect(m_signatureButton, &QToolButton::clicked, this, &RichTextEditor::signatureClicked);
            m_toolbar->addWidget(m_signatureButton);

        m_toolbar->addSeparator();

        layout->addWidget(m_toolbar);
}

void RichTextEditor::onBold() {
    QTextCharFormat fmt;
    fmt.setFontWeight(textCursor().charFormat().fontWeight() == QFont::Bold ? QFont::Normal : QFont::Bold);
    mergeCurrentCharFormat(fmt);
    setFocus();
}

void RichTextEditor::onItalic() {
    QTextCharFormat fmt;
    fmt.setFontItalic(!textCursor().charFormat().fontItalic());
    mergeCurrentCharFormat(fmt);
    setFocus();
}

void RichTextEditor::onUnderline() {
    QTextCharFormat fmt;
    fmt.setFontUnderline(!textCursor().charFormat().fontUnderline());
    mergeCurrentCharFormat(fmt);
    setFocus();
}

void RichTextEditor::onBulletList() {
    QTextCursor cursor = textCursor();
    QTextList *list = cursor.currentList();
    if (list && list->format().style() == QTextListFormat::ListDisc) {
        // Remove list
        QTextBlockFormat bfmt;
        bfmt.setIndent(0);
        cursor.setBlockFormat(bfmt);
        list->remove(cursor.block());
    } else {
        QTextListFormat listFormat;
        listFormat.setStyle(QTextListFormat::ListDisc);
        cursor.createList(listFormat);
    }
}

void RichTextEditor::onNumberedList() {
    QTextCursor cursor = textCursor();
    QTextList *list = cursor.currentList();
    if (list && list->format().style() == QTextListFormat::ListDecimal) {
        QTextBlockFormat bfmt;
        bfmt.setIndent(0);
        cursor.setBlockFormat(bfmt);
        list->remove(cursor.block());
    } else {
        QTextListFormat listFormat;
        listFormat.setStyle(QTextListFormat::ListDecimal);
        cursor.createList(listFormat);
    }
}

void RichTextEditor::onIndent() {
    QTextCursor cursor = textCursor();
    QTextBlockFormat bfmt = cursor.blockFormat();
    bfmt.setIndent(bfmt.indent() + 1);
    cursor.setBlockFormat(bfmt);
}

void RichTextEditor::onOutdent() {
    QTextCursor cursor = textCursor();
    QTextBlockFormat bfmt = cursor.blockFormat();
    int indent = bfmt.indent();
    if (indent > 0) {
        bfmt.setIndent(indent - 1);
        cursor.setBlockFormat(bfmt);
    }
}

void RichTextEditor::onAlignLeft() {
    QTextCursor cursor = textCursor();
    QTextBlockFormat bfmt = cursor.blockFormat();
    bfmt.setAlignment(Qt::AlignLeft);
    cursor.setBlockFormat(bfmt);
}

void RichTextEditor::onAlignCenter() {
    QTextCursor cursor = textCursor();
    QTextBlockFormat bfmt = cursor.blockFormat();
    bfmt.setAlignment(Qt::AlignCenter);
    cursor.setBlockFormat(bfmt);
}

void RichTextEditor::onAlignRight() {
    QTextCursor cursor = textCursor();
    QTextBlockFormat bfmt = cursor.blockFormat();
    bfmt.setAlignment(Qt::AlignRight);
    cursor.setBlockFormat(bfmt);
}

void RichTextEditor::onAlignJustify() {
    QTextCursor cursor = textCursor();
    QTextBlockFormat bfmt = cursor.blockFormat();
    bfmt.setAlignment(Qt::AlignJustify);
    cursor.setBlockFormat(bfmt);
}

void RichTextEditor::onFontChanged(const QFont &font) {
    QTextCharFormat fmt;
    fmt.setFontFamilies({font.family()});
    mergeCurrentCharFormat(fmt);
}

void RichTextEditor::onFontSizeChanged(int size) {
    QTextCharFormat fmt;
    fmt.setFontPointSize(size);
    mergeCurrentCharFormat(fmt);
}

void RichTextEditor::onInsertImage() {
    QString filePath = QFileDialog::getOpenFileName(this, "Insert Image", QString(), "Images (*.png *.jpg *.jpeg *.gif *.bmp)");
    if (filePath.isEmpty()) return;
    QTextCursor cursor = textCursor();
    QTextImageFormat imgFmt;
    imgFmt.setName(filePath);
    // Scale down if too large
    QPixmap pm(filePath);
    if (pm.width() > 600) {
        imgFmt.setWidth(600);
        imgFmt.setHeight(pm.height() * 600 / pm.width());
    }
    cursor.insertImage(imgFmt);
}

void RichTextEditor::onInsertTable() {
    bool ok;
    int rows = QInputDialog::getInt(this, "Table Rows", "Rows:", 3, 1, 50, 1, &ok);
    if (!ok) return;
    int cols = QInputDialog::getInt(this, "Table Columns", "Columns:", 3, 1, 20, 1, &ok);
    if (!ok) return;

    QTextCursor cursor = textCursor();
    QTextTableFormat tableFmt;
    tableFmt.setBorder(1);
    tableFmt.setBorderStyle(QTextFrameFormat::BorderStyle_Solid);
    tableFmt.setCellPadding(4);
    tableFmt.setCellSpacing(0);
    tableFmt.setWidth(QTextLength(QTextLength::PercentageLength, 100));
    cursor.insertTable(rows, cols, tableFmt);
}

ComposeView::ComposeView(QWidget *parent) : QWidget(parent) {
    setupUI();
    // Connect the rich text editor's signature signal to our slot
    connect(m_bodyEditor, &RichTextEditor::signatureClicked, this, &ComposeView::onSignatureClicked);
}

ComposeView::~ComposeView() {
    // Destructor implementation
}
void ComposeView::onSignatureClicked() {
    // This slot is called when the signature button in the rich text editor is clicked
    // The actual menu handling is done in RichTextEditor, so we just need to
    // handle any ComposeView-specific logic here if needed
}

void ComposeView::onSignatureEditRequested() {
    // This slot will be connected to the "Edit Signature..." action in the menu
    // For now, we'll just show a simple input dialog
    QString currentSignature = "";
    // In a real implementation, we would get the current signature for the selected account
    // For now, we'll use an empty string
    
    bool ok;
    QString newSignature = QInputDialog::getMultiLineText(this, "Edit Signature", 
                                                         "Enter your signature:", 
                                                         currentSignature, &ok);
    if (ok && !newSignature.isEmpty()) {
        // In a real implementation, we would save this signature for the selected account
        // For now, we'll just insert it at the cursor position
        QTextCursor cursor = m_bodyEditor->textCursor();
        cursor.insertHtml(newSignature);
    }
}

void ComposeView::setAccountService(AccountService *service)
{
    m_accountService = service;
    populateAccountCombo();
    // Update completer models for address widgets with known emails
    if (m_accountService) {
        QStringList knownEmails;
        const QVector<Account> accounts = m_accountService->getAllAccounts();
        for (const Account &acc : accounts) {
            knownEmails << acc.email();
        }
        // Set completion model for each widget
        QStringListModel *model = new QStringListModel(knownEmails, this);
        QCompleter *comp = new QCompleter(model, this);
        comp->setCaseSensitivity(Qt::CaseInsensitive);
        if (m_toField) m_toField->setCompleter(comp);
        // For cc and bcc we need separate completers but can share model
        QCompleter *comp2 = new QCompleter(model, this);
        comp2->setCaseSensitivity(Qt::CaseInsensitive);
        if (m_ccField) m_ccField->setCompleter(comp2);
        QCompleter *comp3 = new QCompleter(model, this);
        comp3->setCaseSensitivity(Qt::CaseInsensitive);
        if (m_bccField) m_bccField->setCompleter(comp3);
    }
}

void ComposeView::populateAccountCombo()
{
    if (!m_accountService) {
        qWarning() << "AccountService not set";
        return;
    }

    m_accountCombo->clear();
    m_accountCombo->addItem(tr("Select Account..."), QVariant());

    QVector<Account> accounts = m_accountService->getAllAccounts();
    for (const Account &account : accounts) {
        m_accountCombo->addItem(account.email(), account.id());
    }

    // Select the first account by default if there are accounts
    if (accounts.size() > 0) {
        m_accountCombo->setCurrentIndex(1); // Skip the placeholder item
        m_currentAccountId = accounts.first().id();
    }
}

void ComposeView::onAccountChanged(int index)
{
    if (index <= 0) {
        // Placeholder item selected or invalid index
        m_currentAccountId = -1;
        return;
    }

    m_currentAccountId = m_accountCombo->itemData(index).toInt();
    loadSignatureForCurrentAccount();
}

void ComposeView::loadSignatureForCurrentAccount()
{
    if (!m_accountService || m_currentAccountId <= 0) {
        // Clear signature if no account selected
        m_bodyEditor->clear();
        return;
    }

    Account* account = m_accountService->findAccountById(m_currentAccountId);
    if (account) {
        // For now, we'll just clear the editor since we don't have a signature field in Account yet
        // In a real implementation, we would retrieve and display the signature
        m_bodyEditor->clear();
    } else {
        m_bodyEditor->clear();
    }
}


void ComposeView::setupUI()
{
    QVBoxLayout *mainLayout = new QVBoxLayout(this);
    mainLayout->setContentsMargins(20,15,20,15);
    mainLayout->setSpacing(8);

    // Top row: Account selector and detach button
    QHBoxLayout *topLayout = new QHBoxLayout();
    m_accountLabel = new QLabel(tr("From:"), this);
    m_accountLabel->setFixedWidth(40);
    m_accountLabel->setStyleSheet(QStringLiteral("font-weight: bold; color: #555;"));
    m_accountCombo = new QComboBox(this);
    m_accountCombo->setFixedWidth(180);
    m_accountCombo->setStyleSheet(
        "QComboBox { border: 1px solid #d1d1d6; border-radius: 4px; padding: 6px 8px; "
        "background: white; min-height: 20px; }"

        "QComboBox:hover { border-color: #bdbdbd; }"

        "QComboBox:focus { border-color: #1976D2; }"
    );
    m_detachButton = new QPushButton(this);
    m_detachButton->setToolTip(tr("Detach compose window"));
    m_detachButton->setText(QStringLiteral("⤢"));
    m_detachButton->setFixedSize(28,28);
    m_detachButton->setStyleSheet(
        "QPushButton { background: transparent; border: 1px solid #d1d1d6; border-radius: 4px; "
        "padding: 6px; color: #555; font-size: 14px; }"

        "QPushButton:hover { background: #f0f0f0; }"

    );
    connect(m_detachButton, &QPushButton::clicked, this, &ComposeView::onDetachClicked);

    topLayout->addWidget(m_accountLabel);
    topLayout->addWidget(m_accountCombo, 1);
    topLayout->addWidget(m_detachButton);
    mainLayout->addLayout(topLayout);

    // Separator line
    QFrame *line1 = new QFrame(this);
    line1->setFrameShape(QFrame::HLine);
    line1->setStyleSheet(QStringLiteral("color: #e0e0e0;"));
    mainLayout->addWidget(line1);

    // To row
    QHBoxLayout *toLayout = new QHBoxLayout();
    QLabel *toLabel = new QLabel(tr("To:"), this);
    toLabel->setFixedWidth(40);
    toLabel->setStyleSheet(QStringLiteral("font-weight: bold; color: #555;"));
    m_toField = new QLineEdit(this);
    m_toField->setPlaceholderText(tr("To recipients"));
    toLayout->addWidget(toLabel);
    toLayout->addWidget(m_toField, 1);
    mainLayout->addLayout(toLayout);

    // CC/BCC toggle buttons
    QHBoxLayout *toggleLayout = new QHBoxLayout();
    m_ccButton = new QPushButton(tr("CC"), this);
    m_ccButton->setCheckable(true);
    m_ccButton->setChecked(false);
    connect(m_ccButton, &QPushButton::toggled, this, &ComposeView::onCcToggled);
    m_bccButton = new QPushButton(tr("BCC"), this);
    m_bccButton->setCheckable(true);
    m_bccButton->setChecked(false);
    connect(m_bccButton, &QPushButton::toggled, this, &ComposeView::onBccToggled);
    toggleLayout->addWidget(m_ccButton);
    toggleLayout->addWidget(m_bccButton);
    toggleLayout->addStretch();
    mainLayout->addLayout(toggleLayout);

    // Cc row (hidden by default)
    m_ccRow = new QWidget(this);
    QHBoxLayout *ccLayout = new QHBoxLayout(m_ccRow);
    ccLayout->setContentsMargins(0,0,0,0);
    QLabel *ccLabel = new QLabel(tr("Cc:"), this);
    ccLabel->setFixedWidth(40);
    ccLabel->setStyleSheet(QStringLiteral("color: #555;"));
    m_ccField = new QLineEdit(this);
    m_ccField->setPlaceholderText(tr("Cc recipients"));
    ccLayout->addWidget(ccLabel);
    ccLayout->addWidget(m_ccField, 1);
    m_ccRow->setVisible(false);
    mainLayout->addWidget(m_ccRow);

    // Bcc row (hidden by default)
    m_bccRow = new QWidget(this);
    QHBoxLayout *bccLayout = new QHBoxLayout(m_bccRow);
    bccLayout->setContentsMargins(0,0,0,0);
    QLabel *bccLabel = new QLabel(tr("Bcc:"), this);
    bccLabel->setFixedWidth(40);
    bccLabel->setStyleSheet(QStringLiteral("color: #555;"));
    m_bccField = new QLineEdit(this);
    m_bccField->setPlaceholderText(tr("Bcc recipients"));
    bccLayout->addWidget(bccLabel);
    bccLayout->addWidget(m_bccField, 1);
    m_bccRow->setVisible(false);
    mainLayout->addWidget(m_bccRow);

    // Separator
    QFrame *line2 = new QFrame(this);
    line2->setFrameShape(QFrame::HLine);
    line2->setStyleSheet(QStringLiteral("color: #e0e0e0;"));
    mainLayout->addWidget(line2);

    // Subject field
    QLabel *subjectLabel = new QLabel(tr("Subject:"), this);
    subjectLabel->setFixedWidth(50);
    subjectLabel->setStyleSheet(QStringLiteral("font-weight: bold; color: #555;"));
    m_subjectField = new QLineEdit(this);
    m_subjectField->setPlaceholderText(tr("Subject"));
    m_subjectField->setFixedHeight(36);
    QFont subjectFont = m_subjectField->font();
    subjectFont.setPointSize(14);
    subjectFont.setBold(true);
    m_subjectField->setFont(subjectFont);
    QHBoxLayout *subjectLayout = new QHBoxLayout();
    subjectLayout->addWidget(subjectLabel);
    subjectLayout->addWidget(m_subjectField, 1);
    mainLayout->addLayout(subjectLayout);

    // Body editor with toolbar
    m_bodyEditor = new RichTextEditor(this);
    m_bodyEditor->setupToolbar(mainLayout);
    mainLayout->addWidget(m_bodyEditor, 1);

    // Schedule panel (hidden by default)
    m_schedulePanel = new QWidget(this);
    QHBoxLayout *scheduleLayout = new QHBoxLayout(m_schedulePanel);
    scheduleLayout->setContentsMargins(0,0,0,0);
    QLabel *scheduleLabel = new QLabel(tr("Send at:"), this);
    scheduleLabel->setStyleSheet(QStringLiteral("color: #555; font-weight: bold;"));
    m_schedulePicker = new QDateTimeEdit(QDateTime::currentDateTime().addSecs(3600), this);
    m_schedulePicker->setCalendarPopup(true);
    m_schedulePicker->setDisplayFormat(QStringLiteral("dd/MM/yyyy hh:mm AP"));
    scheduleLayout->addWidget(scheduleLabel);
    scheduleLayout->addWidget(m_schedulePicker);
    scheduleLayout->addStretch();
    m_scheduleSendButton = new QPushButton(tr("Schedule Send"), this);
    m_scheduleSendButton->setStyleSheet(
        "QPushButton { background-color: #1976D2; color: white; border: none; border-radius: 4px; "
        "padding: 6px 16px; font-weight: bold; }"

        "QPushButton:hover { background-color: #1565C0; }"

    );
    connect(m_scheduleSendButton, &QPushButton::clicked, this, &ComposeView::onScheduleClicked);
    scheduleLayout->addWidget(m_scheduleSendButton);
    m_schedulePanel->setVisible(false);
    mainLayout->addWidget(m_schedulePanel);

    // Action buttons row
    QHBoxLayout *actionsLayout = new QHBoxLayout();
    actionsLayout->addStretch();

    m_discardButton = new QPushButton(tr("Discard"), this);
    m_discardButton->setStyleSheet(
        "QPushButton { background: transparent; border: 1px solid #d1d1d6; border-radius: 4px; "
        "padding: 8px 20px; color: #555; }"

        "QPushButton:hover { background: #f5f5f5; }"

    );
    actionsLayout->addWidget(m_discardButton);
    connect(m_discardButton, &QPushButton::clicked, this, &ComposeView::discardRequested);

    // Split button for Send / Schedule
    m_sendMenu = new QMenu(this);
    m_sendNowAction = m_sendMenu->addAction(tr("Send Now"));
    connect(m_sendNowAction, &QAction::triggered, this, &ComposeView::onSendClicked);
    m_scheduleAction = m_sendMenu->addAction(tr("Schedule for later..."));
    connect(m_scheduleAction, &QAction::triggered, [this]() {
        m_schedulePanel->setVisible(true);
    });

    m_sendSplit = new QToolButton(this);
    m_sendSplit->setText(tr("Send"));
    m_sendSplit->setToolButtonStyle(Qt::ToolButtonTextOnly);
    m_sendSplit->setPopupMode(QToolButton::MenuButtonPopup);
    m_sendSplit->setMenu(m_sendMenu);
    m_sendSplit->setStyleSheet(
        "QToolButton { background-color: #1976D2; color: white; border: none; border-radius: 4px; "
        "padding: 8px 24px; font-weight: bold; }"

        "QToolButton:hover { background-color: #1565C0; }"

        "QToolButton::menu-button { border-left: 1px solid rgba(255,255,255,0.3); padding-left: 8px; padding-right: 8px; }"

        "QToolButton::menu-button:hover { background-color: #1565C0; border-top-right-radius: 4px; border-bottom-right-radius: 4px; }"

    );
    connect(m_sendSplit, &QToolButton::clicked, this, &ComposeView::onSendClicked);
    actionsLayout->addWidget(m_sendSplit);

    mainLayout->addLayout(actionsLayout);

    // Connect rich text editor signature signals
    connect(m_bodyEditor, &RichTextEditor::signatureClicked, this, &ComposeView::onSignatureClicked);
    connect(m_bodyEditor, &RichTextEditor::signatureEditRequested, this, &ComposeView::onSignatureEditRequested);
}
void ComposeView::onCcToggled(bool checked)
{
    m_ccRow->setVisible(checked);
    if (checked) {
        m_ccField->setFocus();
    }
}
void ComposeView::onBccToggled(bool checked)
{
    m_bccRow->setVisible(checked);
    if (checked) {
        m_bccField->setFocus();
    }
}
void ComposeView::onSendClicked()
{
    QString to = m_toField->text();
    QString cc = m_ccButton->isChecked() ? m_ccField->text() : QString();
    QString bcc = m_bccButton->isChecked() ? m_bccField->text() : QString();
    QString subject = m_subjectField->text();
    QString body = m_bodyEditor->toHtml();
    QDateTime scheduleTime = m_schedulePanel->isVisible() ? m_schedulePicker->dateTime() : QDateTime();
    QString fromAddress;
    if (m_currentAccountId > 0 && m_accountCombo->currentIndex() > 0) {
        fromAddress = m_accountCombo->currentData().toString();
    }
    emit sendRequested(to, cc, bcc, subject, body, scheduleTime, fromAddress);
}
void ComposeView::onScheduleClicked() {
    emit sendRequested(
        m_toField->text(),
        m_ccVisible ? m_ccField->text() : QString(),
        m_bccVisible ? m_bccField->text() : QString(),
        m_subjectField->text(),
        m_bodyEditor->toHtml(),
        m_schedulePicker->dateTime(),
        m_currentAccountId > 0 ? m_accountCombo->itemData(m_accountCombo->currentIndex()).toString() : QString()
    );
}

void ComposeView::onDetachClicked() {
    emit detachRequested(this);
}

void ComposeView::setTo(const QString &to)
{
    if (to.isEmpty()) {
        m_toField->clear();
        return;
    }
    // Split by semicolon or comma
    QStringList parts = to.split(QRegularExpression("[,;]"), Qt::SkipEmptyParts);
    QStringList cleaned;
    for (const QString &part : parts) {
        QString trimmed = part.trimmed();
        if (!trimmed.isEmpty())
            cleaned << trimmed;
    }
    m_toField->setText(cleaned.join("; "));
}

void ComposeView::setSubject(const QString &subject)
{
    m_subjectField->setText(subject);
}

void ComposeView::setBody(const QString &body)
{
    m_bodyEditor->setHtml(body);
}

void ComposeView::initializeComposition()
{
    m_subjectField->clear();
    m_bodyEditor->clear();
    m_toField->clear();
    m_ccField->clear();
    m_bccField->clear();
    m_ccRow->setVisible(false);
    m_bccRow->setVisible(false);
    m_schedulePanel->setVisible(false);
    m_subjectField->setFocus();
}
void ComposeView::startNewEmail(const QString &initialRecipient)
{
    initializeComposition();
    if (!initialRecipient.isEmpty()) {
        m_toField->setText(initialRecipient);
    }
    m_toField->setFocus();
}

void ComposeView::setSubject(const QString &subject)
{
    m_subjectField->setText(subject);
}

void ComposeView::setBody(const QString &body)
{
    m_bodyEditor->setHtml(body);
}

void ComposeView::initializeComposition()
{
    m_subjectField->clear();
    m_bodyEditor->clear();
    m_toField->clear();
    m_ccField->clear();
    m_bccField->clear();
    m_ccRow->setVisible(false);
    m_bccRow->setVisible(false);
    m_schedulePanel->setVisible(false);
    m_subjectField->setFocus();
}
#include "composeview.moc"