//
//  OnboardingState.swift
//  CasinoKit
//
//  Tracks first-time user onboarding progress and contextual hints.
//

import Foundation
import SwiftUI

/// Observable state for managing user onboarding and progressive feature discovery.
@Observable
@MainActor
public final class OnboardingState {
    // MARK: - Properties
    
    /// Whether the user has launched the app before.
    public var hasLaunchedBefore: Bool = false
    
    /// Whether the user has completed the welcome sheet.
    public var hasCompletedWelcome: Bool = false
    
    /// Whether the user is in tutorial mode (shows all contextual hints).
    public var isTutorialMode: Bool = false
    
    /// Set of hint keys that have been shown to the user.
    public var hintsShown: Set<String> = []
    
    /// Hint keys registered by the app for automatic skipping.
    /// When the user skips onboarding, all registered hints are marked as shown.
    private var registeredHintKeys: Set<String> = []
    
    // MARK: - Initialization
    
    private let persistenceKey: String
    
    public init(gameIdentifier: String) {
        self.persistenceKey = "onboarding.\(gameIdentifier)"
        load()
    }
    
    /// Registers hint keys that should be marked as shown when skipping onboarding.
    /// Call this once during app setup with all hint keys used by the game.
    public func registerHintKeys(_ keys: Set<String>) {
        registeredHintKeys = keys
    }
    
    /// Registers hint keys that should be marked as shown when skipping onboarding.
    /// Call this once during app setup with all hint keys used by the game.
    public func registerHintKeys(_ keys: String...) {
        registeredHintKeys = Set(keys)
    }
    
    // MARK: - Hint Management
    
    /// Marks a hint as shown and persists the state.
    public func markHintShown(_ key: String) {
        hintsShown.insert(key)
        save()
    }
    
    /// Returns whether a specific hint should be shown.
    public func shouldShowHint(_ key: String) -> Bool {
        isTutorialMode || !hintsShown.contains(key)
    }
    
    /// Marks the welcome sheet as completed.
    public func completeWelcome() {
        hasLaunchedBefore = true
        hasCompletedWelcome = true
        save()
    }
    
    /// Skips onboarding entirely - marks all registered hints as shown and completes welcome.
    /// Use this when the user dismisses the welcome sheet without choosing tutorial mode
    /// (e.g., swiping down to dismiss, or tapping "Start Playing").
    public func skipOnboarding() {
        for key in registeredHintKeys {
            hintsShown.insert(key)
        }
        hasLaunchedBefore = true
        hasCompletedWelcome = true
        save()
    }
    
    /// Enables tutorial mode (shows all hints again).
    public func startTutorialMode() {
        isTutorialMode = true
    }
    
    /// Disables tutorial mode.
    public func endTutorialMode() {
        isTutorialMode = false
    }
    
    /// Resets all onboarding state (for testing or user-requested reset).
    public func reset() {
        hasLaunchedBefore = false
        hasCompletedWelcome = false
        isTutorialMode = false
        hintsShown.removeAll()
        save()
    }
    
    // MARK: - Persistence
    
    private func load() {
        let defaults = UserDefaults.standard
        hasLaunchedBefore = defaults.bool(forKey: "\(persistenceKey).hasLaunched")
        hasCompletedWelcome = defaults.bool(forKey: "\(persistenceKey).hasCompletedWelcome")
        
        if let hintsData = defaults.array(forKey: "\(persistenceKey).hintsShown") as? [String] {
            hintsShown = Set(hintsData)
        }
    }
    
    private func save() {
        let defaults = UserDefaults.standard
        defaults.set(hasLaunchedBefore, forKey: "\(persistenceKey).hasLaunched")
        defaults.set(hasCompletedWelcome, forKey: "\(persistenceKey).hasCompletedWelcome")
        defaults.set(Array(hintsShown), forKey: "\(persistenceKey).hintsShown")
    }
}

