import SwiftUI

// MARK: - TourCardView

/// The info card shown during a tour step. Contains progress dots, title, body, and navigation buttons.
struct TourCardView: View {

    let coordinator: TourCoordinator
    let totalSteps: Int
    let currentIndex: Int

    var body: some View {
        VStack(spacing: 0) {

            // ── Header: progress dots + close button ──────────────────────────
            HStack(alignment: .center) {
                progressDots
                Spacer()
                Button {
                    coordinator.close()
                } label: {
                    Image(systemName: "xmark")
                        .font(.system(size: 13, weight: .semibold))
                        .foregroundStyle(.secondary)
                        .padding(8)
                        .background(Color.secondary.opacity(0.12), in: Circle())
                }
                .accessibilityLabel("Tour schließen")
            }
            .padding(.horizontal, 20)
            .padding(.top, 18)

            // ── Content: title + body ─────────────────────────────────────────
            if let step = coordinator.currentStep {
                VStack(spacing: 12) {
                    Text(step.title)
                        .font(.title3.bold())
                        .multilineTextAlignment(.center)
                        .fixedSize(horizontal: false, vertical: true)

                    Text(step.body)
                        .font(.subheadline)
                        .foregroundStyle(.secondary)
                        .multilineTextAlignment(.center)
                        .fixedSize(horizontal: false, vertical: true)
                }
                .padding(.horizontal, 20)
                .padding(.top, 18)
                .padding(.bottom, 8)
            }

            // ── Footer: skip + back/next buttons ─────────────────────────────
            HStack {
                // Step counter + skip
                VStack(alignment: .leading, spacing: 2) {
                    Button {
                        coordinator.skip()
                    } label: {
                        Text("Tour überspringen")
                            .font(.caption)
                            .foregroundStyle(.tertiary)
                    }

                    Text(verbatim: String.localizedStringWithFormat(
                        String(localized: "%lld von %lld"),
                        Int64(currentIndex + 1),
                        Int64(totalSteps)
                    ))
                    .font(.caption2)
                    .foregroundStyle(.quaternary)
                }

                Spacer()

                // Back button (hidden on first step)
                if !coordinator.isFirstStep {
                    Button {
                        coordinator.previous()
                    } label: {
                        Text("Zurück")
                            .font(.subheadline.weight(.medium))
                            .foregroundStyle(.secondary)
                    }
                    .padding(.trailing, 8)
                }

                // Next / Finish button — explicit LocalizedStringKey to ensure lookup
                let nextLabel: LocalizedStringKey = coordinator.isLastStep ? "Loslegen" : "Weiter"
                Button {
                    coordinator.next()
                } label: {
                    Text(nextLabel)
                        .font(.subheadline.weight(.semibold))
                        .foregroundStyle(.white)
                        .padding(.horizontal, 18)
                        .padding(.vertical, 9)
                        .background(NahbarInsightStyle.accentPetrol, in: Capsule())
                }
            }
            .padding(.horizontal, 20)
            .padding(.top, 12)
            .padding(.bottom, 20)
        }
        .background(.regularMaterial, in: RoundedRectangle(cornerRadius: 22, style: .continuous))
        .shadow(color: .black.opacity(0.18), radius: 24, y: 10)
        .sensoryFeedback(.selection, trigger: currentIndex)
    }

    // MARK: Progress Dots

    private var progressDots: some View {
        HStack(spacing: 5) {
            ForEach(0..<totalSteps, id: \.self) { i in
                Capsule()
                    .fill(i == currentIndex ? NahbarInsightStyle.accentPetrol : Color.secondary.opacity(0.3))
                    .frame(
                        width: i == currentIndex ? 20 : 6,
                        height: 6
                    )
                    .animation(.spring(response: 0.3, dampingFraction: 0.7), value: currentIndex)
            }
        }
        .accessibilityLabel("Schritt \(currentIndex + 1) von \(totalSteps)")
        .accessibilityHidden(false)
    }
}
