File size: 9,188 Bytes
649703e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
77c968b
 
 
649703e
77c968b
649703e
 
 
 
 
 
 
 
 
 
 
77c968b
 
 
 
649703e
77c968b
 
649703e
 
 
 
 
 
 
 
 
 
77c968b
 
 
 
649703e
77c968b
649703e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
"""
orchestrator.py — Tripplanner search orchestrator.

Coordinates Delta, IHG, and Resy subagents sequentially over a single shared
BrowserSession, then merges their results into ranked TripResult objects.
"""

from __future__ import annotations

import os
import asyncio
from typing import Callable

from .models import (
    TripSearchRequest,
    FlightOption,
    HotelOption,
    RestaurantOption,
    TripResult,
    SearchProgress,
)
from .tools.browser import BrowserSession
from .agents.delta_agent import search_companion_cert
from .agents.ihg_agent import search_points_availability
from .agents.resy_agent import find_resy_restaurants


# ---------------------------------------------------------------------------
# Public helpers
# ---------------------------------------------------------------------------

def calculate_cash_out_of_pocket(
    flight: FlightOption,
    hotel: HotelOption | None,
) -> float:
    """
    Return the total cash the traveler pays.

    The companion's ticket is covered by the certificate; they only owe the
    companion_taxes.  Hotel is redeemed with points so costs $0 cash.
    Restaurant dining credit is separate from this calculation.
    """
    # Primary ticket + companion taxes (companion cert covers the fare itself)
    return flight.base_price + flight.companion_taxes


def collect_benefits(
    flight: FlightOption,
    hotel: HotelOption | None,
    restaurant: RestaurantOption | None,
) -> list[str]:
    """Return human-readable benefit strings for a trip combination."""
    benefits: list[str] = []

    if flight.eligible_for_companion_cert:
        benefits.append("Delta companion certificate used")

    if hotel is not None:
        if hotel.nights_free > 0:
            benefits.append(
                f"IHG 4th night free ({hotel.nights_free} night{'s' if hotel.nights_free != 1 else ''} free)"
            )
        else:
            benefits.append(f"IHG points redemption at {hotel.property_name}")

    if restaurant is not None:
        if restaurant.resy_credit_eligible:
            benefits.append("Resy $20 dining credit eligible")
        if restaurant.global_dining_access:
            benefits.append(f"Global Dining Access reservation at {restaurant.name}")

    return benefits


# ---------------------------------------------------------------------------
# Internal helpers
# ---------------------------------------------------------------------------

def _score_trip(
    flight: FlightOption,
    hotel: HotelOption | None,
    restaurant: RestaurantOption | None,
) -> float:
    """
    Score a trip combination on a 0–100 scale.

    Breakdown:
      +40  companion cert eligible
      +30  hotel found with points availability
      +15  restaurant found
      +15  value score (lower cash out of pocket relative to flight base price)
    """
    score = 0.0

    if flight.eligible_for_companion_cert:
        score += 40.0

    if hotel is not None:
        score += 30.0

    if restaurant is not None:
        score += 15.0

    # Value component: full +15 when cash_out == companion_taxes only (i.e.
    # traveler saved the most), scaling down as cash_out approaches base_price*2.
    cash_out = calculate_cash_out_of_pocket(flight, hotel)
    max_possible_cash = flight.base_price * 2  # both tickets at full price
    if max_possible_cash > 0:
        savings_ratio = 1.0 - (cash_out / max_possible_cash)
        # Clamp to [0, 1] so edge cases don't break the scale.
        savings_ratio = max(0.0, min(1.0, savings_ratio))
        score += savings_ratio * 15.0

    return round(score, 2)


def _extract_date_pairs(
    flights: list[FlightOption],
) -> list[tuple[str, str]]:
    """Return (check_in_iso, check_out_iso) pairs from flight results."""
    return [
        (flight.outbound_date.isoformat(), flight.return_date.isoformat())
        for flight in flights
    ]


def _extract_destinations(flights: list[FlightOption]) -> list[str]:
    return list(dict.fromkeys(flight.destination for flight in flights))


def _extract_cities_and_arrival_dates(
    flights: list[FlightOption],
) -> tuple[list[str], list[str]]:
    cities = list(dict.fromkeys(flight.destination for flight in flights))
    arrival_dates = list(
        dict.fromkeys(flight.outbound_date.isoformat() for flight in flights)
    )
    return cities, arrival_dates


