"use client";

import useSWR from "swr";
import type { Desa } from "@/lib/types";
import { fetcher } from "@/lib/api";
import { formatDate } from "@/lib/utils";

function strip(value: string | null | undefined, prefix: string) {
  if (!value) return "-";
  return value.replace(new RegExp(`^${prefix}\\s+`, "i"), "");
}

export function KopLaporan({
  title,
  subtitle,
}: {
  title: string;
  subtitle?: string;
}) {
  const { data: desa } = useSWR<Desa>("/desa", fetcher);

  return (
    <div className="hidden print:block">
      <div className="border-b-2 border-black pb-2 text-center">
        <p className="text-sm font-semibold uppercase">
          Pemerintah Kabupaten {strip(desa?.kabupaten, "Kabupaten")} · Kecamatan{" "}
          {strip(desa?.kecamatan, "Kecamatan")}
        </p>
        <p className="text-xl font-bold uppercase">Desa {strip(desa?.nama, "Desa")}</p>
        <p className="mt-1 text-sm font-semibold">{title}</p>
        {subtitle && <p className="text-xs">{subtitle}</p>}
        <p className="text-xs">Dicetak {formatDate(new Date())}</p>
      </div>
    </div>
  );
}
