/**
 * The first California transport corridor: two coarse LA-to-Bay itineraries.
 *
 * This is simulation geometry, not a navigation dataset. Coordinates were
 * placed by hand at recognisable cities and junctions, with long road sections
 * represented by one straight edge. In particular, I-5 does not enter San
 * Francisco: that itinerary names its real Bay approach over I-580 and I-80.
 */

import type {
  TransportAnchor,
  TransportNode,
  TransportPack,
  TransportRoute,
  TransportSegment,
} from "./types.ts";

const AUTHORED_NODE =
  "Original coarse waypoint authored by the Tera project from general California geography; approximate and not for navigation.";
const AUTHORED_ROAD =
  "Original coarse simulation segment authored by the Tera project; road identity and representative speed/lanes are approximate, not live navigation data.";
const INTERNAL_OFFICE =
  "Transition coordinate mirrors Tera's authored office site catalogue; it is project data, not copied map geometry.";

export const CALIFORNIA_TRANSPORT_NODES: readonly TransportNode[] = [
  {
    id: "los-angeles",
    label: "Los Angeles",
    kind: "terminus",
    position: { lat: 34.0522, lng: -118.2437 },
    provenance: AUTHORED_NODE,
  },

  // US-101: the coast and Salinas Valley approach.
  {
    id: "ventura",
    label: "Ventura",
    kind: "waypoint",
    position: { lat: 34.2805, lng: -119.2945 },
    provenance: AUTHORED_NODE,
  },
  {
    id: "santa-barbara",
    label: "Santa Barbara",
    kind: "waypoint",
    position: { lat: 34.4208, lng: -119.6982 },
    provenance: AUTHORED_NODE,
  },
  {
    id: "santa-maria",
    label: "Santa Maria",
    kind: "waypoint",
    position: { lat: 34.953, lng: -120.4357 },
    provenance: AUTHORED_NODE,
  },
  {
    id: "san-luis-obispo",
    label: "San Luis Obispo",
    kind: "waypoint",
    position: { lat: 35.2828, lng: -120.6596 },
    provenance: AUTHORED_NODE,
  },
  {
    id: "paso-robles",
    label: "Paso Robles",
    kind: "waypoint",
    position: { lat: 35.626, lng: -120.691 },
    provenance: AUTHORED_NODE,
  },
  {
    id: "king-city",
    label: "King City",
    kind: "waypoint",
    position: { lat: 36.2127, lng: -121.126 },
    provenance: AUTHORED_NODE,
  },
  {
    id: "salinas",
    label: "Salinas",
    kind: "waypoint",
    position: { lat: 36.6777, lng: -121.6555 },
    provenance: AUTHORED_NODE,
  },
  {
    id: "gilroy",
    label: "Gilroy",
    kind: "waypoint",
    position: { lat: 37.0058, lng: -121.5683 },
    provenance: AUTHORED_NODE,
  },
  {
    id: "san-jose",
    label: "San Jose",
    kind: "junction",
    position: { lat: 37.3382, lng: -121.8863 },
    provenance: AUTHORED_NODE,
  },
  {
    id: "redwood-city",
    label: "Redwood City",
    kind: "waypoint",
    position: { lat: 37.4852, lng: -122.2364 },
    provenance: AUTHORED_NODE,
  },

  // I-5: Central Valley, followed by an explicitly named Bay connector.
  {
    id: "santa-clarita",
    label: "Santa Clarita",
    kind: "waypoint",
    position: { lat: 34.3917, lng: -118.5426 },
    provenance: AUTHORED_NODE,
  },
  {
    id: "grapevine",
    label: "Grapevine",
    kind: "waypoint",
    position: { lat: 34.9416, lng: -118.929 },
    provenance: AUTHORED_NODE,
  },
  {
    id: "lost-hills",
    label: "Lost Hills",
    kind: "waypoint",
    position: { lat: 35.6166, lng: -119.6943 },
    provenance: AUTHORED_NODE,
  },
  {
    id: "coalinga-interchange",
    label: "Coalinga / SR-198",
    kind: "junction",
    position: { lat: 36.253, lng: -120.237 },
    provenance: AUTHORED_NODE,
  },
  {
    id: "santa-nella",
    label: "Santa Nella",
    kind: "junction",
    position: { lat: 37.102, lng: -121.016 },
    provenance: AUTHORED_NODE,
  },
  {
    id: "tracy",
    label: "Tracy",
    kind: "junction",
    position: { lat: 37.7397, lng: -121.4252 },
    provenance: AUTHORED_NODE,
  },
  {
    id: "altamont-pass",
    label: "Altamont Pass",
    kind: "waypoint",
    position: { lat: 37.696, lng: -121.686 },
    provenance: AUTHORED_NODE,
  },
  {
    id: "dublin",
    label: "Dublin",
    kind: "waypoint",
    position: { lat: 37.7022, lng: -121.9358 },
    provenance: AUTHORED_NODE,
  },
  {
    id: "oakland",
    label: "Oakland",
    kind: "junction",
    position: { lat: 37.8044, lng: -122.2712 },
    provenance: AUTHORED_NODE,
  },
  {
    id: "bay-bridge",
    label: "San Francisco-Oakland Bay Bridge",
    kind: "junction",
    position: { lat: 37.7983, lng: -122.3778 },
    provenance: AUTHORED_NODE,
  },
  {
    id: "san-francisco",
    label: "San Francisco",
    kind: "terminus",
    position: { lat: 37.7749, lng: -122.4194 },
    provenance: AUTHORED_NODE,
  },
];

