/**
 * GENERATED by `imprint emit` — DO NOT EDIT BY HAND.
 *
 * Tool: search_flights
 * Site: google-flights
 * Intent: Search Google Flights for itineraries between two airports with dates, trip type, and filters (stops, airlines, price, times, duration, bags).
 *
 * To regenerate: imprint emit ~/.imprint/google-flights/search_flights/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": "search_flights",
  "intent": {
    "description": "Search Google Flights for itineraries between two airports with dates, trip type, and filters (stops, airlines, price, times, duration, bags).",
    "userSaid": "searched for a round trip flight; searched for a one way flight; played with the stops and the airlines filters; added bags; played with the price and time filters; played with the duration filter; did a multi city flight query"
  },
  "parameters": [
    {
      "name": "origin",
      "type": "string",
      "description": "Origin airport/city IATA code, e.g. SJC",
      "verified": false,
      "verifyNote": "annotated"
    },
    {
      "name": "destination",
      "type": "string",
      "description": "Destination airport/city IATA code, 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 (omit/empty for one way)",
      "default": "",
      "verified": false,
      "verifyNote": "annotated"
    },
    {
      "name": "trip_type",
      "type": "string",
      "description": "round_trip, one_way, or multi_city",
      "default": "round_trip",
      "verified": false,
      "verifyNote": "annotated"
    },
    {
      "name": "max_stops",
      "type": "number",
      "description": "Max stops: 0=nonstop, 1=<=1 stop, 2=<=2 stops, 3=any",
      "default": 3,
      "verified": false,
      "verifyNote": "annotated"
    },
    {
      "name": "airlines",
      "type": "string",
      "description": "Comma-separated alliance (ONEWORLD/SKYTEAM/STAR_ALLIANCE) or 2-letter carrier codes (e.g. AS,WN). Empty = no filter",
      "default": "",
      "verified": false,
      "verifyNote": "annotated"
    },
    {
      "name": "max_price",
      "type": "number",
      "description": "Maximum total price in USD (0 = no filter)",
      "default": 0,
      "verified": false,
      "verifyNote": "annotated"
    },
    {
      "name": "outbound_times",
      "type": "string",
      "description": "Outbound departure window in hours, e.g. '6-23' (empty = no filter)",
      "default": "",
      "verified": false,
      "verifyNote": "annotated"
    },
    {
      "name": "return_times",
      "type": "string",
      "description": "Return departure window in hours, e.g. '6-23' (empty = no filter)",
      "default": "",
      "verified": false,
      "verifyNote": "annotated"
    },
    {
      "name": "max_duration",
      "type": "number",
      "description": "Maximum total trip duration in minutes, e.g. 540 (0 = no filter)",
      "default": 0,
      "verified": false,
      "verifyNote": "annotated"
    },
    {
      "name": "carry_on_bags",
      "type": "number",
      "description": "Number of carry-on bags to filter/price by (0 = no filter)",
      "default": 0,
      "verified": false,
      "verifyNote": "annotated"
    }
  ],
  "requests": [
    {
      "method": "POST",
      "url": "https://www.google.com/_/FlightsFrontendUi/data/travel.frontend.flights.FlightsFrontendService/GetShoppingResults?f.sid=${state.f_sid}&bl=${state.bl}&hl=en-US&soc-app=162&soc-platform=1&soc-device=1&_reqid=1708023&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.trip_type}|${param.max_stops}|${param.airlines}|${param.max_price}|${param.outbound_times}|${param.return_times}|${param.max_duration}|${param.carry_on_bags}&",
      "effect": "safe"
    }
  ],
  "site": "google-flights",
  "bootstrap": {
    "url": "https://www.google.com/travel/flights",
    "waitUntil": "domcontentloaded",
    "timeoutMs": 30000,
    "captures": [
      {
        "name": "f_sid",
        "required": true,
        "capability": "browser_bootstrap",
        "source": "html_regex",
        "pattern": "\"FdrFJe\":\"([^\"]+)\"",
        "group": 1
      },
      {
        "name": "bl",
        "required": true,
        "capability": "browser_bootstrap",
        "source": "html_regex",
        "pattern": "\"cfb2h\":\"([^\"]+)\"",
        "group": 1
      }
    ]
  },
  "parserModule": "./parser.ts",
  "requestTransformModule": "./request-transform.ts",
  "liveVerified": true
};

export interface SearchFlightsInput {
  /** Origin airport/city IATA code, e.g. SJC */
  origin: string;
  /** Destination airport/city IATA code, e.g. SAN */
  destination: string;
  /** Outbound date in YYYY-MM-DD */
  departure_date: string;
  /** Return date in YYYY-MM-DD (omit/empty for one way) */
  return_date?: string;
  /** round_trip, one_way, or multi_city */
  trip_type?: string;
  /** Max stops: 0=nonstop, 1=<=1 stop, 2=<=2 stops, 3=any */
  max_stops?: number;
  /** Comma-separated alliance (ONEWORLD/SKYTEAM/STAR_ALLIANCE) or 2-letter carrier codes (e.g. AS,WN). Empty = no filter */
  airlines?: string;
  /** Maximum total price in USD (0 = no filter) */
  max_price?: number;
  /** Outbound departure window in hours, e.g. '6-23' (empty = no filter) */
  outbound_times?: string;
  /** Return departure window in hours, e.g. '6-23' (empty = no filter) */
  return_times?: string;
  /** Maximum total trip duration in minutes, e.g. 540 (0 = no filter) */
  max_duration?: number;
  /** Number of carry-on bags to filter/price by (0 = no filter) */
  carry_on_bags?: number;
}

export async function searchFlights(
  input: SearchFlightsInput,
  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 ?? "",
    trip_type: input.trip_type ?? "round_trip",
    max_stops: input.max_stops ?? 3,
    airlines: input.airlines ?? "",
    max_price: input.max_price ?? 0,
    outbound_times: input.outbound_times ?? "",
    return_times: input.return_times ?? "",
    max_duration: input.max_duration ?? 0,
    carry_on_bags: input.carry_on_bags ?? 0,
    origin: input.origin,
    destination: input.destination,
    departure_date: input.departure_date,
  };
  return executeWorkflow({
    workflow: WORKFLOW,
    params,
    credentials: opts.credentials,
    fetchImpl: opts.fetchImpl,
    initialState: opts.initialState,
    workflowPath: join(__dirname, 'workflow.json'),
  });
}

export { WORKFLOW };
