<?php
declare(strict_types=1);

/**
 * @var \App\View\AppView $this
 * @var \App\Model\Entity\ContactMessage $message
 * @var array<string, string> $statusOptions
 */

$interestLabel = $message->interest_area
    ? ucwords(str_replace('_', ' ', (string)$message->interest_area))
    : '—';
$experienceLabel = $message->experience_level
    ? ucfirst((string)$message->experience_level)
    : '—';

$submittedAt = $message->created?->format('Y-m-d H:i:s') ?? '—';
$submittedHuman = $message->created?->format('j M · H:i') ?? '—';
$updatedAt = $message->modified?->format('Y-m-d H:i:s') ?? '—';

$statusMap = [
    'new'         => ['label' => 'New',         'desc' => 'Unread or not yet triaged'],
    'in_progress' => ['label' => 'In progress', 'desc' => 'Being handled or awaiting reply'],
    'resolved'    => ['label' => 'Resolved',    'desc' => 'Replied and closed'],
    'spam'        => ['label' => 'Spam',        'desc' => 'Not a legitimate enquiry'],
];

$replySubject = 'Re: ' . ($message->subject ?: 'Your enquiry to CandleCraft Academy');
?>
<?php $this->assign('title', 'Review enquiry · CandleCraft Admin'); ?>