export const CALIFORNIA_TRANSPORT_SEGMENTS: readonly TransportSegment[] = [
  // US-101 route.
  ["us101-la-ventura", "los-angeles", "ventura", 65, 3],
  ["us101-ventura-santa-barbara", "ventura", "santa-barbara", 65, 2],
  ["us101-santa-barbara-santa-maria", "santa-barbara", "santa-maria", 65, 2],
  ["us101-santa-maria-san-luis-obispo", "santa-maria", "san-luis-obispo", 65, 2],
  ["us101-san-luis-obispo-paso-robles", "san-luis-obispo", "paso-robles", 65, 2],
  ["us101-paso-robles-king-city", "paso-robles", "king-city", 65, 2],
  ["us101-king-city-salinas", "king-city", "salinas", 65, 2],
  ["us101-salinas-gilroy", "salinas", "gilroy", 65, 2],
  ["us101-gilroy-san-jose", "gilroy", "san-jose", 65, 3],
  ["us101-san-jose-redwood-city", "san-jose", "redwood-city", 65, 4],
  ["us101-redwood-city-san-francisco", "redwood-city", "san-francisco", 65, 4],
].map(([id, fromNodeId, toNodeId, speedLimitMph, lanesPerDirection]) => ({
  id: id as string,
  fromNodeId: fromNodeId as string,
  toNodeId: toNodeId as string,
  roadName: "US-101",
  kind: "us-highway" as const,
  speedLimitMph: speedLimitMph as number,
  lanesPerDirection: lanesPerDirection as number,
  provenance: AUTHORED_ROAD,
}));

const INTERSTATE_SEGMENTS: readonly TransportSegment[] = [
  ["i5-la-santa-clarita", "los-angeles", "santa-clarita", 65, 4],
  ["i5-santa-clarita-grapevine", "santa-clarita", "grapevine", 65, 3],
  ["i5-grapevine-lost-hills", "grapevine", "lost-hills", 70, 2],
  ["i5-lost-hills-coalinga", "lost-hills", "coalinga-interchange", 70, 2],
  ["i5-coalinga-santa-nella", "coalinga-interchange", "santa-nella", 70, 2],
  ["i5-santa-nella-tracy", "santa-nella", "tracy", 70, 3],
].map(([id, fromNodeId, toNodeId, speedLimitMph, lanesPerDirection]) => ({
  id: id as string,
  fromNodeId: fromNodeId as string,
  toNodeId: toNodeId as string,
  roadName: "I-5",
  kind: "interstate" as const,
  speedLimitMph: speedLimitMph as number,
  lanesPerDirection: lanesPerDirection as number,
  provenance: AUTHORED_ROAD,
}));

const BAY_CONNECTOR_SEGMENTS: readonly TransportSegment[] = [
  {
    id: "i580-tracy-altamont",
    fromNodeId: "tracy",
    toNodeId: "altamont-pass",
    roadName: "I-580",
    kind: "connector",
    speedLimitMph: 65,
    lanesPerDirection: 3,
    provenance: AUTHORED_ROAD,
  },
  {
    id: "i580-altamont-dublin",
    fromNodeId: "altamont-pass",
    toNodeId: "dublin",
    roadName: "I-580",
    kind: "connector",
    speedLimitMph: 65,
    lanesPerDirection: 4,
    provenance: AUTHORED_ROAD,
  },
  {
    id: "i580-dublin-oakland",
    fromNodeId: "dublin",
    toNodeId: "oakland",
    roadName: "I-580",
    kind: "connector",
    speedLimitMph: 65,
    lanesPerDirection: 4,
    provenance: AUTHORED_ROAD,
  },
  {
    id: "i80-oakland-bay-bridge",
    fromNodeId: "oakland",
    toNodeId: "bay-bridge",
    roadName: "I-80 / Bay Bridge",
    kind: "connector",
    speedLimitMph: 50,
    lanesPerDirection: 5,
    provenance: AUTHORED_ROAD,
  },
  {
    id: "i80-bay-bridge-san-francisco",
    fromNodeId: "bay-bridge",
    toNodeId: "san-francisco",
    roadName: "I-80",
    kind: "connector",
    speedLimitMph: 50,
    lanesPerDirection: 5,
    provenance: AUTHORED_ROAD,
  },
];

