import SwiftUI

struct VODCardView: View {
    let vodItem: VODItem
    let isFocused: Bool
    var releaseDate: String?  // Optional TMDB release date in "YYYY-MM-DD" format

    var body: some View {
        VStack(alignment: .leading, spacing: 15) {
            ZStack {
                if let iconURL = vodItem.iconURLObject {
                    CachedAsyncImage(
                        url: iconURL,
                        content: { image in
                            image
                                .resizable()
                                .aspectRatio(contentMode: .fit)
                        },
                        placeholder: {
                            placeholderView
                        }
                    )
                } else {
                    placeholderView
                }
            }
            .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(vodItem.name)
                .font(.title3)
                .fontWeight(.semibold)
                .foregroundColor(.white)
                .lineLimit(2)
                .frame(width: 400, alignment: .leading)
                .multilineTextAlignment(.leading)

            // Show release date if available, otherwise show category
            if let date = releaseDate {
                Text(formattedReleaseDate(date))
                    .font(.caption)
                    .foregroundColor(.gray)
            } else if let category = vodItem.categoryID {
                Text(category)
                    .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
    }

    /// Format "YYYY-MM-DD" to a readable date like "January 15, 2025"
    private func formattedReleaseDate(_ dateString: String) -> String {
        let inputFormatter = DateFormatter()
        inputFormatter.dateFormat = "yyyy-MM-dd"

        let outputFormatter = DateFormatter()
        outputFormatter.dateStyle = .long
        outputFormatter.timeStyle = .none

        if let date = inputFormatter.date(from: dateString) {
            return outputFormatter.string(from: date)
        }
        return dateString  // Fallback to original string if parsing fails
    }

    private var placeholderView: some View {
        ZStack {
            Color.gray.opacity(0.3)
            Image(systemName: "film")
                .font(.system(size: 60))
                .foregroundColor(.white.opacity(0.5))
        }
    }
}
