CURRENT CANONICAL BASELINE
Version: v41
Status: CRM timeline edit and deletion governance refinement
Next planned package: v42

# Routing and Distance Plan

## Current phase note
- No routing or distance logic changes are introduced in v42; this package focuses on CRM timeline governance and auditability.

# Routing and Distance Plan

## Purpose
This document preserves the planned architecture for route-aware load creation, distance calculation, and mapping integration in Dispatch Spot.

## Why This Matters
Pickup location, delivery location, and miles are not static spreadsheet fields. In the application they must become route-aware operational data so that dispatch, profitability, and driver instructions are based on an actual routing engine rather than manual guesswork.

## Recommended Provider Strategy
Primary recommendation:
- Google Maps Platform Routes API for routing, route miles, travel time, place search, and geocoding

Strong alternatives to preserve in the architecture:
- HERE
- Mapbox

## Architecture Rule
Routing should be implemented behind a provider abstraction so the application can change or compare providers later without rewriting load workflows.

## Planned Functional Responsibilities
- Pickup address search / autocomplete
- Delivery address search / autocomplete
- Address normalization
- Geocoding
- Route calculation
- Travel duration calculation
- Route miles persistence
- Optional manual mileage override
- Source attribution for calculated vs manual vs broker-quoted miles

## Key Freight-Specific Requirement
Dispatch Spot should eventually distinguish between:
- Route Miles: returned by the routing provider
- Quoted Miles: broker or commercial miles
- Operational / KPI Miles: the miles actually used for performance and profitability rules

This separation matters because real dispatch operations often rely on broker-confirmed miles that differ from route-engine miles.

## Planned Schema Direction
Future load/location schema should preserve the following direction:

### Pickup
- pickupAddressRaw
- pickupFormattedAddress
- pickupPlaceId
- pickupLat
- pickupLng

### Delivery
- deliveryAddressRaw
- deliveryFormattedAddress
- deliveryPlaceId
- deliveryLat
- deliveryLng

### Route Data
- routeMiles
- routeDurationMinutes
- routeProvider
- routeCalculatedAt

### Commercial Override Data
- quotedMiles
- milesSource

Recommended milesSource values:
- ROUTE_PROVIDER
- BROKER_QUOTED
- MANUAL_OVERRIDE

## Workflow Direction
Later implementation should support:
- route-aware load create/edit flows
- recalculation when pickup or delivery changes
- clear indication of how miles were derived
- optional manual override with audit visibility

## Mobile Readiness Notes
This routing layer is important for future Android and iOS support because it will later enable:
- route preview cards
- place autocomplete
- navigation handoff
- mileage validation
- route-aware field workflows

## Timing Decision
Document now, implement later.

The current platform should preserve this architecture immediately, but the actual mapping integration should be handled in a dedicated implementation phase so it does not interrupt current dispatch and dashboard work.
