// Shipping Clarity — sample data
// LA-focused dataset; other cities have lighter data for demo

const SHIPPING_CATEGORIES = [
  { id: "small", label: "Small Package", short: "Small Pkg", desc: "UPS, FedEx, USPS, Amazon" },
  { id: "ltl", label: "LTL Freight", short: "LTL", desc: "Less-than-truckload" },
  { id: "ftl", label: "Truckload (FTL)", short: "FTL", desc: "Full-truck freight" },
  { id: "courier", label: "Last-Mile Courier", short: "Courier", desc: "Same-day & local" },
  { id: "intl", label: "International", short: "Intl", desc: "Cross-border & customs" },
];

const METRICS = [
  { id: "ontime",          label: "On-Time Delivery",       weight: 0.18 },
  { id: "damage",          label: "Damage & Claims",        weight: 0.13 },
  { id: "service",         label: "Customer Service",       weight: 0.11 },
  { id: "tracking",        label: "Tracking Accuracy",      weight: 0.09 },
  { id: "transit",         label: "Transit vs. Promise",    weight: 0.11 },
  { id: "price",           label: "Price Competitiveness",  weight: 0.09 },
  { id: "pickup",          label: "Pickup Reliability",     weight: 0.09 },
  { id: "professionalism", label: "Driver Professionalism", weight: 0.09 },
  { id: "infrastructure",  label: "Infrastructure",         weight: 0.11 },
];

// Convert a 0-100 score to a letter grade (Rotten-Tomatoes-style hybrid)
function scoreToLetter(s) {
  if (s >= 93) return "A+";
  if (s >= 88) return "A";
  if (s >= 83) return "A-";
  if (s >= 78) return "B+";
  if (s >= 73) return "B";
  if (s >= 68) return "B-";
  if (s >= 63) return "C+";
  if (s >= 58) return "C";
  if (s >= 53) return "C-";
  if (s >= 48) return "D+";
  if (s >= 43) return "D";
  if (s >= 38) return "D-";
  return "F";
}

function scoreToTier(s) {
  if (s >= 85) return { label: "Excellent", color: "var(--green)", icon: "★" };
  if (s >= 70) return { label: "Solid", color: "var(--ink)", icon: "✓" };
  if (s >= 55) return { label: "Mixed", color: "var(--amber)", icon: "~" };
  return { label: "Poor", color: "var(--red)", icon: "✕" };
}

// Featured cities (homepage)
const FEATURED_CITIES = [
  { id: "los-angeles", name: "Los Angeles", state: "CA", pop: "12.5M metro", topGrade: "B+", topCarrier: "UPS", x: 12, y: 58 },
  { id: "new-york", name: "New York", state: "NY", pop: "19.1M metro", topGrade: "A-", topCarrier: "FedEx", x: 82, y: 32 },
  { id: "chicago", name: "Chicago", state: "IL", pop: "9.4M metro", topGrade: "A", topCarrier: "UPS", x: 60, y: 36 },
  { id: "dallas", name: "Dallas", state: "TX", pop: "7.6M metro", topGrade: "B+", topCarrier: "FedEx", x: 50, y: 65 },
  { id: "atlanta", name: "Atlanta", state: "GA", pop: "6.1M metro", topGrade: "A-", topCarrier: "UPS", x: 71, y: 60 },
  { id: "phoenix", name: "Phoenix", state: "AZ", pop: "5.0M metro", topGrade: "B", topCarrier: "OnTrac", x: 22, y: 60 },
  { id: "seattle", name: "Seattle", state: "WA", pop: "4.0M metro", topGrade: "B+", topCarrier: "Amazon", x: 14, y: 18 },
  { id: "miami", name: "Miami", state: "FL", pop: "6.3M metro", topGrade: "B-", topCarrier: "USPS", x: 80, y: 78 },
  { id: "denver", name: "Denver", state: "CO", pop: "3.0M metro", topGrade: "B", topCarrier: "UPS", x: 38, y: 45 },
  { id: "boston", name: "Boston", state: "MA", pop: "4.9M metro", topGrade: "A-", topCarrier: "UPS", x: 86, y: 26 },
  { id: "houston", name: "Houston", state: "TX", pop: "7.3M metro", topGrade: "B", topCarrier: "FedEx", x: 54, y: 75 },
  { id: "philly", name: "Philadelphia", state: "PA", pop: "6.2M metro", topGrade: "B+", topCarrier: "UPS", x: 81, y: 36 },
];

