<?php
/** @var array $incident */
/** @var array $tenant */
/** @var array $events */
$companyName = \Core\Settings::get('company_name') ?: APP_NAME;
?>
<style>
    body { font-family: sans-serif; color: #1e2d3d; font-size: 10pt; }
    .brand { color: #0b2545; font-weight: 800; font-size: 16pt; }
    .sub { color: #6b7a8d; font-size: 9pt; text-transform: uppercase; letter-spacing: 1.5px; }
    h1 { font-size: 18pt; margin: 8mm 0 2mm; color: #0b2545; }
    h2 { font-size: 12pt; margin: 6mm 0 2mm; color: #1a4d8c; border-bottom: 1px solid #d1d5db; padding-bottom: 1mm; }
    table.def { width: 100%; border-collapse: collapse; margin: 2mm 0 4mm; }
    table.def td { padding: 1mm 2mm; vertical-align: top; }
    table.def td.k { color: #6b7a8d; width: 25%; text-transform: uppercase; font-size: 8pt; letter-spacing: .5px; }
    table.def td.v { color: #1e2d3d; font-weight: 600; }
    .pill { display:inline-block; padding: 1mm 2mm; border-radius: 4px; font-size: 8pt; text-transform: uppercase; font-weight: 700; }
    .pill.critical { background: #fee2e2; color: #991b1b; }
    .pill.high     { background: #ffedd5; color: #9a3412; }
    .pill.medium   { background: #fef3c7; color: #92400e; }
    .pill.low      { background: #e0f2fe; color: #075985; }
    .feed { border-left: 2px solid #d1d5db; padding-left: 5mm; margin-top: 3mm; }
    .feed .ev { margin-bottom: 3mm; font-size: 9pt; }
    .foot { margin-top: 12mm; color: #94a3b8; font-size: 7pt; text-align: center; }
</style>

<div>
    <div class="brand"><?= e($companyName) ?></div>
    <div class="sub"><?= e($tenant['name'] ?? '') ?> · Incident Report</div>
</div>

<h1><?= e($incident['code'] ?? '#' . $incident['incident_id']) ?> · <?= e($incident['title']) ?></h1>

<h2>Details</h2>
<table class="def">
    <tr><td class="k">Category</td>    <td class="v"><?= e(strtoupper($incident['category'])) ?></td>
        <td class="k">Type</td>         <td class="v"><?= e($incident['type_name'] ?? '—') ?></td></tr>
    <tr><td class="k">Severity</td>    <td class="v"><span class="pill <?= e($incident['severity']) ?>"><?= e($incident['severity']) ?></span></td>
        <td class="k">Status</td>       <td class="v"><?= e(strtoupper($incident['status'])) ?></td></tr>
    <tr><td class="k">Asset</td>       <td class="v"><?= e($incident['asset_name'] ?? '—') ?></td>
        <td class="k">Site</td>         <td class="v"><?= e($incident['site_name'] ?? '—') ?></td></tr>
    <tr><td class="k">Reported by</td> <td class="v"><?= e($incident['reported_username'] ?? '—') ?></td>
        <td class="k">Assigned to</td>  <td class="v"><?= e($incident['assigned_username'] ?? 'unassigned') ?></td></tr>
    <tr><td class="k">Start</td>       <td class="v"><?= e($incident['start_at'] ?? '—') ?></td>
        <td class="k">End</td>          <td class="v"><?= e($incident['end_at'] ?? '—') ?></td></tr>
    <tr><td class="k">Closed at</td>   <td class="v" colspan="3"><?= e($incident['closed_at'] ?? '—') ?></td></tr>
</table>

<h2>Description</h2>
<p><?= nl2br(e($incident['description'] ?? '')) ?: '<span style="color:#6b7a8d;">No description.</span>' ?></p>

<h2>Root Cause</h2>
<p><?= nl2br(e($incident['root_cause'] ?? '')) ?: '<span style="color:#6b7a8d;">Not documented.</span>' ?></p>

<h2>Resolution</h2>
<p><?= nl2br(e($incident['resolution'] ?? '')) ?: '<span style="color:#6b7a8d;">No resolution yet.</span>' ?></p>

<?php if (!empty($events)): ?>
    <h2>Activity Feed</h2>
    <div class="feed">
        <?php foreach ($events as $ev): ?>
            <div class="ev">
                <strong><?= e($ev['username'] ?? 'system') ?></strong>
                <span style="color:#6b7a8d;">· <?= e($ev['created_at']) ?></span><br>
                <?php if ($ev['event_type'] === 'status_change'): ?>
                    changed status: <em><?= e($ev['from_value'] ?? '—') ?></em> → <em><?= e($ev['to_value'] ?? '—') ?></em>
                <?php endif; ?>
                <?php if (!empty($ev['message'])): ?><?= e($ev['message']) ?><?php endif; ?>
            </div>
        <?php endforeach; ?>
    </div>
<?php endif; ?>

<div class="foot">
    Generated <?= date('Y-m-d H:i') ?> · <?= e($companyName) ?>
</div>