export const CALIFORNIA_I5_SEGMENTS: readonly TransportSegment[] = [
  ...INTERSTATE_SEGMENTS,
  ...BAY_CONNECTOR_SEGMENTS,
];

const US_101_SEGMENT_IDS = CALIFORNIA_TRANSPORT_SEGMENTS.map((segment) => segment.id);
const I_5_SEGMENT_IDS = CALIFORNIA_I5_SEGMENTS.map((segment) => segment.id);

export const CALIFORNIA_TRANSPORT_ROUTES: readonly TransportRoute[] = [
  {
    id: "la-sf-us-101",
    label: "Los Angeles to San Francisco via US-101",
    description: "The coastal and Salinas Valley route through Santa Barbara and San Jose.",
    fromNodeId: "los-angeles",
    toNodeId: "san-francisco",
    segmentIds: US_101_SEGMENT_IDS,
    provenance: AUTHORED_ROAD,
  },
  {
    id: "la-sf-i-5",
    label: "Los Angeles to San Francisco via I-5, I-580, and I-80",
    description:
      "The Central Valley route, leaving I-5 at Tracy for I-580 and I-80 across the Bay Bridge.",
    fromNodeId: "los-angeles",
    toNodeId: "san-francisco",
    segmentIds: I_5_SEGMENT_IDS,
    provenance: AUTHORED_ROAD,
  },
];

export const CALIFORNIA_TRANSPORT_ANCHORS: readonly TransportAnchor[] = [
  {
    id: "city-socal",
    kind: "city",
    cityId: "socal",
    label: "Southern California",
    nodeId: "los-angeles",
    position: { lat: 34.0522, lng: -118.2437 },
    provenance: AUTHORED_NODE,
  },
  {
    id: "city-sf",
    kind: "city",
    cityId: "sf",
    label: "San Francisco",
    nodeId: "san-francisco",
    position: { lat: 37.7749, lng: -122.4194 },
    provenance: AUTHORED_NODE,
  },
  {
    id: "office-mateo-court",
    kind: "office",
    cityId: "socal",
    officeId: "mateo-court",
    label: "Mateo Court",
    nodeId: "los-angeles",
    position: { lat: 34.0395, lng: -118.2288 },
    provenance: INTERNAL_OFFICE,
  },
  {
    id: "office-lumbridge-hq",
    kind: "office",
    cityId: "sf",
    officeId: "lumbridge-hq",
    label: "Lumbridge HQ",
    nodeId: "san-francisco",
    position: { lat: 37.7897, lng: -122.3972 },
    provenance: INTERNAL_OFFICE,
  },
  {
    id: "office-frontier-valley",
    kind: "office",
    cityId: "sf",
    officeId: "frontier-valley",
    label: "Frontier Valley",
    nodeId: "oakland",
    position: { lat: 37.7756, lng: -122.3186 },
    provenance: INTERNAL_OFFICE,
  },
];

/** All corridor data in one JSON-safe object. */
export const CALIFORNIA_TRANSPORT: TransportPack = {
  id: "california-la-bay",
  name: "California: Los Angeles to the Bay",
  schemaVersion: 1,
  description:
    "A coarse statewide simulation corridor connecting Tera's Southern California and San Francisco scenes.",
  nodes: CALIFORNIA_TRANSPORT_NODES,
  segments: [...CALIFORNIA_TRANSPORT_SEGMENTS, ...CALIFORNIA_I5_SEGMENTS],
  routes: CALIFORNIA_TRANSPORT_ROUTES,
  anchors: CALIFORNIA_TRANSPORT_ANCHORS,
  provenance: [
    "Original manually authored Tera project data; no third-party map geometry is embedded.",
    "Coordinates, lane counts, and speed envelopes are coarse simulation inputs and must not be used for navigation.",
    "Office transition coordinates mirror the repository's own src/offices/sites.ts catalogue.",
  ],
};

export default CALIFORNIA_TRANSPORT;