<section class="cc-admin-page">
    <a class="cc-msg-back" href="<?= $this->Url->build(['_name' => 'admin_messages']) ?>">
        <svg width="14" height="14" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round"><line x1="19" y1="12" x2="5" y2="12"/><polyline points="12 19 5 12 12 5"/></svg>
        Back to inbox
    </a>

    <div class="cc-msg-grid">
        <div class="cc-reveal">
            <div class="cc-msg-meta">
                <span class="cc-badge cc-badge--<?= h((string)$message->status) ?>"><?= h($statusMap[$message->status]['label'] ?? ucfirst((string)$message->status)) ?></span>
                <span class="cc-chip"><?= h($interestLabel) ?></span>
                <?php if ($message->experience_level): ?>
                    <span class="cc-chip cc-chip--moss"><?= h($experienceLabel) ?></span>
                <?php endif; ?>
                <span class="cc-muted" style="font-size: 13px;">Submitted · <?= h($submittedHuman) ?> · #<?= sprintf('%04d', (int)$message->id) ?></span>
            </div>
            <div class="cc-msg-title"><?= h((string)($message->subject ?: 'General question')) ?></div>

            <div class="cc-card">
                <div class="cc-eyebrow" style="margin-bottom: 18px;">Sender details</div>
                <div class="cc-msg-kv">
                    <div>
                        <div class="cc-msg-kv__k">Name</div>
                        <div class="cc-msg-kv__v"><?= h($message->full_name) ?></div>
                    </div>
                    <div>
                        <div class="cc-msg-kv__k">Email</div>
                        <div class="cc-msg-kv__v"><a href="mailto:<?= h($message->email) ?>"><?= h($message->email) ?></a></div>
                    </div>
                    <div>
                        <div class="cc-msg-kv__k">Phone</div>
                        <div class="cc-msg-kv__v"><?= h((string)($message->phone ?: '—')) ?></div>
                    </div>
                    <div>
                        <div class="cc-msg-kv__k">Interest area</div>
                        <div class="cc-msg-kv__v"><?= h($interestLabel) ?></div>
                    </div>
                    <div>
                        <div class="cc-msg-kv__k">Experience</div>
                        <div class="cc-msg-kv__v"><?= h($experienceLabel) ?></div>
                    </div>
                    <div>
                        <div class="cc-msg-kv__k">Goals</div>
                        <div class="cc-msg-kv__v"><?= h((string)($message->goals ?: '—')) ?></div>
                    </div>
                    <div>
                        <div class="cc-msg-kv__k">Consent to contact</div>
                        <div class="cc-msg-kv__v cc-msg-kv__v--moss">
                            <?= $message->consent_to_contact ? 'Yes — given at submission' : 'No' ?>
                        </div>
                    </div>
                    <div>
                        <div class="cc-msg-kv__k">Marketing opt-ins</div>
                        <div class="cc-msg-kv__v">
                            <?php
                            $opts = [];
                            if (!empty($message->marketing_opt_in_email)) $opts[] = 'Email';
                            if (!empty($message->marketing_opt_in_sms)) $opts[] = 'SMS';
                            echo h($opts ? implode(' + ', $opts) : 'None');
                            ?>
                        </div>
                    </div>
                </div>
            </div>

            <div class="cc-msg-body">
                <div class="cc-eyebrow" style="margin-bottom: 14px;">Message</div>
                <?php foreach (preg_split('/\r?\n\r?\n/', trim((string)$message->message_body)) ?: [] as $para): ?>
                    <p><?= nl2br(h(trim($para))) ?></p>
                <?php endforeach; ?>
            </div>

            <!-- Reply via email form -->
            <div class="cc-msg-reply" style="margin-top: 24px;">
                <h3>Reply by email</h3>
                <?= $this->Form->create(null, ['url' => ['_name' => 'admin_message_reply', 'id' => $message->id]]) ?>
                    <div style="margin-bottom: 12px;">
                        <label class="cc-label" for="reply_subject">Subject</label>
                        <input class="cc-input" name="subject" id="reply_subject" value="<?= h($replySubject) ?>" required>
                    </div>
                    <div>
                        <label class="cc-label" for="reply_body">Your reply</label>
                        <textarea class="cc-textarea" name="body" id="reply_body" rows="6" required placeholder="Hi <?= h($message->full_name) ?>, thanks for getting in touch…"></textarea>
                        <div class="cc-msg-reply__helper">Sent from hello@candlecraft.local. Marks the enquiry as resolved.</div>
                    </div>
                    <div style="display: flex; gap: 10px; margin-top: 14px;">
                        <button class="cc-btn cc-btn--primary" type="submit">Send reply</button>
                        <button class="cc-btn cc-btn--ghost" type="button" onclick="document.getElementById('reply_body').value='';">Clear</button>
                    </div>
                <?= $this->Form->end() ?>
            </div>
        </div>

        <aside class="cc-reveal" style="--cc-reveal-delay: 100ms;">
            <?= $this->Form->create($message) ?>
                <div class="cc-card">
                    <div class="cc-eyebrow" style="margin-bottom: 14px;">Status</div>
                    <div class="cc-status-select">
                        <?php foreach ($statusMap as $key => $meta):
                            $active = $message->status === $key;
                        ?>
                            <label class="cc-status-select__opt<?= $active ? ' is-active' : '' ?>">
                                <input type="radio" name="status" value="<?= h($key) ?>" <?= $active ? 'checked' : '' ?> style="display:none;">
                                <span class="cc-badge cc-badge--<?= h($key) ?>"><?= h($meta['label']) ?></span>
                                <span class="cc-status-select__desc"><?= h($meta['desc']) ?></span>
                            </label>
                        <?php endforeach; ?>
                    </div>
                </div>

                <div class="cc-card" style="margin-top: 16px;">
                    <div class="cc-eyebrow" style="margin-bottom: 14px;">Internal notes</div>
                    <?= $this->Form->control('admin_notes', [
                        'label' => false,
                        'class' => 'cc-textarea',
                        'type' => 'textarea',
                        'rows' => 5,
                        'placeholder' => 'Note what has happened so far, what still needs attention.',
                    ]) ?>
                    <div style="font-size: 12px; color: var(--cc-ink-3); margin-top: 8px;">
                        Last edited · <?= h($updatedAt) ?>. Not visible to the enquirer.
                    </div>
                </div>

                <div class="cc-card" style="margin-top: 16px;">
                    <div class="cc-eyebrow" style="margin-bottom: 14px;">Metadata</div>
                    <div style="font-family: 'Plus Jakarta Sans', monospace; font-size: 12.5px; color: var(--cc-ink-3); display: flex; flex-direction: column; gap: 4px;">
                        <div style="display:flex;justify-content:space-between;"><span>id</span><span><?= sprintf('%04d', (int)$message->id) ?></span></div>
                        <div style="display:flex;justify-content:space-between;"><span>submitted</span><span><?= h($submittedAt) ?></span></div>
                        <div style="display:flex;justify-content:space-between;"><span>updated</span><span><?= h($updatedAt) ?></span></div>
                        <div style="display:flex;justify-content:space-between;"><span>captcha</span><span style="color: var(--cc-moss);">Verified</span></div>
                    </div>
                </div>

                <div style="display: flex; gap: 10px; margin-top: 16px;">
                    <button class="cc-btn cc-btn--dark" type="submit">Save changes</button>
                    <a class="cc-btn cc-btn--ghost" href="<?= $this->Url->build(['_name' => 'admin_messages']) ?>">Cancel</a>
                </div>
            <?= $this->Form->end() ?>
        </aside>
    </div>
</section>