// Search index — broader than featured
const ALL_CITIES = [
  ...FEATURED_CITIES,
  { id: "san-francisco", name: "San Francisco", state: "CA", pop: "4.6M metro" },
  { id: "san-diego", name: "San Diego", state: "CA", pop: "3.3M metro" },
  { id: "portland", name: "Portland", state: "OR", pop: "2.5M metro" },
  { id: "minneapolis", name: "Minneapolis", state: "MN", pop: "3.7M metro" },
  { id: "detroit", name: "Detroit", state: "MI", pop: "4.4M metro" },
  { id: "tampa", name: "Tampa", state: "FL", pop: "3.2M metro" },
  { id: "orlando", name: "Orlando", state: "FL", pop: "2.7M metro" },
  { id: "charlotte", name: "Charlotte", state: "NC", pop: "2.7M metro" },
  { id: "nashville", name: "Nashville", state: "TN", pop: "2.0M metro" },
  { id: "austin", name: "Austin", state: "TX", pop: "2.4M metro" },
  { id: "las-vegas", name: "Las Vegas", state: "NV", pop: "2.3M metro" },
  { id: "kansas-city", name: "Kansas City", state: "MO", pop: "2.2M metro" },
  { id: "indianapolis", name: "Indianapolis", state: "IN", pop: "2.1M metro" },
  { id: "columbus", name: "Columbus", state: "OH", pop: "2.1M metro" },
];

