// // ScenarioAPlanner.swift // SportsTime // // Scenario A: Date range only planning. // User provides a date range, we find all games and build chronological routes. // import Foundation import CoreLocation /// Scenario A: Date range planning /// /// This is the simplest scenario - user just picks a date range and we find games. /// /// Input: /// - date_range: Required. The trip dates (e.g., Jan 5-15) /// - must_stop: Optional. A location they must visit (not yet implemented) /// /// Output: /// - Success: Ranked list of itinerary options /// - Failure: Explicit error with reason (no games, dates invalid, etc.) /// /// Example: /// User selects Jan 5-10, 2026 /// We find: Lakers (Jan 5), Warriors (Jan 7), Kings (Jan 9) /// Output: Single itinerary visiting LA → SF → Sacramento in order /// final class ScenarioAPlanner: ScenarioPlanner { // MARK: - ScenarioPlanner Protocol /// Main entry point for Scenario A planning. /// /// Flow: /// 1. Validate inputs (date range must exist) /// 2. Find all games within the date range /// 3. Convert games to stops (grouping by stadium) /// 4. Calculate travel between stops /// 5. Return the complete itinerary /// /// Failure cases: /// - No date range provided → .missingDateRange /// - No games in date range → .noGamesInRange /// - Can't build valid route → .constraintsUnsatisfiable /// func plan(request: PlanningRequest) -> ItineraryResult { // ────────────────────────────────────────────────────────────────── // Step 1: Validate date range exists // ────────────────────────────────────────────────────────────────── // Scenario A requires a date range. Without it, we can't filter games. guard let dateRange = request.dateRange else { return .failure( PlanningFailure( reason: .missingDateRange, violations: [] ) ) } // ────────────────────────────────────────────────────────────────── // Step 2: Filter games within date range // ────────────────────────────────────────────────────────────────── // Get all games that fall within the user's travel dates. // Sort by start time so we visit them in chronological order. let gamesInRange = request.allGames .filter { dateRange.contains($0.startTime) } .sorted { $0.startTime < $1.startTime } // No games? Nothing to plan. if gamesInRange.isEmpty { return .failure( PlanningFailure( reason: .noGamesInRange, violations: [] ) ) } // ────────────────────────────────────────────────────────────────── // Step 3: Find ALL geographically sensible route variations // ────────────────────────────────────────────────────────────────── // Not all games in the date range may form a sensible route. // Example: NY (Jan 5), TX (Jan 6), SC (Jan 7), CA (Jan 8) // - Including all = zig-zag nightmare // - Option 1: NY, TX, DEN, NM, CA (skip SC) // - Option 2: NY, SC, DEN, NM, CA (skip TX) // - etc. // // We explore ALL valid combinations and return multiple options. // Uses GameDAGRouter for polynomial-time beam search. // let validRoutes = GameDAGRouter.findAllSensibleRoutes( from: gamesInRange, stadiums: request.stadiums, stopBuilder: buildStops ) if validRoutes.isEmpty { return .failure( PlanningFailure( reason: .noValidRoutes, violations: [ ConstraintViolation( type: .geographicSanity, description: "No geographically sensible route found for games in this date range", severity: .error ) ] ) ) } // ────────────────────────────────────────────────────────────────── // Step 4: Build itineraries for each valid route // ────────────────────────────────────────────────────────────────── // For each valid game combination, build stops and calculate travel. // Some routes may fail driving constraints - filter those out. // var itineraryOptions: [ItineraryOption] = [] var routesAttempted = 0 var routesFailed = 0 for (index, routeGames) in validRoutes.enumerated() { routesAttempted += 1 // Build stops for this route let stops = buildStops(from: routeGames, stadiums: request.stadiums) guard !stops.isEmpty else { routesFailed += 1 continue } // Calculate travel segments using shared ItineraryBuilder guard let itinerary = ItineraryBuilder.build( stops: stops, constraints: request.drivingConstraints ) else { // This route fails driving constraints, skip it routesFailed += 1 continue } // Create the option let cities = stops.map { $0.city }.joined(separator: " → ") let option = ItineraryOption( rank: index + 1, stops: itinerary.stops, travelSegments: itinerary.travelSegments, totalDrivingHours: itinerary.totalDrivingHours, totalDistanceMiles: itinerary.totalDistanceMiles, geographicRationale: "\(stops.count) games: \(cities)" ) itineraryOptions.append(option) } // ────────────────────────────────────────────────────────────────── // Step 5: Return ranked results // ────────────────────────────────────────────────────────────────── // If no routes passed all constraints, fail. // Otherwise, return all valid options for the user to choose from. // if itineraryOptions.isEmpty { return .failure( PlanningFailure( reason: .constraintsUnsatisfiable, violations: [ ConstraintViolation( type: .drivingTime, description: "No routes satisfy driving constraints", severity: .error ) ] ) ) } // Sort and rank based on leisure level let leisureLevel = request.preferences.leisureLevel let rankedOptions = ItineraryOption.sortByLeisure( itineraryOptions, leisureLevel: leisureLevel, limit: request.preferences.maxTripOptions ) return .success(rankedOptions) } // MARK: - Stop Building /// Converts a list of games into itinerary stops. /// /// The goal: Create one stop per stadium, in the order we first encounter each stadium /// when walking through games chronologically. /// /// Example: /// Input games (already sorted by date): /// 1. Jan 5 - Lakers @ Staples Center (LA) /// 2. Jan 6 - Clippers @ Staples Center (LA) <- same stadium as #1 /// 3. Jan 8 - Warriors @ Chase Center (SF) /// /// Output stops: /// Stop 1: Los Angeles (contains game 1 and 2) /// Stop 2: San Francisco (contains game 3) /// /// Note: If you visit the same city, leave, and come back, that creates /// separate stops (one for each visit). /// private func buildStops( from games: [Game], stadiums: [UUID: Stadium] ) -> [ItineraryStop] { guard !games.isEmpty else { return [] } // Sort games chronologically let sortedGames = games.sorted { $0.startTime < $1.startTime } // Group consecutive games at the same stadium into stops // If you visit A, then B, then A again, that's 3 stops (A, B, A) var stops: [ItineraryStop] = [] var currentStadiumId: UUID? = nil var currentGames: [Game] = [] for game in sortedGames { if game.stadiumId == currentStadiumId { // Same stadium as previous game - add to current group currentGames.append(game) } else { // Different stadium - finalize previous stop (if any) and start new one if let stadiumId = currentStadiumId, !currentGames.isEmpty { if let stop = createStop(from: currentGames, stadiumId: stadiumId, stadiums: stadiums) { stops.append(stop) } } currentStadiumId = game.stadiumId currentGames = [game] } } // Don't forget the last group if let stadiumId = currentStadiumId, !currentGames.isEmpty { if let stop = createStop(from: currentGames, stadiumId: stadiumId, stadiums: stadiums) { stops.append(stop) } } return stops } /// Creates an ItineraryStop from a group of games at the same stadium. private func createStop( from games: [Game], stadiumId: UUID, stadiums: [UUID: Stadium] ) -> ItineraryStop? { guard !games.isEmpty else { return nil } let sortedGames = games.sorted { $0.startTime < $1.startTime } let stadium = stadiums[stadiumId] let city = stadium?.city ?? "Unknown" let state = stadium?.state ?? "" let coordinate = stadium?.coordinate let location = LocationInput( name: city, coordinate: coordinate, address: stadium?.fullAddress ) // departureDate is day AFTER last game (we leave the next morning) let lastGameDate = sortedGames.last?.gameDate ?? Date() let departureDateValue = Calendar.current.date(byAdding: .day, value: 1, to: lastGameDate) ?? lastGameDate return ItineraryStop( city: city, state: state, coordinate: coordinate, games: sortedGames.map { $0.id }, arrivalDate: sortedGames.first?.gameDate ?? Date(), departureDate: departureDateValue, location: location, firstGameStart: sortedGames.first?.startTime ) } }