<script lang="ts">
    import { type ReportMetadata } from '$lib/analysis.svelte';
    import type { ManifestEntry } from '$lib/manifest.svelte';
    import { gps_mode_label } from '$lib/utils.svelte';
    import { AnalysisManager } from '$lib/analysisManager.svelte';
    import AnalysisTable from './AnalysisTable.svelte';
    import ReAnalyzeButton from './ReAnalyzeButton.svelte';
    let {
        entry,
        manager,
        current,
    }: {
        entry: ManifestEntry;
        manager: AnalysisManager;
        current: boolean;
    } = $props();

    const date_formatter = new Intl.DateTimeFormat(undefined, {
        timeStyle: 'long',
        dateStyle: 'short',
    });
</script>

<div class="container mt-2">
    {#if entry.analysis_report === undefined}
        <p>Report unavailable, try refreshing.</p>
    {:else if typeof entry.analysis_report === 'string'}
        <p>Error getting analysis report: {entry.analysis_report}</p>
    {:else}
        {@const metadata: ReportMetadata = entry.analysis_report.metadata}
        {@const numWarnings: number = entry.get_num_warnings() || 0}
        <div class="flex flex-col gap-2">
            {#if !!numWarnings || !current}
                <div class="flex flex-row justify-between items-center">
                    {#if !!numWarnings}
                        <div
                            class="text-red-700 border-red-500 border rounded-lg text-blue-600 px-2 py-1 mr-12"
                        >
                            Your Rayhunter device raised {`${numWarnings}`} warning{`${
                                numWarnings > 1 ? 's' : ''
                            }`}!
                            <a
                                href="https://efforg.github.io/rayhunter/faq.html#red"
                                class="text-blue-600 underline">Read the FAQ</a
                            > to learn what you can do about it
                        </div>
                    {/if}
                    {#if !current}
                        <ReAnalyzeButton {entry} {manager} />
                    {/if}
                </div>
            {/if}
            {#if entry.analysis_report.rows.length > 0}
                <AnalysisTable report={entry.analysis_report} />
            {:else}
                <p>No warnings to display!</p>
            {/if}
            <div>
                <p class="text-lg underline">Metadata</p>
                {#if metadata !== undefined && metadata.rayhunter !== undefined}
                    <p><b>Rayhunter version:</b> {metadata.rayhunter.rayhunter_version}</p>
                    <p><b>Device system OS:</b> {metadata.rayhunter.system_os}</p>
                {:else}
                    <p>N/A (analysis generated by an older version of rayhunter)</p>
                {/if}
                {#if entry.upload_time}
                    <p>
                        <b>WebDAV uploaded at:</b>
                        <span class="text-green-700"
                            >{date_formatter.format(entry.upload_time)}</span
                        >
                    </p>
                {/if}
                <p>
                    <b>GPS Mode:</b>
                    {gps_mode_label(entry.gps_mode)}
                </p>
            </div>
            {#if metadata && metadata.analyzers}
                <div>
                    <p class="text-lg underline">Enabled Analyzers</p>
                    {#each metadata.analyzers as analyzer}
                        <p><b>{analyzer.name}:</b> {analyzer.description}</p>
                    {/each}
                </div>
            {/if}
        </div>
    {/if}
</div>
