<section class="grid two">
    <div class="card">
        <div class="toolbar"><h2><?= e(trim($contact['first_name'] . ' ' . $contact['last_name'])) ?></h2><span class="badge"><?= e(ucfirst($contact['lifecycle_stage'] ?? 'lead')) ?></span></div>
        <table>
            <tbody>
                <tr><th>Phone</th><td><?= e($contact['phone']) ?></td></tr>
                <tr><th>Email</th><td><?= e($contact['email']) ?></td></tr>
                <tr><th>Company</th><td><?= e($contact['company']) ?></td></tr>
                <tr><th>Account</th><td><?= e($contact['account_name'] ?? '—') ?></td></tr>
                <tr><th>Pipeline</th><td><?= e($contact['pipeline_name'] ?? '—') ?></td></tr>
                <tr><th>Owner</th><td><?= e($contact['owner_name'] ?? '—') ?></td></tr>
                <tr><th>Tags</th><td><?= e($contact['tags']) ?></td></tr>
                <tr><th>Timezone</th><td><?= e($contact['timezone']) ?></td></tr>
                <tr><th>Score</th><td><?= (int)($contact['score'] ?? 0) ?></td></tr>
                <tr><th>SMS</th><td><?= !empty($suppressed['sms']) ? 'Suppressed' : ((int)$contact['unsubscribed_sms'] ? 'Unsubscribed' : 'Active') ?></td></tr>
                <tr><th>Email</th><td><?= !empty($suppressed['email']) ? 'Suppressed' : ((int)$contact['unsubscribed_email'] ? 'Unsubscribed' : 'Active') ?></td></tr>
            </tbody>
        </table>
    </div>
    <div class="card">
        <h2>Lifecycle Profile</h2>
        <form method="post" action="index.php?route=contacts/updateProfile">
            <?= csrf_input() ?>
            <input type="hidden" name="contact_id" value="<?= (int)$contact['id'] ?>">
            <div class="form-grid">
                <select name="lifecycle_stage">
                    <?php foreach (['lead','qualified','proposal','customer'] as $stage): ?>
                        <option value="<?= $stage ?>" <?= ($contact['lifecycle_stage'] ?? 'lead') === $stage ? 'selected' : '' ?>><?= ucfirst($stage) ?></option>
                    <?php endforeach; ?>
                </select>
                <input type="number" name="score" value="<?= (int)($contact['score'] ?? 0) ?>">
                <select name="owner_user_id">
                    <option value="">No owner</option>
                    <?php foreach ($users as $user): ?>
                        <option value="<?= (int)$user['id'] ?>" <?= (int)($contact['owner_user_id'] ?? 0) === (int)$user['id'] ? 'selected' : '' ?>><?= e($user['name']) ?></option>
                    <?php endforeach; ?>
                </select>
                <select name="account_id">
                    <option value="">No account</option>
                    <?php foreach ($accounts as $account): ?><option value="<?= (int)$account['id'] ?>" <?= (int)($contact['account_id'] ?? 0) === (int)$account['id'] ? 'selected' : '' ?>><?= e($account['name']) ?></option><?php endforeach; ?>
                </select>
                <select name="pipeline_id">
                    <option value="">No pipeline</option>
                    <?php foreach ($pipelines as $pipeline): ?><option value="<?= (int)$pipeline['id'] ?>" <?= (int)($contact['pipeline_id'] ?? 0) === (int)$pipeline['id'] ? 'selected' : '' ?>><?= e($pipeline['name']) ?></option><?php endforeach; ?>
                </select>
            </div>
            <button class="btn primary">Save Profile</button>
        </form>
        <hr>
        <div class="grid two">
            <div>
                <h3>Engagement</h3>
                <div class="small muted">Messages: <?= (int)($summary['total_messages'] ?? 0) ?></div>
                <div class="small muted">Inbound: <?= (int)($summary['inbound_total'] ?? 0) ?></div>
                <div class="small muted">Delivered: <?= (int)($summary['delivered_total'] ?? 0) ?></div>
            </div>
            <div>
                <h3>Events</h3>
                <div class="small muted">Opens: <?= (int)($summary['open_total'] ?? 0) ?></div>
                <div class="small muted">Clicks: <?= (int)($summary['click_total'] ?? 0) ?></div>
            </div>
        </div>
    </div>
