/**
 * GENERATED by `imprint emit` — DO NOT EDIT BY HAND.
 *
 * Tool: search_southwest_flights
 * Site: southwest
 * Intent: Search for one-way Southwest Airlines flights between two airports on a given departure date, returning available flights and fares.
 *
 * To regenerate: imprint emit ~/.imprint/southwest/search_southwest_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_southwest_flights",
  "intent": {
    "description": "Search for one-way Southwest Airlines flights between two airports on a given departure date, returning available flights and fares.",
    "userSaid": "i just specified the departure and arrival airports (SJC and SAN) i set teh depart date now i changed to one way trip only now i clicked search now i clicked hte low fare calendar i clicked 26 june from the low fare calendar now i clicked continue to flight times on the low fare calendar, and now it's showing me the flight prices for 26 june"
  },
  "parameters": [
    {
      "name": "origin_airport_code",
      "type": "string",
      "description": "IATA code for the departure airport (e.g. SJC)",
      "default": "SJC"
    },
    {
      "name": "destination_airport_code",
      "type": "string",
      "description": "IATA code for the arrival airport (e.g. SAN)",
      "default": "SAN"
    },
    {
      "name": "departure_date",
      "type": "string",
      "description": "Departure date in YYYY-MM-DD format",
      "default": "2026-06-23"
    },
    {
      "name": "adult_passengers_count",
      "type": "number",
      "description": "Number of adult passengers",
      "default": 1
    },
    {
      "name": "fare_type",
      "type": "string",
      "description": "Fare currency type: USD for dollars, POINTS for Rapid Rewards points",
      "default": "USD"
    }
  ],
  "requests": [
    {
      "method": "GET",
      "url": "https://www.southwest.com/swa-ui/bootstrap/landing-home-page-v2/1/data.js",
      "headers": {
        "Accept": "application/javascript, text/javascript, */*; q=0.01"
      },
      "captures": [
        {
          "name": "southwest_api_key",
          "source": "text_regex",
          "pattern": "\"swa-bootstrap-landing-home-page-v2/api-keys\":\\[function\\(require,module,exports\\)\\{\\s*module\\.exports = \\{[^}]*\"prod\":\"([^\"]+)\"",
          "group": 1,
          "required": true,
          "capability": "ordinary_http"
        }
      ]
    },
    {
      "method": "POST",
      "url": "https://www.southwest.com/api/air-booking/v1/air-booking/page/air/booking/shopping",
      "headers": {
        "Content-Type": "application/json",
        "Accept": "application/json, text/javascript, */*; q=0.01",
        "X-API-Key": "${state.southwest_api_key}",
        "X-App-ID": "air-booking",
        "X-Channel-ID": "southwest"
      },
      "body": "{\"adultPassengersCount\":\"${param.adult_passengers_count}\",\"adultsCount\":\"${param.adult_passengers_count}\",\"departureDate\":\"${param.departure_date}\",\"departureTimeOfDay\":\"ALL_DAY\",\"destinationAirportCode\":\"${param.destination_airport_code}\",\"fareType\":\"${param.fare_type}\",\"int\":\"HOMEQBOMAIR\",\"originationAirportCode\":\"${param.origin_airport_code}\",\"passengerType\":\"ADULT\",\"promoCode\":\"\",\"returnDate\":\"\",\"returnTimeOfDay\":\"ALL_DAY\",\"tripType\":\"oneway\",\"application\":\"air-booking\",\"site\":\"southwest\"}"
    }
  ],
  "site": "southwest"
};

export interface SearchSouthwestFlightsInput {
  /** IATA code for the departure airport (e.g. SJC) */
  origin_airport_code?: string;
  /** IATA code for the arrival airport (e.g. SAN) */
  destination_airport_code?: string;
  /** Departure date in YYYY-MM-DD format */
  departure_date?: string;
  /** Number of adult passengers */
  adult_passengers_count?: number;
  /** Fare currency type: USD for dollars, POINTS for Rapid Rewards points */
  fare_type?: string;
}

export async function searchSouthwestFlights(
  input: SearchSouthwestFlightsInput,
  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> = {
    origin_airport_code: input.origin_airport_code ?? "SJC",
    destination_airport_code: input.destination_airport_code ?? "SAN",
    departure_date: input.departure_date ?? "2026-06-23",
    adult_passengers_count: input.adult_passengers_count ?? 1,
    fare_type: input.fare_type ?? "USD",

  };
  return executeWorkflow({
    workflow: WORKFLOW,
    params,
    credentials: opts.credentials,
    fetchImpl: opts.fetchImpl,
    initialState: opts.initialState,
    workflowPath: join(__dirname, 'workflow.json'),
  });
}

export { WORKFLOW };
