<?= $this->extend('layouts/enterprise') ?>

<?= $this->section('content') ?>

<?php
$formatDate = static fn (?string $d): string => $d ? date('M j, Y', strtotime($d)) : '—';
$formatDateTime = static fn (?string $d): string => $d ? date('M j, Y g:i A', strtotime($d)) : '—';
$formatSize = static fn (int $bytes): string => $bytes >= 1048576 ? round($bytes / 1048576, 1) . ' MB' : round($bytes / 1024, 1) . ' KB';
$status = $expiry->expiryStatus($document['expiry_date'], $today);
$statusMap = [
    'expired'  => ['terminated', 'Expired'],
    'critical' => ['on_leave', 'Expiring within 7 days'],
    'warning'  => ['on_leave', 'Expiring within 30 days'],
    'ok'       => ['active', 'Valid'],
    'none'     => ['inactive', 'No expiry date'],
];
[$pillClass, $pillLabel] = $statusMap[$status] ?? ['inactive', '—'];
?>

<?= view('partials/workspace_hero', [
    'workspaceCrumbPill'    => 'HRM',
    'workspaceCrumbCurrent' => esc($document['type_name']),
    'workspaceTitle'        => esc($document['type_name']),
    'workspaceDesc'         => esc($document['full_name']) . ' · ' . esc($document['employee_number']),
    'workspaceAction'       => [
        'href'  => base_url('documents/' . $document['id'] . '/edit'),
        'label' => 'Edit / New Version',
        'icon'  => 'fa-solid fa-pen',
    ],
]) ?>

<div class="row g-4 mb-4">
    <div class="col-lg-6">
        <article class="enterprise-detail-card">
            <h3 class="enterprise-detail-card__title">Document Details</h3>
            <div class="enterprise-detail-list">
                <dl class="enterprise-detail-item"><dt>Employee</dt><dd><a href="<?= base_url('employees/' . $document['employee_id']) ?>"><?= esc($document['full_name']) ?></a></dd></dl>
                <dl class="enterprise-detail-item"><dt>Type</dt><dd><?= esc($document['type_name']) ?> <code><?= esc($document['type_code']) ?></code></dd></dl>
                <dl class="enterprise-detail-item"><dt>Document Number</dt><dd><span class="enterprise-data-table__mono"><?= esc($document['document_number'] ?: '—') ?></span></dd></dl>
                <dl class="enterprise-detail-item"><dt>Issue Date</dt><dd><?= $formatDate($document['issue_date']) ?></dd></dl>
                <dl class="enterprise-detail-item"><dt>Expiry Date</dt><dd><?= $formatDate($document['expiry_date']) ?></dd></dl>
                <dl class="enterprise-detail-item"><dt>Expiry Status</dt><dd><span class="enterprise-status-pill enterprise-status-pill--<?= esc($pillClass) ?>"><?= esc($pillLabel) ?></span></dd></dl>
                <dl class="enterprise-detail-item enterprise-detail-item--full"><dt>Notes</dt><dd><?= esc($document['notes'] ?: '—') ?></dd></dl>
            </div>
            <div class="d-flex gap-2 flex-wrap mt-3">
                <a href="<?= base_url('documents/' . $document['id'] . '/download') ?>" class="btn btn-sm btn-primary">
                    <i class="fa-solid fa-download me-1"></i>Secure Download (Latest)
                </a>
                <a href="<?= base_url('documents/expiry') ?>" class="btn btn-sm btn-outline-secondary">Expiry Tracker</a>
            </div>
        </article>
    </div>

    <div class="col-lg-6">
        <div class="enterprise-data-panel h-100">
            <div class="enterprise-data-panel__head">
                <h2 class="enterprise-data-panel__title">Version History</h2>
                <p class="enterprise-data-panel__subtitle"><?= count($versions) ?> version(s) on file.</p>
            </div>
            <div class="enterprise-data-panel__body">
                <?php if ($versions === []): ?>
                    <p class="text-muted text-center py-4 mb-0">No file versions found.</p>
                <?php else: ?>
                    <ul class="list-unstyled mb-0">
                        <?php foreach ($versions as $v): ?>
                            <li class="d-flex justify-content-between align-items-start py-3 border-bottom">
                                <div>
                                    <span class="fw-semibold">Version <?= (int) $v['version_number'] ?></span>
                                    <?php if ($v['version_number'] == $document['version_count']): ?>
                                        <span class="badge bg-primary ms-1">Current</span>
                                    <?php endif; ?>
                                    <br><small class="text-muted"><?= esc($v['original_filename']) ?> · <?= $formatSize((int) $v['file_size']) ?></small>
                                    <?php if (! empty($v['change_notes'])): ?>
                                        <br><small class="text-muted"><?= esc($v['change_notes']) ?></small>
                                    <?php endif; ?>
                                </div>
                                <div class="text-end">
                                    <small class="text-muted d-block"><?= $formatDateTime($v['created_at']) ?></small>
                                    <a href="<?= base_url('documents/' . $document['id'] . '/download?version=' . $v['id']) ?>" class="btn btn-sm btn-outline-secondary mt-1">
                                        <i class="fa-solid fa-download"></i>
                                    </a>
                                </div>
                            </li>
                        <?php endforeach; ?>
                    </ul>
                <?php endif; ?>
            </div>
        </div>
    </div>
</div>

<?= $this->endSection() ?>