// LA carriers — full data per category
const LA_REPORT = {
  cityId: "los-angeles",
  cityName: "Los Angeles",
  state: "CA",
  metroPop: "12.5M",
  zips: 538,
  shipmentsTracked: "2.4M",
  reportPeriod: "Q1 2026",
  lastUpdated: "Apr 18, 2026",
  overall: {
    small: 81, ltl: 74, ftl: 79, courier: 77, intl: 71,
  },
  carriers: {
    small: [
      {
        id: "ups", name: "UPS", score: 89, trend: +2, shipments: "412K", complaints: 84,
        sponsored: false,
        scores: { ontime: 92, damage: 88, service: 85, tracking: 94, transit: 90, price: 78, pickup: 91, professionalism: 89 },
        verdict: "The most reliable choice in LA — premium pricing, but it shows up.",
        strengths: ["Industry-leading tracking", "Consistent pickup windows", "Low damage rate"],
        weaknesses: ["Pricing 12% above category median", "Surcharges add up on residential"],
      },
      {
        id: "fedex", name: "FedEx", score: 84, trend: -1, shipments: "298K", complaints: 112,
        sponsored: true,
        scores: { ontime: 86, damage: 82, service: 79, tracking: 91, transit: 87, price: 81, pickup: 84, professionalism: 86 },
        verdict: "Strong for time-definite air. Ground service is more inconsistent in LA county.",
        strengths: ["Best-in-class overnight", "Solid tracking", "Good pricing on Express"],
        weaknesses: ["Ground delays in San Fernando Valley", "Customer service hold times rising"],
      },
      {
        id: "usps", name: "USPS", score: 71, trend: -3, shipments: "601K", complaints: 287,
        sponsored: false,
        scores: { ontime: 68, damage: 74, service: 62, tracking: 65, transit: 70, price: 92, pickup: 73, professionalism: 75 },
        verdict: "Cheapest by far, but service is slipping. Best for non-urgent, lightweight parcels.",
        strengths: ["Unbeatable on price", "Universal coverage", "Good for residential"],
        weaknesses: ["Tracking gaps", "Inconsistent transit times", "Long service hold times"],
      },
      {
        id: "amazon", name: "Amazon Logistics", score: 76, trend: +5, shipments: "489K", complaints: 156,
        sponsored: false,
        scores: { ontime: 82, damage: 71, service: 64, tracking: 78, transit: 81, price: 86, pickup: 70, professionalism: 72 },
        verdict: "Fast and improving — but only if you're shipping through Amazon's ecosystem.",
        strengths: ["Aggressive transit times", "Good price", "Sunday delivery"],
        weaknesses: ["Limited to Amazon ecosystem", "Higher damage reports", "Driver consistency"],
      },
      {
        id: "ontrac", name: "OnTrac", score: 74, trend: +1, shipments: "187K", complaints: 92,
        sponsored: false,
        scores: { ontime: 78, damage: 72, service: 70, tracking: 76, transit: 79, price: 82, pickup: 74, professionalism: 73 },
        verdict: "Solid regional alternative for West Coast B2C — competitive pricing, decent reliability.",
        strengths: ["Regional speed advantage", "Better price than UPS/FedEx", "Improving tracking"],
        weaknesses: ["Limited coverage outside West", "Less mature support"],
      },
      {
        id: "dhl", name: "DHL eCommerce", score: 68, trend: -2, shipments: "94K", complaints: 78,
        sponsored: false,
        scores: { ontime: 65, damage: 70, service: 68, tracking: 72, transit: 64, price: 79, pickup: 66, professionalism: 70 },
        verdict: "Better suited for international handoffs than domestic last-mile.",
        strengths: ["Strong international", "Reasonable pricing"],
        weaknesses: ["Slow domestic transit", "Multi-handoff delays"],
      },
    ],
    ltl: [
      { id: "old-dominion", name: "Old Dominion", score: 91, trend: +1, shipments: "38K", complaints: 12, sponsored: false,
        scores: { ontime: 94, damage: 92, service: 89, tracking: 88, transit: 93, price: 76, pickup: 92, professionalism: 94 },
        verdict: "The benchmark for LTL in Southern California. Pay more, get more.",
        strengths: ["Damage-free record", "On-time leader", "Premium drivers"],
        weaknesses: ["Highest pricing in category", "Capacity tight in peak season"] },
      { id: "saia", name: "Saia", score: 84, trend: +3, shipments: "29K", complaints: 21, sponsored: true,
        scores: { ontime: 86, damage: 83, service: 82, tracking: 81, transit: 85, price: 81, pickup: 84, professionalism: 86 },
        verdict: "Climbing fast. Strong value play for regional LTL across the Southwest.",
        strengths: ["Improving every quarter", "Competitive pricing", "Good tech stack"],
        weaknesses: ["Still building density", "Less mature in remote ZIPs"] },
      { id: "estes", name: "Estes Express", score: 79, trend: 0, shipments: "26K", complaints: 28, sponsored: false,
        scores: { ontime: 80, damage: 79, service: 76, tracking: 75, transit: 81, price: 82, pickup: 80, professionalism: 78 },
        verdict: "Reliable middle-of-the-pack. No surprises good or bad.",
        strengths: ["Wide coverage", "Stable service"],
        weaknesses: ["Tracking trails competitors", "Service hold times"] },
      { id: "xpo", name: "XPO Logistics", score: 76, trend: -2, shipments: "31K", complaints: 41, sponsored: false,
        scores: { ontime: 75, damage: 78, service: 71, tracking: 79, transit: 76, price: 80, pickup: 72, professionalism: 78 },
        verdict: "Decent but pickup reliability has been a sticking point lately.",
        strengths: ["Good pricing", "Decent damage rate"],
        weaknesses: ["Pickup misses up YoY", "Slipping on-time"] },
      { id: "abf", name: "ABF Freight", score: 73, trend: -1, shipments: "18K", complaints: 24, sponsored: false,
        scores: { ontime: 73, damage: 76, service: 70, tracking: 71, transit: 74, price: 78, pickup: 74, professionalism: 72 },
        verdict: "Union carrier with solid bones — but service speed is a known tradeoff.",
        strengths: ["Reliable for heavyweight", "Strong claims handling"],
        weaknesses: ["Slower transit", "Older tracking tech"] },
    ],
    ftl: [
      { id: "knight-swift", name: "Knight-Swift", score: 86, trend: +2, shipments: "11K", complaints: 18, sponsored: false,
        scores: { ontime: 88, damage: 89, service: 84, tracking: 86, transit: 87, price: 80, pickup: 88, professionalism: 86 },
        verdict: "Big-fleet capacity with surprisingly tight execution. Top pick for FTL in LA.",
        strengths: ["Massive capacity", "Good driver pool", "Reliable ETAs"],
        weaknesses: ["Pricing premium", "Less flexible on small shippers"] },
      { id: "schneider", name: "Schneider National", score: 83, trend: +1, shipments: "9K", complaints: 14, sponsored: false,
        scores: { ontime: 85, damage: 84, service: 81, tracking: 88, transit: 84, price: 79, pickup: 83, professionalism: 84 },
        verdict: "Tech-forward and reliable. Excellent visibility, competitive on price.",
        strengths: ["Best-in-class tracking", "Strong tech tools"],
        weaknesses: ["Capacity tight in peaks"] },
      { id: "jb-hunt", name: "J.B. Hunt", score: 82, trend: 0, shipments: "12K", complaints: 22, sponsored: true,
        scores: { ontime: 84, damage: 82, service: 80, tracking: 86, transit: 83, price: 78, pickup: 82, professionalism: 83 },
        verdict: "Intermodal specialist — great rail-truck combo for LA-to-Midwest lanes.",
        strengths: ["Intermodal leader", "Solid network density"],
        weaknesses: ["Premium pricing", "Less flexible on spot"] },
      { id: "werner", name: "Werner Enterprises", score: 76, trend: -1, shipments: "7K", complaints: 19, sponsored: false,
        scores: { ontime: 76, damage: 78, service: 73, tracking: 78, transit: 77, price: 81, pickup: 74, professionalism: 76 },
        verdict: "Middle-of-pack with strong long-haul lanes; weaker on short regional runs.",
        strengths: ["Long-haul reliable", "OK pricing"],
        weaknesses: ["Regional misses", "Service slow"] },
    ],
    courier: [
      { id: "uber-freight", name: "Uber Direct", score: 82, trend: +4, shipments: "61K", complaints: 88, sponsored: false,
        scores: { ontime: 86, damage: 78, service: 75, tracking: 90, transit: 88, price: 80, pickup: 84, professionalism: 76 },
        verdict: "Best-in-class for under-2-hour delivery in core LA. Outside the city, results vary.",
        strengths: ["Sub-2hr delivery", "Live tracking", "API-friendly"],
        weaknesses: ["Patchy outside LA core", "Driver consistency varies"] },
      { id: "doordash-drive", name: "DoorDash Drive", score: 79, trend: +2, shipments: "54K", complaints: 91, sponsored: false,
        scores: { ontime: 82, damage: 75, service: 72, tracking: 86, transit: 84, price: 82, pickup: 80, professionalism: 74 },
        verdict: "Strong for retail same-day — improving fast on the B2B side.",
        strengths: ["Wide driver network", "Good app experience"],
        weaknesses: ["Damage rate higher", "Limited large-item support"] },
      { id: "roadie", name: "Roadie", score: 76, trend: +1, shipments: "23K", complaints: 41, sponsored: false,
        scores: { ontime: 78, damage: 74, service: 76, tracking: 79, transit: 80, price: 79, pickup: 76, professionalism: 75 },
        verdict: "Best for awkward, oversized, or scheduled same-day. UPS-owned now.",
        strengths: ["Handles oversized", "Scheduled delivery"],
        weaknesses: ["Smaller driver pool", "Pricing varies wildly"] },
      { id: "courier-express", name: "LA Courier Express", score: 71, trend: 0, shipments: "8K", complaints: 19, sponsored: false,
        scores: { ontime: 72, damage: 73, service: 75, tracking: 64, transit: 73, price: 78, pickup: 70, professionalism: 76 },
        verdict: "Local independent. Personal service, but less polished tech.",
        strengths: ["Personal service", "Flexible scheduling"],
        weaknesses: ["Weak tracking", "Limited capacity"] },
    ],
    intl: [
      { id: "dhl-express", name: "DHL Express", score: 87, trend: +1, shipments: "78K", complaints: 31, sponsored: false,
        scores: { ontime: 90, damage: 86, service: 84, tracking: 91, transit: 89, price: 76, pickup: 88, professionalism: 87 },
        verdict: "The gold standard for international from LAX. Premium pricing earns its keep.",
        strengths: ["Best customs clearance", "Global tracking", "Reliable transit"],
        weaknesses: ["Premium pricing", "Brokerage fees"] },
      { id: "fedex-intl", name: "FedEx International", score: 82, trend: 0, shipments: "64K", complaints: 38, sponsored: false,
        scores: { ontime: 84, damage: 81, service: 79, tracking: 86, transit: 83, price: 79, pickup: 82, professionalism: 83 },
        verdict: "Strong all-rounder. Slight edge on Asia-Pacific lanes from LAX.",
        strengths: ["Asia-Pacific strength", "Good tracking"],
        weaknesses: ["Customs delays in Q4"] },
      { id: "ups-intl", name: "UPS Worldwide", score: 80, trend: -1, shipments: "57K", complaints: 42, sponsored: false,
        scores: { ontime: 82, damage: 84, service: 78, tracking: 84, transit: 80, price: 77, pickup: 81, professionalism: 82 },
        verdict: "Reliable, especially for Europe lanes. Customer service pressure points.",
        strengths: ["Europe lanes strong", "Low damage"],
        weaknesses: ["Service hold times", "Customs paperwork"] },
      { id: "usps-intl", name: "USPS International", score: 58, trend: -4, shipments: "29K", complaints: 87, sponsored: false,
        scores: { ontime: 54, damage: 62, service: 51, tracking: 48, transit: 56, price: 91, pickup: 64, professionalism: 68 },
        verdict: "Cheap, but you get what you pay for. Tracking often goes dark at customs.",
        strengths: ["Cheapest by far", "Universal access"],
        weaknesses: ["Tracking dark zones", "Slow customs", "Service nightmare"] },
    ],
  },
};

