@extends('install.layout')
@section('title', 'Install complete — AI Vision Attendance')
@section('tagline', 'Setup complete')
@section('footnote', 'Save the device tokens above before you navigate away — they are shown only once.')

@php
    $lines = [];
    $lines[] = 'AI Vision Attendance — Device Tokens (generated by the /install wizard)';
    $lines[] = 'Generated: '.now()->toDateTimeString();
    $lines[] = str_repeat('=', 70);
    foreach ($sites as $row) {
        $lines[] = "{$row['site']->name} — device_id={$row['device']->id} — token: {$row['token']}";
    }
    $lines[] = '';
    $lines[] = 'Admin login: '.$admin->email;
    $lines[] = 'Tasks token (for external cron URLs): '.$tasksToken;
    $tokensFile = implode("\n", $lines)."\n";
@endphp

@section('content')
<div style="text-align:center;">
    <div class="success-icon" aria-hidden="true">&#10003;</div>
    <h2 style="margin:0 0 6px;font-size:19px;">Installation complete</h2>
    <p class="muted" style="color:var(--i-text-muted);margin:0 0 22px;">AI Vision Attendance is ready to use.</p>
</div>

<div class="alert alert-error">
    <strong>Copy these device tokens now.</strong> They are shown only on this screen and are never
    stored in plaintext on the server — if you navigate away without saving them, you will need to
    add replacement devices manually later (there is no way to recover a lost token, only reissue a
    new one).
</div>

<div class="card">
    <h3>Device tokens (one per site)</h3>
    <div class="table-wrap">
        <table class="token-table">
            <thead><tr><th>Site</th><th>Device ID</th><th>Token</th></tr></thead>
            <tbody>
            @foreach($sites as $row)
                <tr>
                    <td>{{ $row['site']->name }}</td>
                    <td>{{ $row['device']->id }}</td>
                    <td class="token-cell">{{ $row['token'] }}</td>
                </tr>
            @endforeach
            </tbody>
        </table>
    </div>
    <a class="btn btn-primary" style="margin-top:14px;"
       download="device-tokens.txt"
       href="data:text/plain;charset=utf-8;base64,{{ base64_encode($tokensFile) }}">
       Download tokens (.txt)
    </a>
</div>

<div class="card">
    <h3>Admin login</h3>
    <p style="margin:0 0 4px;">Email: <strong>{{ $admin->email }}</strong></p>
    <p class="muted" style="margin:0 0 14px;color:var(--i-text-muted);">Use the password you just set in the wizard.</p>
    <a class="btn btn-primary btn-block" href="{{ route('login') }}">Go to login</a>
</div>

<div class="card">
    <h3>No-cron scheduled tasks (optional)</h3>
    <p class="muted" style="color:var(--i-text-muted);">This host may have no cron access. Reports/dashboards already
        compute on the fly, so nothing below is required to run the app — but you can wire these into a free
        external pinger (e.g. cron-job.org) for periodic housekeeping:</p>
    <div class="mono-line">{{ $appUrl }}/tasks/purge-snapshots?token={{ $tasksToken }}&amp;days=60</div>
    <div class="mono-line">{{ $appUrl }}/tasks/compute-attendance?token={{ $tasksToken }}</div>
    <p class="muted" style="color:var(--i-text-muted);">The same URLs (with the token already filled in) are also
        shown on the admin <strong>Maintenance</strong> page after you log in.</p>
</div>

<div class="card">
    <h3>Security — do this next</h3>
    <ul class="checklist">
        <li>Delete <code>public/check.php</code> from the server — it is a diagnostic tool only.</li>
        <li>Save the device tokens above somewhere safe (password manager / secrets store).</li>
        <li>Point each edge device's <code>central_url</code> at <code>{{ $appUrl }}</code> and set its
            token to the value shown for its site.</li>
    </ul>
</div>
@endsection
