import SwiftUI
import SwiftData

struct StatisticsView: View {
    @Query private var records: [StatsRecord]
    @Environment(\.dismiss) private var dismiss

    var body: some View {
        NavigationStack {
            List {
                if records.isEmpty {
                    ContentUnavailableView("No Statistics Yet",
                                            systemImage: "chart.bar",
                                            description: Text("Play some games to see your stats here."))
                } else {
                    ForEach(GameVariant.allCases) { variant in
                        let variantRecords = records.filter { $0.variant == variant.rawValue }
                        if !variantRecords.isEmpty {
                            Section(variant.displayName) {
                                ForEach(variantRecords, id: \.variant) { record in
                                    statsRow(record)
                                }
                            }
                        }
                    }
                }
            }
            .navigationTitle("Statistics")
            #if os(iOS)
            .navigationBarTitleDisplayMode(.inline)
            #endif
            .toolbar {
                ToolbarItem(placement: .confirmationAction) {
                    Button("Done") { dismiss() }
                }
            }
        }
    }

    private func statsRow(_ record: StatsRecord) -> some View {
        VStack(alignment: .leading, spacing: 4) {
            HStack {
                Text(record.difficulty.capitalized)
                    .font(.subheadline.bold())
                Spacer()
                Text("\(record.gamesWon)/\(record.gamesPlayed) wins")
                    .font(.caption)
                    .foregroundStyle(.secondary)
            }
            HStack {
                if record.gamesPlayed > 0 {
                    Text("Win rate: \(record.winRate * 100, specifier: "%.0f")%")
                    Spacer()
                    if record.bestStreak > 0 {
                        Text("Best streak: \(record.bestStreak)")
                    }
                }
            }
            .font(.caption)
            .foregroundStyle(.secondary)
        }
    }
}
