import SwiftUI

struct RecentlyAiredShowsRow: View {
    let shows: [TVShow]
    let airDates: [String: String]  // showName -> "YYYY-MM-DD"
    let onSeeAll: () -> Void
    let onSelectShow: (TVShow) -> Void
    @FocusState private var focusedItem: String?

    private var displayedShows: [TVShow] {
        Array(shows.prefix(Constants.TMDB.tvMaxDisplayCount))
    }

    /// Format subtitle showing current season and air date
    private func subtitle(for show: TVShow) -> String {
        var parts: [String] = []

        // Current season (highest season number)
        if let latestSeason = show.seasons.last {
            parts.append("Season \(latestSeason.number)")
        }

        // Format air date
        if let dateString = airDates[show.name] {
            let formatted = Self.formatAirDate(dateString)
            parts.append(formatted)
        }

        return parts.joined(separator: " • ")
    }

    /// Convert "YYYY-MM-DD" to "Jan 18, 2026" format
    private static func formatAirDate(_ dateString: String) -> String {
        let inputFormatter = DateFormatter()
        inputFormatter.dateFormat = "yyyy-MM-dd"

        let outputFormatter = DateFormatter()
        outputFormatter.dateFormat = "MMM d, yyyy"

        if let date = inputFormatter.date(from: dateString) {
            return outputFormatter.string(from: date)
        }
        return dateString
    }

    var body: some View {
        VStack(alignment: .leading, spacing: 20) {
            HStack {
                Text("Shows with Recently Aired Episodes")
                    .font(.title2)
                    .fontWeight(.bold)
                    .foregroundColor(.white)

                Spacer()

                if shows.count > Constants.TMDB.tvMaxDisplayCount {
                    Button {
                        onSeeAll()
                    } label: {
                        HStack(spacing: 8) {
                            Text("See All")
                            Image(systemName: "chevron.right")
                        }
                        .font(.callout)
                        .foregroundColor(.gray)
                    }
                    .buttonStyle(.plain)
                }
            }
            .padding(.horizontal, 80)

            ScrollView(.horizontal, showsIndicators: false) {
                HStack(spacing: 10) {
                    ForEach(displayedShows) { show in
                        Button {
                            onSelectShow(show)
                        } label: {
                            TVShowCardView(
                                tvShow: show,
                                isFocused: focusedItem == show.id,
                                customSubtitle: subtitle(for: show)
                            )
                        }
                        .buttonStyle(.plain)
                        .focused($focusedItem, equals: show.id)
                    }
                }
                .padding(.horizontal, 80)
            }
        }
    }
}