// Insights/news for the homepage
const INSIGHTS = [
  { tag: "Q1 2026 Report", title: "UPS reclaims #1 in LA small-package — by a hair", date: "Apr 22", read: "4 min" },
  { tag: "Trend", title: "Why USPS scores are dropping in major metros", date: "Apr 18", read: "6 min" },
  { tag: "Buyer's Guide", title: "How to pick an LTL carrier when price isn't everything", date: "Apr 12", read: "8 min" },
  { tag: "Spotlight", title: "Saia's quiet rise: what they're getting right", date: "Apr 08", read: "5 min" },
];

// Driver report data — keyed by carrier id. Composite "driver score"
// blends professionalism, retention, training and driver sentiment.
const DRIVER_REPORTS = {
  ups: {
    score: 87, trend: +1,
    fleet: { drivers: "11.4K", avgTenure: "8.2 yr", retention: "91%", employee: 100, contractor: 0 },
    training: "32 hrs onboarding · annual safety recert",
    safety: { incidents: 0.8, complaints: 12, citations: 4 },
    sentiment: { positive: 78, neutral: 14, negative: 8 },
    strengths: ["Union-backed wage floor", "Long driver tenure", "Well-known training pipeline"],
    concerns: ["Heavy peak-season overtime", "Aging fleet on residential routes"],
    quotes: [
      { who: "12-yr feeder driver, Inland Empire", text: "Pay's why people stay. Routes are tight but the equipment works." },
      { who: "Package car driver, LA basin", text: "We're hustling, but customers know us by name. That counts for something." },
    ],
  },
  fedex: {
    score: 78, trend: -2,
    fleet: { drivers: "8.7K", avgTenure: "4.1 yr", retention: "72%", employee: 22, contractor: 78 },
    training: "Varies by ISP contractor · FedEx-set safety floor",
    safety: { incidents: 1.4, complaints: 38, citations: 11 },
    sentiment: { positive: 58, neutral: 24, negative: 18 },
    strengths: ["Express drivers highly rated", "Solid tracking discipline"],
    concerns: ["ISP model creates uneven service", "Driver turnover up 14% YoY", "Pay varies by contractor"],
    quotes: [
      { who: "Ground ISP driver, San Fernando Valley", text: "Different van, different rules every quarter. Customers feel it before we do." },
      { who: "Express courier, LAX hub", text: "Air side runs like clockwork. Ground is a different company entirely." },
    ],
  },
  usps: {
    score: 69, trend: -3,
    fleet: { drivers: "14.2K", avgTenure: "11.6 yr", retention: "84%", employee: 100, contractor: 0 },
    training: "Federal standard · 80 hrs onboarding",
    safety: { incidents: 1.1, complaints: 76, citations: 9 },
    sentiment: { positive: 52, neutral: 28, negative: 20 },
    strengths: ["Career carriers with deep route knowledge", "Strong federal benefits"],
    concerns: ["Mandatory overtime running high", "CCAs (city carrier assistants) churning fast", "Fleet electrification stalled"],
    quotes: [
      { who: "26-yr letter carrier, West LA", text: "Volume's doubled, staffing hasn't. We do what we can." },
      { who: "CCA, year 2", text: "Six-day weeks, no guaranteed schedule. Most of us don't last to year three." },
    ],
  },
  amazon: {
    score: 71, trend: +4,
    fleet: { drivers: "6.8K", avgTenure: "1.9 yr", retention: "61%", employee: 0, contractor: 100 },
    training: "DSP-managed · 1 wk onboarding · scorecard-driven",
    safety: { incidents: 1.9, complaints: 48, citations: 14 },
    sentiment: { positive: 49, neutral: 26, negative: 25 },
    strengths: ["Heavy investment in driver-facing tech", "Improving safety scores"],
    concerns: ["DSP model = high churn", "Aggressive route quotas", "Camera-monitored cabs unpopular with drivers"],
    quotes: [
      { who: "DSP driver, year 1", text: "App tells me when to pee. The pay's fine, the pressure isn't." },
      { who: "DSP fleet manager, SoCal", text: "We rehire 60% of our roster every year. That's the model, not a bug." },
    ],
  },
  ontrac: {
    score: 73, trend: +2,
    fleet: { drivers: "1.9K", avgTenure: "3.4 yr", retention: "76%", employee: 35, contractor: 65 },
    training: "Mixed model · 2 wk onboarding",
    safety: { incidents: 1.2, complaints: 21, citations: 6 },
    sentiment: { positive: 64, neutral: 22, negative: 14 },
    strengths: ["Smaller routes, less burnout", "Regional density helps drivers"],
    concerns: ["Pay below FedEx Ground floor in some markets", "Tracking tools playing catch-up"],
    quotes: [
      { who: "Driver, Riverside route, 4 yr", text: "Better than the big guys for quality of life. Worse on pay." },
    ],
  },
  dhl: {
    score: 67, trend: -1,
    fleet: { drivers: "1.2K", avgTenure: "2.8 yr", retention: "68%", employee: 18, contractor: 82 },
    training: "Contractor-managed · varies",
    safety: { incidents: 1.5, complaints: 24, citations: 7 },
    sentiment: { positive: 51, neutral: 28, negative: 21 },
    strengths: ["International handoff drivers well-trained"],
    concerns: ["Last-mile contracted out, quality varies", "Pay structure complicated"],
    quotes: [
      { who: "Last-mile contractor, Long Beach", text: "We're the third pair of hands a package sees. That's where the cracks show." },
    ],
  },
  // Generic fallback — used for carriers without an explicit entry
  _default: {
    score: 75, trend: 0,
    fleet: { drivers: "—", avgTenure: "—", retention: "—", employee: 50, contractor: 50 },
    training: "Industry standard",
    safety: { incidents: 1.2, complaints: 20, citations: 5 },
    sentiment: { positive: 60, neutral: 25, negative: 15 },
    strengths: ["Driver data limited — request via methodology page"],
    concerns: ["Public driver data thin for this carrier"],
    quotes: [],
  },
};

window.SI_DATA = {
  SHIPPING_CATEGORIES, METRICS, FEATURED_CITIES, ALL_CITIES, LA_REPORT, INSIGHTS, DRIVER_REPORTS,
  scoreToLetter, scoreToTier,
};
