'use client'

import type { TripParticipant } from '@/types/db'

/**
 * A purchased route in its THREE delivered formats (2026-09-07 meetings).
 *
 * Nash, morning: "it's 3 types of maps delivered. First one, it's a GPX file.
 * Second one, it's a Hummer GPS. And the third one is Google, which could be
 * one part. But it could be like eight parts or 12 parts for Texas."
 *
 * Nash, midday (Task 66): "How we're gonna display the Hammer GPS and GPX file
 * in this Section 3 of the trip. We need to include that in there, too."
 *
 * One component for BOTH surfaces — the driver's Active Trip state section and
 * the desktop workspace permit card — so the two can never drift apart.
 */

export interface RouteFormats {
  route_links: string[] | null
  route_gpx_url: string | null
  route_hummer_url: string | null
}

export const ROUTE_CHIP =
  'rounded-md bg-paper px-2 py-1 text-[10px] font-bold text-info ring-1 ring-line hover:ring-navy-500'

export function hasAnyRouteFormat(r: RouteFormats | null | undefined): boolean {
  return !!r && ((r.route_links?.length ?? 0) > 0 || !!r.route_gpx_url || !!r.route_hummer_url)
}

/** GPX · Hummer GPS · Google Maps (1 chip, or Part 1..N). Hidden chips for missing formats. */
export function RouteFormatChips({
  request,
  chipClassName = ROUTE_CHIP,
}: {
  request: RouteFormats
  chipClassName?: string
}) {
  const links = request.route_links ?? []
  return (
    <>
      {request.route_gpx_url && (
        <a
          href={request.route_gpx_url}
          target="_blank"
          rel="noreferrer"
          className={chipClassName}
          title="GPX file — send it to your truck GPS over Bluetooth"
        >
          📄 GPX
        </a>
      )}
      {request.route_hummer_url && (
        <a
          href={request.route_hummer_url}
          target="_blank"
          rel="noreferrer"
          className={chipClassName}
          title="Open this route in the Hummer GPS app"
        >
          📱 Hummer GPS
        </a>
      )}
      {links.length === 1 ? (
        <a href={links[0]} target="_blank" rel="noreferrer" className={chipClassName}>
          🗺️ Google Maps ↗
        </a>
      ) : (
        links.map((link, i) => (
          <a key={link} href={link} target="_blank" rel="noreferrer" className={chipClassName}>
            {i === 0 && '🗺️ '}Part {i + 1} ↗
          </a>
        ))
      )}
    </>
  )
}

/**
 * Who ordered a route, resolved to name + trip role (Task 70). Nash: "who
 * placed the request… Which member of the group? It could be the broker, it
 * could be the driver. Who made it, you know? This is public."
 */
export function resolveRequester(
  request: { requested_by?: string | null; requester_label?: string | null },
  participants: TripParticipant[],
): { name: string; role: string | null } {
  const match =
    (request.requested_by &&
      participants.find((p) => p.user_id && p.user_id === request.requested_by)) ||
    participants.find((p) => p.name === request.requester_label)
  return {
    name: match?.name || request.requester_label || 'a trip member',
    role: match?.role ?? null,
  }
}

export function requesterText(r: { name: string; role: string | null }): string {
  return r.role ? `${r.name} (${r.role})` : r.name
}
