<script lang="ts">
    import Pager from '$lib/components/Pager.svelte';
    import type { DeadJob, RequeueScope } from '$lib/api/admin';
    import SearchBar from './SearchBar.svelte';

    let {
        jobs,
        total,
        page,
        totalPages,
        search = $bindable(''),
        busy,
        onSearch,
        onPageChange,
        onRequeue,
        onRequeueAll
    }: {
        jobs: DeadJob[];
        total: number;
        page: number;
        totalPages: number;
        search?: string;
        busy: boolean;
        onSearch: () => void;
        onPageChange: (p: number) => void;
        onRequeue: (scope: RequeueScope) => void;
        onRequeueAll: () => void;
    } = $props();
</script>

<section class="deadjobs">
    <div class="deadhead">
        <h2>Dead jobs ({total})</h2>
        <div class="deadtools">
            <SearchBar bind:value={search} {onSearch} />
            <button onclick={onRequeueAll} disabled={busy || total === 0}
                >Requeue all ({total})</button
            >
        </div>
    </div>

    {#if jobs.length === 0}
        <p class="muted">No dead jobs 🎉</p>
    {:else}
        <table class="dead">
            <thead>
                <tr>
                    <th>Manga / Chapter</th>
                    <th>Att.</th>
                    <th>Failed</th>
                    <th>Last error</th>
                    <th class="actions">Action</th>
                </tr>
            </thead>
            <tbody>
                {#each jobs as j (j.id)}
                    <tr>
                        <td>
                            {#if j.manga_title}
                                {j.manga_title}
                            {:else if j.payload_title}
                                {#if j.source_url}
                                    <a href={j.source_url} target="_blank" rel="noreferrer noopener"
                                        >{j.payload_title}</a
                                    >
                                {:else}
                                    {j.payload_title}
                                {/if}
                            {:else}
                                {j.source_key ?? '(unknown)'}
                            {/if}
                            {#if j.chapter_number != null}· ch.{j.chapter_number}{/if}
                        </td>
                        <td>{j.attempts}/{j.max_attempts}</td>
                        <td>{new Date(j.updated_at).toLocaleDateString()}</td>
                        <td class="err" title={j.last_error ?? ''}>{j.last_error ?? '—'}</td>
                        <td class="actions">
                            <button
                                onclick={() => onRequeue({ scope: 'job', job_id: j.id })}
                                disabled={busy}>Requeue</button
                            >
                            {#if j.manga_id}
                                <button
                                    class="secondary"
                                    onclick={() => onRequeue({ scope: 'manga', manga_id: j.manga_id! })}
                                    disabled={busy}>Manga</button
                                >
                            {/if}
                        </td>
                    </tr>
                {/each}
            </tbody>
        </table>
        <Pager {page} {totalPages} onChange={onPageChange} />
    {/if}
</section>

<style>
    h2 {
        margin: 0 0 var(--space-3) 0;
        font-size: var(--font-sm);
        color: var(--text-muted);
        text-transform: uppercase;
        letter-spacing: 0.04em;
    }
    table {
        width: 100%;
        border-collapse: collapse;
    }
    th,
    td {
        padding: var(--space-2);
        text-align: left;
        border-bottom: 1px solid var(--border);
        font-size: var(--font-sm);
    }
    .actions {
        text-align: right;
    }
    .err {
        max-width: 22rem;
        overflow: hidden;
        text-overflow: ellipsis;
        white-space: nowrap;
        color: var(--text-muted);
    }
    .deadhead {
        display: flex;
        justify-content: space-between;
        align-items: center;
        gap: var(--space-3);
        flex-wrap: wrap;
    }
    .deadtools {
        display: flex;
        gap: var(--space-2);
    }
    button.secondary {
        background: var(--surface-elevated);
    }
    .muted {
        color: var(--text-muted);
    }
</style>
