export const adminMessages = {
  newTripCreatedByDispatcher:
    'New trip created/updated by "{{DISPATCHER}}" for "{{CUSTOMER}}" on "{{DATE}}" at "{{TIME}}".',
  tripAcceptedByDriver:
    'Trip "#T{{TRIP_ID}}" for "{{CUSTOMER}}" has been accepted by Driver "{{DRIVER}}".',
  tripCanceledBy:
    'Trip for "{{CUSTOMER}}" scheduled on "{{DATE}}" has been cancelled by "{{CANCELED_BY}}".',
  driverIncidentReport:
    'Driver "{{DRIVER}}" reported an incident during Trip{{TRIP_ID}}. Please review the report "{{REPORT_ID}}".',
  clientContactRenewalReminder:
    'The contract with Client "{{CLIENT}}" will expire on "{{EXPIRY_DATE}}". Please take necessary renewal steps if required.',
  tripUpdatedBy:
    'Trip "#T{{TRIP_ID}}" has been updated by "{{UPDATED_BY}}". Please review the latest details.',
} as const

export const driverMessages = {
  newTripAssigned: 'Pickup "{{CUSTOMER}}" at "{{TIME}}" from "{{LOCATION}}".',
  tripCancelled:
    'Your trip at "{{TIME}}" with "{{CUSTOMER}}" has been cancelled.',
  tripReminder: 'Trip for "{{CUSTOMER}}" starts in {{MINUTES}} minutes.',
  tripUpdate: 'Pickup time for "{{CUSTOMER}}" updated to "{{TIME}}".',
  tripOverdue: "Trip start overdue by {{MINUTES}} mins. Please confirm.",
  customerArrived: '"{{CUSTOMER}}" has arrived at pickup location.',
  paymentReceived: "Payment received successfully for Trip #{{TRIP_ID}}.",
  newTripRequest:
    'Pickup "{{CUSTOMER}}" at "{{TIME}}" from "{{LOCATION}}" with "{{VEHICLE}}". Accept if available.',
  tripExpired: "This Trip request has been expired.",
  flightUpdateOnSchedule:
    "Flight {{FLIGHT_NUMBER}} is on schedule. Pickup {{CUSTOMER}} at {{TIME}}, {{AIRPORT}}.",
  flightUpdateDelayed:
    "Flight {{FLIGHT_NUMBER}} delayed to {{TIME}}. Updated pickup time for {{CUSTOMER}}.",
  flightUpdateEarly:
    "Flight {{FLIGHT_NUMBER}} arriving early at {{TIME}}. Adjust arrival accordingly.",
  flightUpdateCancelled:
    "Flight {{FLIGHT_NUMBER}} cancelled. Dispatcher will confirm next steps.",
} as const

export const customerMessages = {
  tripConfirm:
    "Your trip from {{PICKUP_LOCATION}} to {{DROP_LOCATION}} is confirmed for {{PICKUP_DATE_TIME}}.",
  tripReminderCustomer:
    "Your trip starts at {{TIME}}. {{DRIVER_NAME}} will pick you up from {{PICKUP_LOCATION}}.",
  startNavigation:
    "Driver {{DRIVER_NAME}} is on the way to pick you up from {{PICKUP_LOCATION}}. Estimated arrival in {{ETA}}.",
  driverArriving:
    "{{DRIVER_NAME}} is on the way to {{PICKUP_LOCATION}}. Estimated arrival: {{ETA}} minutes.",
  otpVerified:
    "Your trip has started. Have a safe and pleasant journey to {{DROP_LOCATION}}!",
  tripEnd:
    "You’ve arrived at your destination. Thank you for riding with us — we wish you a pleasant day ahead!",
  submitTripRequest:
    "Your trip request from {{PICKUP_LOCATION}} to {{DROP_LOCATION}} on {{PICKUP_DATE_TIME}} has been received. {{DISPATCHER}} will coordinate with you shortly regarding the request.",
  customersTripCancelled:
    "Your scheduled trip on {{PICKUP_DATE_TIME}} has been cancelled. Please contact the dispatcher for assistance or submit a new request if needed.",
  tripUpdateCustomer:
    "Your trip from {{PICKUP_LOCATION}} to {{DROP_LOCATION}} on {{PICKUP_DATE_TIME}} has been updated. Please review the latest details in your trip summary.",
} as const

export const dispatcherMessages = {
  driverStartedNavigation:
    'Trip "#T{{TRIP_ID}}": Driver "{{DRIVER}}" is on the way to the pickup location.',
  driverStartedRideToLocation:
    'Trip "#T{{TRIP_ID}}": Driver "{{DRIVER}}" has started the ride with "{{CUSTOMER}}", navigating to the drop-off location.',
  driverStartedRideToStop:
    'Trip "#T{{TRIP_ID}}": Driver {{DRIVER}} has started the ride with Patient {{CUSTOMER}}, navigating to the next stop.',
  driverCompletedTrip:
    'Trip "#T{{TRIP_ID}}": Driver "{{DRIVER}}" has completed the ride with "{{CUSTOMER}}".',
  newCustomerAssigned:
    "New customer {{CUSTOMER}} has been assigned to you. Please review their details.",
  customerRequestTrip:
    "New trip request received from {{CUSTOMER}}: {{PICKUP_LOCATION}} → {{DROP_LOCATION}} on {{PICKUP_DATE_TIME}}. Coordinate with the customer to confirm details and schedule the trip.",
} as const

type ExtractPlaceholders<S extends string> =
  S extends `${string}{{${infer Param}}}${infer Rest}`
    ? Param | ExtractPlaceholders<Rest>
    : never

// Build param type from placeholders
type ParamsFor<S extends string> =
  ExtractPlaceholders<S> extends never
    ? Record<string, never>
    : Record<ExtractPlaceholders<S>, string>

// Union of all messages
const messages = {
  ...driverMessages,
  ...customerMessages,
  ...adminMessages,
  ...dispatcherMessages,
}

type TemplateKey = keyof typeof messages

export const formatMessage = <K extends TemplateKey>(
  key: K,
  params: ParamsFor<(typeof messages)[K]>,
): string => {
  const template = messages[key]
  return template.replace(/{{(\w+)}}/g, (_, k) => params[k] ?? "")
}
