/**
 * GENERATED by `imprint emit` — DO NOT EDIT BY HAND.
 *
 * Tool: get_flight_booking_details
 * Site: google-flights
 * Intent: Get booking/fare details and bookable options for a specific selected flight itinerary on Google Flights.
 *
 * To regenerate: imprint emit ~/.imprint/google-flights/get_flight_booking_details/workflow.json --force
 */

import { fileURLToPath } from 'node:url';
import { dirname, join } from 'node:path';
import {
  executeWorkflow,
  type CredentialStore,
} from 'imprint/runtime';
import type { ToolResult, Workflow } from 'imprint/types';

const WORKFLOW: Workflow = {
  "toolName": "get_flight_booking_details",
  "intent": {
    "description": "Get booking/fare details and bookable options for a specific selected flight itinerary on Google Flights.",
    "userSaid": "clicked one of the one-way fares to find more details and booking details; saw the options for another flight; chose round trip fares; kept exploring round trip fare details"
  },
  "parameters": [
    {
      "name": "origin",
      "type": "string",
      "description": "Origin airport IATA code for the outbound leg, e.g. SJC",
      "verified": false,
      "verifyNote": "annotated"
    },
    {
      "name": "destination",
      "type": "string",
      "description": "Destination airport IATA code for the outbound leg, e.g. SAN",
      "verified": false,
      "verifyNote": "annotated"
    },
    {
      "name": "departure_date",
      "type": "string",
      "description": "Outbound date in YYYY-MM-DD",
      "verified": false,
      "verifyNote": "annotated"
    },
    {
      "name": "return_date",
      "type": "string",
      "description": "Return date in YYYY-MM-DD. Empty/omit for a one-way booking.",
      "default": "",
      "verified": true
    },
    {
      "name": "outbound_flight",
      "type": "string",
      "description": "Selected outbound flight as 'carrier number', e.g. 'WN 3489'",
      "verified": false,
      "verifyNote": "annotated"
    },
    {
      "name": "return_flight",
      "type": "string",
      "description": "Selected return flight as 'carrier number' (round trip only), e.g. 'WN 3540'. Empty for one-way.",
      "default": "",
      "verified": false,
      "verifyNote": "annotated"
    },
    {
      "name": "flight_token",
      "type": "string",
      "description": "Opaque per-itinerary booking token minted by the search_flights tool's flight_token output for the selected itinerary."
    }
  ],
  "requests": [
    {
      "method": "POST",
      "url": "https://www.google.com/_/FlightsFrontendUi/data/travel.frontend.flights.FlightsFrontendService/GetBookingResults?f.sid=${state.f_sid}&bl=${state.bl}&hl=en-US&soc-app=162&soc-platform=1&soc-device=1&_reqid=1659189&rt=c",
      "headers": {
        "Content-Type": "application/x-www-form-urlencoded;charset=UTF-8",
        "X-Same-Domain": "1",
        "Referer": "https://www.google.com/travel/flights",
        "x-goog-ext-259736195-jspb": "[\"en-US\",\"US\",\"USD\",2,null,[420],null,null,7,[]]"
      },
      "body": "f.req=${param.origin}|${param.destination}|${param.departure_date}|${param.return_date}|${param.outbound_flight}|${param.return_flight}|${param.flight_token}&",
      "effect": "safe"
    }
  ],
  "site": "google-flights",
  "bootstrap": {
    "url": "https://www.google.com/travel/flights",
    "waitUntil": "domcontentloaded",
    "timeoutMs": 30000,
    "captures": [
      {
        "name": "f_sid",
        "required": false,
        "capability": "browser_bootstrap",
        "source": "html_regex",
        "pattern": "\"FdrFJe\":\"([^\"]+)\"",
        "group": 1
      },
      {
        "name": "bl",
        "required": false,
        "capability": "browser_bootstrap",
        "source": "html_regex",
        "pattern": "\"cfb2h\":\"([^\"]+)\"",
        "group": 1
      }
    ]
  },
  "parserModule": "./parser.ts",
  "requestTransformModule": "./request-transform.ts",
  "liveVerified": true
};

export interface GetFlightBookingDetailsInput {
  /** Origin airport IATA code for the outbound leg, e.g. SJC */
  origin: string;
  /** Destination airport IATA code for the outbound leg, e.g. SAN */
  destination: string;
  /** Outbound date in YYYY-MM-DD */
  departure_date: string;
  /** Return date in YYYY-MM-DD. Empty/omit for a one-way booking. */
  return_date?: string;
  /** Selected outbound flight as 'carrier number', e.g. 'WN 3489' */
  outbound_flight: string;
  /** Selected return flight as 'carrier number' (round trip only), e.g. 'WN 3540'. Empty for one-way. */
  return_flight?: string;
  /** Opaque per-itinerary booking token minted by the search_flights tool's flight_token output for the selected itinerary. */
  flight_token: string;
}

export async function getFlightBookingDetails(
  input: GetFlightBookingDetailsInput,
  opts: { credentials?: CredentialStore; fetchImpl?: typeof fetch; initialState?: Record<string, unknown> } = {},
): Promise<ToolResult> {
  const __dirname = dirname(fileURLToPath(import.meta.url));
  const params: Record<string, string | number | boolean> = {
    return_date: input.return_date ?? "",
    return_flight: input.return_flight ?? "",
    origin: input.origin,
    destination: input.destination,
    departure_date: input.departure_date,
    outbound_flight: input.outbound_flight,
    flight_token: input.flight_token,
  };
  return executeWorkflow({
    workflow: WORKFLOW,
    params,
    credentials: opts.credentials,
    fetchImpl: opts.fetchImpl,
    initialState: opts.initialState,
    workflowPath: join(__dirname, 'workflow.json'),
  });
}

export { WORKFLOW };
