//
//  LoadingSheet.swift
//  SportsTime
//
//  Full-screen blocking loading overlay with centered card.
//

import SwiftUI

struct LoadingSheet: View {
    static let backgroundOpacity: Double = 0.5

    let label: String
    let detail: String?

    @Environment(\.colorScheme) private var colorScheme

    init(label: String, detail: String? = nil) {
        self.label = label
        self.detail = detail
    }

    var body: some View {
        ZStack {
            // Dimmed background
            Color.black.opacity(Self.backgroundOpacity)
                .ignoresSafeArea()

            // Centered card
            VStack(spacing: Theme.Spacing.lg) {
                LoadingSpinner(size: .large)

                VStack(spacing: Theme.Spacing.xs) {
                    Text(label)
                        .font(.headline)
                        .foregroundStyle(Theme.textPrimary(colorScheme))

                    if let detail {
                        Text(detail)
                            .font(.subheadline)
                            .foregroundStyle(Theme.textSecondary(colorScheme))
                            .multilineTextAlignment(.center)
                    }
                }
            }
            .padding(Theme.Spacing.xl)
            .background(Theme.cardBackground(colorScheme))
            .clipShape(RoundedRectangle(cornerRadius: Theme.CornerRadius.large))
            .shadow(color: .black.opacity(0.2), radius: 16, y: 8)
        }
        .transition(.opacity)
    }
}

// MARK: - Preview

#Preview("Loading Sheet") {
    ZStack {
        Color.gray.ignoresSafeArea()
        LoadingSheet(label: "Planning trip")
    }
}

#Preview("Loading Sheet with Detail") {
    ZStack {
        Color.gray.ignoresSafeArea()
        LoadingSheet(label: "Exporting PDF", detail: "Generating maps...")
    }
}
