import SwiftUI

struct TVShowCardView: View {
    let tvShow: TVShow
    let isFocused: Bool
    var customSubtitle: String? = nil

    /// Default subtitle showing season count
    private var subtitle: String {
        if let custom = customSubtitle {
            return custom
        }
        return "\(tvShow.seasons.count) season\(tvShow.seasons.count == 1 ? "" : "s")"
    }

    var body: some View {
        VStack(alignment: .leading, spacing: 12) {
            if let iconURL = tvShow.iconURL, let url = URL(string: iconURL) {
                AsyncImage(url: url) { phase in
                    switch phase {
                    case .empty:
                        ZStack {
                            Color.gray.opacity(0.3)
                            ProgressView()
                        }
                    case .success(let image):
                        image
                            .resizable()
                            .aspectRatio(contentMode: .fill)
                    case .failure:
                        ZStack {
                            Color.gray.opacity(0.3)
                            Image(systemName: "tv")
                                .font(.system(size: 50))
                                .foregroundColor(.white.opacity(0.5))
                        }
                    @unknown default:
                        EmptyView()
                    }
                }
                .frame(width: 400, height: 225)
                .clipShape(RoundedRectangle(cornerRadius: Constants.UI.vodCardCornerRadius))
                .overlay(
                    RoundedRectangle(cornerRadius: Constants.UI.vodCardCornerRadius)
                        .stroke(isFocused ? Color.white : Color.clear, lineWidth: Constants.UI.focusBorderWidth)
                )
            } else {
                ZStack {
                    Color.gray.opacity(0.3)
                    Image(systemName: "tv")
                        .font(.system(size: 50))
                        .foregroundColor(.white.opacity(0.5))
                }
                .frame(width: 400, height: 225)
                .clipShape(RoundedRectangle(cornerRadius: Constants.UI.vodCardCornerRadius))
                .overlay(
                    RoundedRectangle(cornerRadius: Constants.UI.vodCardCornerRadius)
                        .stroke(isFocused ? Color.white : Color.clear, lineWidth: Constants.UI.focusBorderWidth)
                )
            }

            Text(tvShow.name)
                .font(.title3)
                .foregroundColor(.white)
                .lineLimit(2)
                .frame(width: 400, alignment: .leading)

            Text(subtitle)
                .font(.caption)
                .foregroundColor(.gray)
        }
        .scaleEffect(isFocused ? Constants.UI.focusScale : 1.0)
        .shadow(color: isFocused ? Color.white.opacity(0.3) : Color.black.opacity(0.5), radius: isFocused ? Constants.UI.focusShadowRadius : 5)
        .animation(.easeInOut(duration: Constants.UI.standardAnimationDuration), value: isFocused)
        .padding(20)  // Add padding to prevent overlap and provide breathing room
    }
}
