import { data, Link, useLoaderData } from "react-router";
import type { LoaderFunctionArgs } from "react-router";
import { getArtistById, getArtistRevisions } from "~/lib/db.server";

export async function loader({ params }: LoaderFunctionArgs) {
  const artist = getArtistById(params.uuid!);
  if (!artist) throw data("Not found", { status: 404 });
  const revisions = getArtistRevisions(artist.id);
  return { artist, revisions };
}

export default function ArtistHistory() {
  const { artist, revisions } = useLoaderData<typeof loader>();
  return (
    <main className="max-w-3xl mx-auto px-4 py-8">
      <div className="flex items-center gap-3 mb-6">
        <Link
          to={`/artists/of/${artist.id}`}
          className="text-gray-400 hover:text-gray-200 transition-colors"
        >
          ←
        </Link>
        <h1 className="text-xl font-semibold">{artist.name} — 編集履歴</h1>
      </div>

      {revisions.length === 0 ? (
        <p className="text-gray-400">履歴がありません。</p>
      ) : (
        <ol className="space-y-4">
          {revisions.map((rev, i) => {
            let snap: { name?: string; links?: unknown[] } = {};
            try { snap = JSON.parse(rev.snapshot); } catch { /* ignore */ }
            return (
              <li key={rev.id} className="bg-gray-900 rounded-lg p-4">
                <div className="flex items-start justify-between gap-4">
                  <div className="flex-1 min-w-0">
                    <p className="font-medium text-gray-100 truncate">{rev.message}</p>
                    <p className="text-xs text-gray-500 mt-1">
                      {rev.created_at} · {rev.ip_address}
                    </p>
                  </div>
                  {i === 0 && (
                    <span className="text-xs text-blue-400 shrink-0">最新</span>
                  )}
                </div>
                <div className="mt-3 text-xs text-gray-400 space-y-0.5">
                  <p>名前: {snap.name ?? "—"}</p>
                  <p>リンク: {snap.links?.length ?? 0}件</p>
                </div>
              </li>
            );
          })}
        </ol>
      )}
    </main>
  );
}
