//
//  SettingsViewModel.swift
//  SelfieCam
//
//  Created by Matt Bruce on 1/4/26.
//

import SwiftUI
import Bedrock
import MijickCamera

// MARK: - Settings ViewModel

/// Observable settings view model with iCloud sync across all devices.
/// Uses Bedrock's CloudSyncManager for automatic synchronization.
/// Premium features are automatically reset to defaults when user doesn't have premium.
///
/// Extensions:
/// - `SettingsViewModel+RingLight.swift` - Ring light settings
/// - `SettingsViewModel+Camera.swift` - Camera settings (flash, HDR, timer, etc.)
/// - `SettingsViewModel+Premium.swift` - Premium/debug toggle and reset logic
/// - `SettingsViewModel+CloudSync.swift` - iCloud sync conformance
@MainActor
@Observable
final class SettingsViewModel: RingLightConfigurable {
    
    // MARK: - Ring Size Limits
    
    /// Minimum ring border size in points
    static let minRingSize: CGFloat = 40
    
    /// Maximum ring border size in points
    static let maxRingSize: CGFloat = 120
    
    /// Default ring border size
    static let defaultRingSize: CGFloat = 40
    
    // MARK: - Premium Constants
    
    /// Default free color ID for non-premium users
    static let defaultFreeColorId = "pureWhite"
    
    /// Premium color IDs that require subscription
    static let premiumColorIds: Set<String> = ["iceBlue", "softPink", "warmAmber", "coolLavender", RingLightColor.customId]
    
    /// Premium timer options that require subscription
    static let premiumTimerOptions: Set<TimerOption> = [.five, .ten]
    
    /// Premium photo quality options that require subscription
    static let premiumPhotoQualities: Set<PhotoQuality> = [.high]
    
    // MARK: - Internal Dependencies
    
    /// Premium manager for checking subscription status
    @ObservationIgnored let premiumManager = PremiumManager()
    
    /// Manages iCloud sync for settings across all devices
    let cloudSync = CloudSyncManager<SyncedSettings>()
    
    /// Debounce task for slider values
    var debounceTask: Task<Void, Never>?
    
    /// Debounce delay for continuous slider updates (in seconds)
    private static let debounceDelay: Duration = .milliseconds(300)
    
    // MARK: - Cached Values (for immediate UI updates)
    
    /// Cached ring size for immediate UI updates (before debounced save)
    var _cachedRingSize: CGFloat?
    
    /// Cached light color ID for immediate UI updates
    var _cachedLightColorId: String?
    
    /// Cached custom color for immediate UI updates
    var _cachedCustomColor: Color?
    
    // MARK: - Premium Status
    
    /// Whether the user has premium access (stored property for proper UI updates)
    private var _isPremiumUnlocked: Bool = false
    
    /// Whether the user has premium access (observable for UI updates)
    var isPremiumUnlocked: Bool {
        get { _isPremiumUnlocked }
        set { _isPremiumUnlocked = newValue }
    }
    
    // MARK: - Display Settings
    
    /// Whether the camera preview is flipped to show a true mirror (PREMIUM)
    var isMirrorFlipped: Bool {
        get { PremiumGate.get(cloudSync.data.isMirrorFlipped, default: false, isPremium: isPremiumUnlocked) }
        set { 
            guard PremiumGate.canSet(isPremium: isPremiumUnlocked) else { return }
            updateSettings { $0.isMirrorFlipped = newValue } 
        }
    }
    
    /// Whether skin smoothing filter is enabled (PREMIUM)
    var isSkinSmoothingEnabled: Bool {
        get { PremiumGate.get(cloudSync.data.isSkinSmoothingEnabled, default: false, isPremium: isPremiumUnlocked) }
        set { 
            guard PremiumGate.canSet(isPremium: isPremiumUnlocked) else { return }
            updateSettings { $0.isSkinSmoothingEnabled = newValue } 
        }
    }
    
    /// Whether the grid overlay is visible
    var isGridVisible: Bool {
        get { cloudSync.data.isGridVisible }
        set { updateSettings { $0.isGridVisible = newValue } }
    }
    
    /// Current camera zoom factor
    var currentZoomFactor: Double {
        get { cloudSync.data.currentZoomFactor }
        set { updateSettings { $0.currentZoomFactor = newValue } }
    }
    
    // MARK: - Capture Settings
    
    /// Whether captures are auto-saved to Photo Library
    var isAutoSaveEnabled: Bool {
        get { cloudSync.data.isAutoSaveEnabled }
        set { updateSettings { $0.isAutoSaveEnabled = newValue } }
    }
    
    // MARK: - Initialization
    
    init() {
        // Initialize premium status
        _isPremiumUnlocked = premiumManager.isPremiumUnlocked
        // CloudSyncManager handles syncing automatically
    }
    
    // MARK: - Internal Methods
    
    /// Updates settings and saves to cloud immediately
    func updateSettings(_ transform: (inout SyncedSettings) -> Void) {
        cloudSync.update { settings in
            transform(&settings)
            settings.modificationCount += 1
        }
    }
    
    /// Debounces save operations for continuous values like sliders
    func debouncedSave(key: String, action: @escaping () -> Void) {
        // Cancel any pending debounce
        debounceTask?.cancel()
        
        // Schedule debounced save
        debounceTask = Task {
            try? await Task.sleep(for: Self.debounceDelay)
            
            guard !Task.isCancelled else { return }
            
            action()
        }
    }
    
    // MARK: - Validation
    
    var isValidConfiguration: Bool {
        ringSize >= Self.minRingSize
    }
}
