import { Head, Link } from '@inertiajs/react';
import PublicLayout from '@/pages/layouts/public-layout';

type HistoryItem = {
    tanggal: string | null;
    waktu: string | null;
    waktu_akhir: string | null;
    location: string | null;
};

type Match = {
    tanggal: string | null;
    status: number;
    status_label: string;
    nama_pengirim: string | null;
    photo_url: string | null;
    history: HistoryItem[];
};

type Props = {
    token: string;
    matches: Match[];
    total: number;
    truncated: boolean;
    notFound: boolean;
    lookupReady: boolean;
};

export default function TrackingShow({ token, matches, truncated, notFound }: Props) {
    return (
        <PublicLayout>
            <Head title={`Lacak ${token}`}>
                <meta name="robots" content="noindex,nofollow" />
            </Head>

            <section className="border-b border-stb-line bg-stb-paper">
                <div className="stb-container stb-section">
                    <p className="stb-type-caption text-stb-muted">Hasil tracking</p>
                    <h1 className="stb-type-hero mt-[var(--stb-space-2)]">{token}</h1>
                </div>
            </section>

            <section className="stb-container stb-section">
                {notFound || matches.length === 0 ? (
                    <div className="max-w-xl space-y-[var(--stb-space-3)]">
                        <p className="stb-type-lead text-stb-muted">
                            Nomor tracking tidak ditemukan.
                        </p>
                        <Link
                            href="/lacak"
                            className="inline-flex font-semibold text-stb-ink underline decoration-stb-accent decoration-2 underline-offset-4"
                        >
                            Coba lagi
                        </Link>
                    </div>
                ) : (
                    <div className="space-y-[var(--stb-space-6)]">
                        {matches.length > 1 || truncated ? (
                            <p className="stb-type-small text-stb-muted">
                                {truncated
                                    ? `Menampilkan ${matches.length} kiriman terbaru. Nomor tracking ini dipakai lebih dari satu kiriman — hubungi layanan pelanggan bila data tidak sesuai.`
                                    : `Ditemukan ${matches.length} kiriman dengan nomor yang sama.`}
                            </p>
                        ) : null}

                        {matches.map((match, index) => (
                            <article
                                key={`${match.tanggal ?? 't'}-${match.status}-${index}`}
                                className="border-t border-stb-line pt-[var(--stb-space-5)] first:border-t-0 first:pt-0"
                            >
                                <div className="flex flex-wrap items-baseline justify-between gap-[var(--stb-space-2)]">
                                    <p className="stb-type-title">{match.status_label}</p>
                                    {match.tanggal ? (
                                        <p className="stb-type-small text-stb-muted">
                                            {match.tanggal}
                                        </p>
                                    ) : null}
                                </div>

                                {match.nama_pengirim ? (
                                    <p className="stb-type-body mt-[var(--stb-space-2)] text-stb-muted">
                                        Pengirim: {match.nama_pengirim}
                                    </p>
                                ) : null}

                                {match.photo_url ? (
                                    <div className="mt-[var(--stb-space-4)]">
                                        <img
                                            src={match.photo_url}
                                            alt="Bukti penerimaan"
                                            className="max-h-80 w-auto max-w-full rounded-[var(--stb-control-radius)] border border-stb-line object-contain"
                                            loading="lazy"
                                        />
                                    </div>
                                ) : null}

                                {match.history.length > 0 ? (
                                    <ol className="mt-[var(--stb-space-5)] space-y-[var(--stb-space-3)] border-l border-stb-line pl-[var(--stb-space-4)]">
                                        {match.history.map((item, historyIndex) => (
                                            <li
                                                key={`${item.tanggal}-${item.waktu}-${historyIndex}`}
                                                className="relative"
                                            >
                                                <span
                                                    className="absolute -left-[calc(var(--stb-space-4)+0.28rem)] top-1.5 size-2 rounded-full bg-stb-accent"
                                                    aria-hidden
                                                />
                                                <p className="stb-type-small font-semibold text-stb-ink">
                                                    {[
                                                        item.tanggal,
                                                        item.waktu_akhir && item.waktu
                                                            ? `${item.waktu} – ${item.waktu_akhir}`
                                                            : item.waktu,
                                                    ]
                                                        .filter(Boolean)
                                                        .join(' · ') || '—'}
                                                </p>
                                                {item.location ? (
                                                    <p className="stb-type-body mt-1 text-stb-muted">
                                                        {item.location}
                                                    </p>
                                                ) : null}
                                            </li>
                                        ))}
                                    </ol>
                                ) : (
                                    <p className="stb-type-small mt-[var(--stb-space-4)] text-stb-muted">
                                        Belum ada riwayat perjalanan.
                                    </p>
                                )}
                            </article>
                        ))}

                        <Link
                            href="/lacak"
                            className="inline-flex font-semibold text-stb-ink underline decoration-stb-accent decoration-2 underline-offset-4"
                        >
                            Lacak nomor lain
                        </Link>
                    </div>
                )}
            </section>
        </PublicLayout>
    );
}
