import SwiftUI

struct CategoryDetailView: View {
    let category: Category
    let movies: [VODItem]
    @EnvironmentObject var appState: AppState
    @State private var selectedVODItem: VODItem? = nil
    @FocusState private var focusedItem: String?

    let columns = Array(repeating: GridItem(.flexible(), spacing: Constants.UI.vodGridSpacing), count: Constants.UI.vodGridColumns)

    /// All movies in playlist for finding versions
    private var allPlaylistMovies: [VODItem] {
        appState.playlist?.vodItems.filter { $0.isMovie } ?? []
    }

    /// Get all versions for a movie
    private func allVersions(for item: VODItem) -> [VODItem] {
        let groups = allPlaylistMovies.groupedByTitle()
        for group in groups {
            if group.versions.contains(where: { $0.id == item.id }) {
                return group.versions
            }
        }
        return [item]
    }

    var body: some View {
        ZStack {
            Color.black.ignoresSafeArea()

            ScrollView {
                VStack(spacing: 40) {
                    headerView

                    if movies.isEmpty {
                        emptyStateView
                    } else {
                        LazyVGrid(columns: columns, spacing: Constants.UI.vodGridSpacing) {
                            ForEach(movies) { item in
                                Button {
                                    selectedVODItem = item
                                } label: {
                                    VODCardView(
                                        vodItem: item,
                                        isFocused: focusedItem == item.id,
                                        releaseDate: appState.tmdbReleaseDates[item.id]
                                    )
                                }
                                .buttonStyle(.plain)
                                .focused($focusedItem, equals: item.id)
                            }
                        }
                        .padding(.horizontal, 80)
                        .padding(.bottom, 60)
                    }
                }
            }
        }
        .navigationDestination(item: $selectedVODItem) { item in
            VODDetailView(vodItem: item, allVersions: allVersions(for: item))
                .environmentObject(appState)
        }
    }

    private var headerView: some View {
        VStack(spacing: 25) {
            HStack {
                Text(category.name)
                    .font(.system(size: 60, weight: .bold))
                    .foregroundColor(.white)

                Spacer()

                Text("\(movies.count) movies")
                    .font(.title3)
                    .foregroundColor(.gray)
            }
            .padding(.horizontal, 80)
            .padding(.top, 60)
        }
    }

    private var emptyStateView: some View {
        VStack(spacing: 30) {
            Image(systemName: "film")
                .font(.system(size: 100))
                .foregroundColor(.gray)

            Text("No Movies in This Category")
                .font(.title)
                .foregroundColor(.white)

            Text("Check back later for new content")
                .font(.title3)
                .foregroundColor(.gray)
        }
        .frame(maxWidth: .infinity, maxHeight: .infinity)
        .padding(.top, 100)
    }
}
