using ShiftScheduler.Shared.Models; using System.Text.Json; namespace ShiftScheduler.Services { public class TransportService { private readonly HttpClient _httpClient; private readonly TransportConfiguration _config; public TransportService(HttpClient httpClient, TransportConfiguration config) { _httpClient = httpClient; _config = config; } public async Task GetConnectionAsync(DateTime shiftStartTime, string? customEndStation = null) { try { var endStation = customEndStation ?? _config.EndStation; // Calculate the latest acceptable arrival time (shift start time - safety buffer) var latestArrivalTime = shiftStartTime.AddMinutes(-_config.SafetyBufferMinutes); // Search for connections starting from a reasonable time before the latest acceptable arrival // Start searching from 90 minutes before the latest acceptable time var searchTime = latestArrivalTime.AddMinutes(-90); var searchTimeStr = searchTime.ToString("yyyy-MM-dd"); var searchHourStr = searchTime.ToString("HH:mm"); var url = $"{_config.ApiBaseUrl}/connections?from={Uri.EscapeDataString(_config.StartStation)}&to={Uri.EscapeDataString(endStation)}&date={searchTimeStr}&time={searchHourStr}&limit=15"; var response = await _httpClient.GetStringAsync(url); var apiResponse = JsonSerializer.Deserialize(response, new JsonSerializerOptions { PropertyNameCaseInsensitive = true }); if (apiResponse?.Connections?.Count > 0) { // Find the best connection that arrives before the required time var bestConnection = FindBestConnection(apiResponse.Connections, latestArrivalTime); return MapToTransportConnection(bestConnection); } return null; } catch (Exception ex) { // Log the exception and return a mock connection for demonstration Console.WriteLine($"Error fetching transport connection: {ex.Message}"); return CreateMockConnection(shiftStartTime); } } private TransportConnection CreateMockConnection(DateTime shiftStartTime) { // Create a realistic mock connection that arrives before shift start with safety buffer var latestArrivalTime = shiftStartTime.AddMinutes(-_config.SafetyBufferMinutes); var arrivalTime = latestArrivalTime.AddMinutes(-5); // Arrive 5 minutes before the latest acceptable time var departureTime = arrivalTime.AddMinutes(-45); // 45 minutes journey time return new TransportConnection { DepartureTime = departureTime.ToString("HH:mm"), ArrivalTime = arrivalTime.ToString("HH:mm"), Duration = "00:45:00", Platform = "3", Sections = new List { new TransportSection { Journey = new TransportJourney { Name = "S1", Category = "S", Number = "1" }, Departure = new TransportCheckpoint { Station = new TransportStation { Name = _config.StartStation, Id = "start" }, Departure = departureTime.ToString("HH:mm"), Platform = "3" }, Arrival = new TransportCheckpoint { Station = new TransportStation { Name = _config.EndStation, Id = "end" }, Arrival = arrivalTime.ToString("HH:mm"), Platform = "1" } } } }; } private TransportApiConnection? FindBestConnection(List connections, DateTime latestArrivalTime) { // Filter connections that arrive before the latest acceptable time var validConnections = new List(); foreach (var connection in connections) { if (DateTime.TryParse(connection.To?.Arrival, out var arrivalTime)) { // Compare arrival time with latest acceptable time if (arrivalTime <= latestArrivalTime) { validConnections.Add(connection); } } } if (validConnections.Count > 0) { // Sort by arrival time (earliest first) and take the one that arrives // with reasonable margin but not too early var sortedValid = validConnections .OrderBy(c => DateTime.Parse(c.To?.Arrival ?? "00:00")) .ToList(); // Prefer a connection that arrives with 10-45 minutes margin var preferredMarginMin = 10; var preferredMarginMax = 45; var idealConnection = sortedValid.LastOrDefault(c => { var arrivalTime = DateTime.Parse(c.To?.Arrival ?? "00:00"); var marginMinutes = (latestArrivalTime - arrivalTime).TotalMinutes; return marginMinutes >= preferredMarginMin && marginMinutes <= preferredMarginMax; }); // If we found an ideal connection, use it; otherwise use the latest valid one return idealConnection ?? sortedValid.Last(); } // If no valid connections, return the earliest available return connections .OrderBy(c => DateTime.Parse(c.To?.Arrival ?? "23:59")) .FirstOrDefault(); } private TransportConnection MapToTransportConnection(TransportApiConnection? apiConnection) { if (apiConnection == null) return new TransportConnection(); return new TransportConnection { DepartureTime = apiConnection.From?.Departure ?? string.Empty, ArrivalTime = apiConnection.To?.Arrival ?? string.Empty, Duration = apiConnection.Duration ?? string.Empty, Platform = apiConnection.From?.Platform ?? string.Empty, Sections = apiConnection.Sections?.Select(s => new TransportSection { Journey = s.Journey != null ? new TransportJourney { Name = s.Journey.Name ?? string.Empty, Category = s.Journey.Category ?? string.Empty, Number = s.Journey.Number ?? string.Empty } : null, Departure = s.Departure != null ? new TransportCheckpoint { Station = s.Departure.Station != null ? new TransportStation { Name = s.Departure.Station.Name ?? string.Empty, Id = s.Departure.Station.Id ?? string.Empty } : null, Departure = s.Departure.Departure ?? string.Empty, Arrival = s.Departure.Arrival ?? string.Empty, Platform = s.Departure.Platform ?? string.Empty } : null, Arrival = s.Arrival != null ? new TransportCheckpoint { Station = s.Arrival.Station != null ? new TransportStation { Name = s.Arrival.Station.Name ?? string.Empty, Id = s.Arrival.Station.Id ?? string.Empty } : null, Departure = s.Arrival.Departure ?? string.Empty, Arrival = s.Arrival.Arrival ?? string.Empty, Platform = s.Arrival.Platform ?? string.Empty } : null }).ToList() ?? new List() }; } public string FormatConnectionSummary(TransportConnection? connection) { if (connection == null || string.IsNullOrEmpty(connection.DepartureTime)) return "No transport info"; var departure = DateTime.TryParse(connection.DepartureTime, out var dep) ? dep.ToString("HH:mm") : connection.DepartureTime; var arrival = DateTime.TryParse(connection.ArrivalTime, out var arr) ? arr.ToString("HH:mm") : connection.ArrivalTime; var mainJourney = connection.Sections?.FirstOrDefault()?.Journey; var trainInfo = mainJourney != null ? $"{mainJourney.Category} {mainJourney.Number}" : "Train"; return $"{trainInfo}: {departure} → {arrival}"; } } }