</section>

<section class="grid two">
    <div class="card">
        <h2>Tasks</h2>
        <form method="post" action="index.php?route=contacts/addTask">
            <?= csrf_input() ?>
            <input type="hidden" name="contact_id" value="<?= (int)$contact['id'] ?>">
            <div class="form-grid">
                <input name="title" placeholder="Task title" required>
                <input type="datetime-local" name="due_at">
                <select name="assigned_user_id"><option value="">Assign later</option><?php foreach ($users as $user): ?><option value="<?= (int)$user['id'] ?>"><?= e($user['name']) ?></option><?php endforeach; ?></select>
                <select name="account_id">
                    <option value="">No account</option>
                    <?php foreach ($accounts as $account): ?><option value="<?= (int)$account['id'] ?>" <?= (int)($contact['account_id'] ?? 0) === (int)$account['id'] ? 'selected' : '' ?>><?= e($account['name']) ?></option><?php endforeach; ?>
                </select>
                <select name="pipeline_id">
                    <option value="">No pipeline</option>
                    <?php foreach ($pipelines as $pipeline): ?><option value="<?= (int)$pipeline['id'] ?>" <?= (int)($contact['pipeline_id'] ?? 0) === (int)$pipeline['id'] ? 'selected' : '' ?>><?= e($pipeline['name']) ?></option><?php endforeach; ?>
                </select>
            </div>
            <button class="btn secondary">Add Task</button>
        </form>
        <table>
            <thead><tr><th>Task</th><th>Due</th><th>Assigned</th><th>Status</th><th></th></tr></thead>
            <tbody>
            <?php foreach ($tasks as $task): ?>
                <tr>
                    <td><?= e($task['title']) ?></td>
                    <td><?= e($task['due_at']) ?></td>
                    <td><?= e($task['assigned_user_name'] ?: '—') ?></td>
                    <td><?= e($task['status']) ?></td>
                    <td><?php if (($task['status'] ?? '') !== 'done'): ?><a class="btn secondary small" href="index.php?route=contacts/completeTask&contact_id=<?= (int)$contact['id'] ?>&task_id=<?= (int)$task['id'] ?>">Done</a><?php endif; ?></td>
                </tr>
            <?php endforeach; ?>
            </tbody>
        </table>
    </div>
    <div class="card">
        <h2>Internal Notes</h2>
        <form method="post" action="index.php?route=contacts/addNote">
            <?= csrf_input() ?>
            <input type="hidden" name="contact_id" value="<?= (int)$contact['id'] ?>">
            <textarea name="note_body" rows="5" placeholder="Add an internal note" required></textarea>
            <button class="btn secondary">Add Note</button>
        </form>
        <?php foreach ($notes as $note): ?>
            <article class="bubble inbound" style="margin-top:12px;">
                <div class="bubble-meta small muted"><?= e($note['created_by_name'] ?: 'System') ?> · <?= e($note['created_at']) ?></div>
                <div><?= nl2br(e($note['note_body'])) ?></div>
            </article>
        <?php endforeach; ?>
    </div>
</section>

