import { PreviewLink } from "@/components/layout/preview-context";
import { DetailList, PageHeader, Panel, StatusBadge } from "@/components/shared/page";
import type { LocationDirectoryRecord } from "./api-contract";

export function LocationDetail({
  location,
  error,
}: {
  location: LocationDirectoryRecord | undefined;
  error: string | null;
}) {
  if (!location || error) {
    return (
      <Panel title="Location unavailable">
        <p role="status" className="p-5 text-sm text-muted-foreground">
          {error ?? "This location does not exist or is outside your assigned scope."}
        </p>
      </Panel>
    );
  }

  const coordinates =
    location.latitude !== null && location.longitude !== null
      ? `${location.latitude}, ${location.longitude}`
      : "—";

  return (
    <div className="space-y-5">
      <PageHeader title={location.name} parent={{ label: "Locations", href: "/locations" }}>
        <PreviewLink
          href={`/reports/financial-report?location_id=${encodeURIComponent(location.id)}`}
          className="font-medium text-foreground hover:underline"
        >
          View financial report
        </PreviewLink>
      </PageHeader>
      <div className="grid gap-5 xl:grid-cols-2">
        <Panel title="Site Information">
          <DetailList items={{
            "Location code": location.code || "—",
            Email: location.email || "—",
            Phone: location.phone || "—",
            Status: <StatusBadge>{location.active ? "Active" : "Inactive"}</StatusBadge>,
          }} />
        </Panel>
        <Panel title="Address">
          <DetailList items={{
            Address: location.address.street_and_number || "—",
            City: location.city || "—",
            Country: location.address.country || "—",
            Coordinates: coordinates,
          }} />
        </Panel>
      </div>
    </div>
  );
}