def _find_matching_hotel(
    flight: FlightOption,
    hotels: list[HotelOption],
) -> HotelOption | None:
    """Return the first HotelOption whose destination and dates overlap the flight."""
    for hotel in hotels:
        if hotel.destination != flight.destination:
            continue
        # Dates overlap when check_in <= return_date AND check_out >= outbound_date
        if hotel.check_in <= flight.return_date and hotel.check_out >= flight.outbound_date:
            return hotel
    return None


def _find_matching_restaurant(
    flight: FlightOption,
    restaurants: list[RestaurantOption],
) -> RestaurantOption | None:
    """Return the first RestaurantOption matching the flight city and arrival date."""
    for restaurant in restaurants:
        if (
            restaurant.city == flight.destination
            and restaurant.reservation_date == flight.outbound_date
        ):
            return restaurant
    return None


def _emit(
    callback: Callable[[SearchProgress], None],
    step: str,
    message: str,
    progress: int,
) -> None:
    callback(SearchProgress(step=step, message=message, progress=progress))


# ---------------------------------------------------------------------------
# Main orchestrator
# ---------------------------------------------------------------------------

async def run_trip_search(
    request: TripSearchRequest,
    progress_callback: Callable[[SearchProgress], None],
) -> list[TripResult]:
    """
    Coordinate Delta, IHG, and Resy subagents and return ranked TripResult list.

    All three agents share a single BrowserSession so that login state is
    preserved across site transitions.
    """
    user_data_dir = os.environ.get("BROWSER_USER_DATA_DIR", "./browser_data")

    async with BrowserSession(user_data_dir=user_data_dir) as browser:

        # --- Step 1: Delta companion certificate search ---
        _emit(
            progress_callback,
            step="delta",
            message="Checking companion certificate availability...",
            progress=10,
        )
        def delta_progress(msg: str) -> None:
            _emit(progress_callback, step="delta", message=msg, progress=20)

        flights: list[FlightOption] = await search_companion_cert(
            request, browser, delta_progress
        )

        # --- Step 2: IHG points availability ---
        _emit(
            progress_callback,
            step="ihg",
            message="Checking IHG points availability...",
            progress=40,
        )
        destinations = _extract_destinations(flights)
        date_pairs = _extract_date_pairs(flights)

        def ihg_progress(msg: str) -> None:
            _emit(progress_callback, step="ihg", message=msg, progress=55)

        hotels: list[HotelOption] = await search_points_availability(
            destinations, date_pairs, browser,
            request.ihg_brands, request.trip_duration_nights, ihg_progress
        )

        # --- Step 3: Resy dining options ---
        _emit(
            progress_callback,
            step="resy",
            message="Finding Resy dining options...",
            progress=70,
        )
        cities, arrival_dates = _extract_cities_and_arrival_dates(flights)

        def resy_progress(msg: str) -> None:
            _emit(progress_callback, step="resy", message=msg, progress=80)

        restaurants: list[RestaurantOption] = await find_resy_restaurants(
            cities, arrival_dates, request.party_size, browser, resy_progress
        )

    # BrowserSession closed — merge results in memory.

    # --- Step 4: Merge and rank ---
    _emit(
        progress_callback,
        step="merging",
        message="Ranking trip combinations...",
        progress=90,
    )

    results: list[TripResult] = []
    for flight in flights:
        hotel = _find_matching_hotel(flight, hotels)
        restaurant = _find_matching_restaurant(flight, restaurants)

        cash_out = calculate_cash_out_of_pocket(flight, hotel)
        points_required = hotel.total_points if hotel is not None else 0
        benefits = collect_benefits(flight, hotel, restaurant)
        score = _score_trip(flight, hotel, restaurant)

        results.append(
            TripResult(
                destination=flight.destination,
                flight=flight,
                hotel=hotel,
                restaurant=restaurant,
                total_cash_out_of_pocket=cash_out,
                total_points_required=points_required,
                benefits_captured=benefits,
                score=score,
            )
        )

    results.sort(key=lambda r: r.score, reverse=True)

    # --- Step 5: Done ---
    _emit(
        progress_callback,
        step="done",
        message="Search complete!",
        progress=100,
    )

    return results