<section class="grid two">
    <div class="card">
        <h2>Custom Fields</h2>
        <form method="post" action="index.php?route=contacts/saveCustomValues">
            <?= csrf_input() ?>
            <input type="hidden" name="contact_id" value="<?= (int)$contact['id'] ?>">
            <?php foreach ($customFields as $field): ?>
                <label><?= e($field['name']) ?>
                    <input name="custom[<?= e($field['field_key']) ?>]" value="<?= e($customValues[$field['field_key']] ?? '') ?>">
                </label>
            <?php endforeach; ?>
            <button class="btn secondary">Save Custom Fields</button>
        </form>
    </div>
    <div class="card">
        <h2>Consent History</h2>
        <form method="post" action="index.php?route=contacts/addConsent">
            <?= csrf_input() ?>
            <input type="hidden" name="contact_id" value="<?= (int)$contact['id'] ?>">
            <div class="form-grid">
                <select name="channel"><option value="email">Email</option><option value="sms">SMS</option></select>
                <select name="status"><option value="opt_in">Opt In</option><option value="opt_out">Opt Out</option></select>
                <input name="source" placeholder="Source e.g. form, call, manual">
                <input name="proof_note" placeholder="Proof note">
            </div>
            <button class="btn secondary">Add Consent Record</button>
        </form>
        <table>
            <thead><tr><th>When</th><th>Channel</th><th>Status</th><th>Source</th></tr></thead>
            <tbody>
            <?php foreach ($consentLogs as $log): ?>
                <tr><td><?= e($log['created_at']) ?></td><td><?= e($log['channel']) ?></td><td><?= e($log['status']) ?></td><td><?= e($log['source']) ?></td></tr>
            <?php endforeach; ?>
            </tbody>
        </table>
    </div>
</section>

<section class="grid two">
    <div class="card">
        <h2>Reminders</h2>
        <form method="post" action="index.php?route=contacts/addReminder">
            <?= csrf_input() ?>
            <input type="hidden" name="contact_id" value="<?= (int)$contact['id'] ?>">
            <div class="form-grid">
                <input name="title" placeholder="Reminder title" required>
                <input type="datetime-local" name="remind_at">
                <select name="assigned_user_id"><option value="">Assign later</option><?php foreach ($users as $user): ?><option value="<?= (int)$user['id'] ?>"><?= e($user['name']) ?></option><?php endforeach; ?></select>
            </div>
            <button class="btn secondary">Add Reminder</button>
        </form>
        <table><thead><tr><th>Reminder</th><th>When</th><th>Assigned</th><th></th></tr></thead><tbody><?php foreach ($reminders as $reminder): ?><tr><td><?= e($reminder['title']) ?></td><td><?= e($reminder['remind_at']) ?></td><td><?= e($reminder['assigned_user_name'] ?: '—') ?></td><td><?php if (($reminder['status'] ?? '') !== 'done'): ?><a class="btn secondary small" href="index.php?route=contacts/completeReminder&contact_id=<?= (int)$contact['id'] ?>&reminder_id=<?= (int)$reminder['id'] ?>">Done</a><?php endif; ?></td></tr><?php endforeach; ?></tbody></table>
    </div>
    <div class="card">
        <h2>Consent Vault Summary</h2>
        <p class="small muted">This contact stores channel-level consent events with source and proof notes. Use this section together with DNC and suppression controls for compliance workflows.</p>
        <div class="small muted">Total consent records: <?= count($consentLogs) ?></div>
        <div class="small muted">SMS status: <?= (int)$contact['unsubscribed_sms'] ? 'Do not contact' : 'Reachable' ?></div>
        <div class="small muted">Email status: <?= (int)$contact['unsubscribed_email'] ? 'Do not contact' : 'Reachable' ?></div>
    </div>
</section>

<section class="card">
    <h2>Activity Timeline</h2>
    <?php foreach ($timeline as $item): ?>
        <article class="bubble <?= e(($item['direction'] ?? 'inbound')) ?>">
            <div class="bubble-meta small muted"><?= e($item['item_type']) ?> · <?= e($item['created_at']) ?> · <?= e($item['status']) ?></div>
            <?php if (!empty($item['subject'])): ?><strong><?= e($item['subject']) ?></strong><?php endif; ?>
            <div><?= nl2br(e(strip_tags((string)($item['body'] ?? '')))) ?></div>
        </article>
    <?php endforeach; ?>
</section>
