import SwiftUI

struct FlightScheduleRow: View {
    let schedule: FlightSchedule
    let departureCode: String
    let arrivalCode: String

    var body: some View {
        VStack(alignment: .leading, spacing: 14) {
            // MARK: - Top row: airline logo + name + flight number
            HStack(spacing: 10) {
                AsyncImage(url: schedule.airline.logoURL) { phase in
                    switch phase {
                    case .success(let image):
                        image
                            .resizable()
                            .scaledToFit()
                    default:
                        RoundedRectangle(cornerRadius: 10)
                            .fill(FlightTheme.accent)
                            .overlay {
                                Text(String(schedule.airline.name.prefix(1)))
                                    .font(.system(size: 18, weight: .bold, design: .rounded))
                                    .foregroundStyle(.white)
                            }
                    }
                }
                .frame(width: 40, height: 40)
                .clipShape(RoundedRectangle(cornerRadius: 10))

                Text(schedule.airline.name)
                    .font(.subheadline.weight(.semibold))
                    .foregroundStyle(FlightTheme.textPrimary)
                    .lineLimit(1)

                Spacer()

                Text(schedule.flightNumber)
                    .font(FlightTheme.flightNumber())
                    .foregroundStyle(FlightTheme.textSecondary)
            }

            // MARK: - Route visualization (curved arc with codes + times)
            RouteVisualization(
                departureCode: departureCode,
                arrivalCode: arrivalCode,
                departureTime: schedule.departureTime,
                arrivalTime: schedule.arrivalTime,
                codeSize: 28,
                timeSize: 18
            )

            // MARK: - Aircraft pill tag + tap hint
            HStack {
                if !schedule.aircraft.isEmpty {
                    Text(schedule.aircraft)
                        .font(FlightTheme.label(11))
                        .foregroundStyle(FlightTheme.textSecondary)
                        .padding(.horizontal, 10)
                        .padding(.vertical, 4)
                        .background(Color(.quaternarySystemFill), in: Capsule())
                }

                Spacer()

                Image(systemName: "chevron.right")
                    .font(.caption)
                    .foregroundStyle(FlightTheme.textTertiary)
            }
        }
        .flightCard()
    }
}
