import Foundation
import Combine

// MARK: - OnboardingStep

/// Each phase of the first-launch onboarding flow.
enum OnboardingStep: Int, CaseIterable {
    case profile  = 0
    case quiz     = 1    // Persönlichkeitsquiz-Prompt
    case contacts = 2    // was 1
    case tour     = 3    // was 2
    case complete = 4    // was 3
}

// MARK: - OnboardingCoordinator

/// Observable coordinator that owns all onboarding data and drives step transitions.
@MainActor
final class OnboardingCoordinator: ObservableObject {

    // MARK: – Navigation state

    @Published var currentStep: OnboardingStep = .profile

    // MARK: – Phase 1: Profile

    @Published var firstName: String   = ""
    @Published var displayName: String = ""
    @Published var aboutMe: String     = ""
    @Published var gender: String      = ""

    // MARK: – Phase 2: Contacts

    @Published var selectedContacts: [NahbarContact] = []

    // MARK: – Validation

    /// `true` when at least a non-empty first name has been entered.
    var isProfileValid: Bool {
        !firstName.trimmingCharacters(in: .whitespaces).isEmpty
    }

    // MARK: – Navigation actions

    /// Advances to the personality quiz prompt if the profile is valid.
    func advanceToQuiz() {
        guard isProfileValid else { return }
        currentStep = .quiz
    }

    /// Skips the personality quiz and goes directly to contact import.
    func skipQuiz() {
        currentStep = .contacts
    }

    /// Called after the personality quiz completes or is dismissed; advances to contact import.
    func advanceFromQuizToContacts() {
        currentStep = .contacts
    }

    /// Advances to the contact import phase. Validates profile first.
    func advanceToContacts() {
        guard isProfileValid else { return }
        currentStep = .contacts
    }

    /// Advances to the feature tour if at least one contact has been selected.
    func advanceToTour() {
        guard !selectedContacts.isEmpty else { return }
        currentStep = .tour
    }

    /// Skips the contact selection and goes directly to the feature tour.
    func skipToTour() {
        currentStep = .tour
    }

    /// Marks onboarding as fully complete.
    func completeOnboarding() {
        currentStep = .complete
    }
}
