//
// MainView.swift
// IceGlass-iOS
//
// Copyright 2026 Rouslan Zenetl. All Rights Reserved.
//

import SwiftUI

struct MainView: View {
    @Environment(ScoreboardViewModel.self) private var viewModel
    @State private var showingSettings = false

    var body: some View {
        NavigationStack {
            ScrollView {
                LazyVStack(alignment: .leading, spacing: 16, pinnedViews: []) {
                    UpdatedHeader(
                        lastUpdated: viewModel.lastUpdated,
                        isRefreshing: viewModel.isRefreshing
                    )
                    .padding(.horizontal)

                    let roundGroups = viewModel.roundSeriesGroups
                    let latestRound = roundGroups.last?.round
                    ForEach(roundGroups, id: \.round) { group in
                        if !group.items.isEmpty {
                            PlayoffRoundSection(
                                round: group.round,
                                items: group.items,
                                showStatus: group.round == latestRound
                            )
                            .padding(.horizontal)
                        }
                    }

                    let gameDays = viewModel.gamesByDate
                    if gameDays.isEmpty && roundGroups.isEmpty {
                        emptyState
                            .padding(.horizontal)
                            .padding(.top, 40)
                    } else {
                        ForEach(gameDays, id: \.date) { gameDay in
                            GameDaySection(gameDay: gameDay)
                                .padding(.horizontal)
                        }
                    }
                }
                .padding(.vertical)
            }
            .refreshable {
                await viewModel.refreshNow()
            }
            .navigationTitle("IceGlass")
            .navigationBarTitleDisplayMode(.inline)
            .toolbar {
                ToolbarItem(placement: .topBarTrailing) {
                    Button {
                        showingSettings = true
                    } label: {
                        Image(systemName: "gearshape")
                    }
                    .accessibilityLabel("Settings")
                }
            }
            .sheet(isPresented: $showingSettings) {
                SettingsSheet()
            }
        }
    }

    private var emptyState: some View {
        VStack(spacing: 8) {
            Image(systemName: "hockey.puck")
                .font(.system(size: 40))
                .foregroundStyle(.tertiary)
            Text("No games scheduled")
                .font(.headline)
                .foregroundStyle(.secondary)
            Text("Pull down to refresh")
                .font(.subheadline)
                .foregroundStyle(.tertiary)
        }
        .frame(maxWidth: .infinity)
    }
}
