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

import SwiftUI
import Bedrock

// MARK: - Premium Features

extension SettingsViewModel {
    
    // MARK: - Debug Premium Toggle
    
    /// Debug premium toggle for testing (DEBUG builds only)
    var isDebugPremiumEnabled: Bool {
        get { premiumManager.isDebugPremiumToggleEnabled }
        set {
            let wasPremium = premiumManager.isDebugPremiumToggleEnabled
            premiumManager.isDebugPremiumToggleEnabled = newValue
            
            // Update premium status for UI refresh
            isPremiumUnlocked = premiumManager.isPremiumUnlocked
            
            // Reset premium settings when toggling OFF
            if wasPremium && !newValue {
                resetPremiumSettingsToDefaults()
            }
        }
    }
    
    // MARK: - Premium Reset
    
    /// Reset premium-only settings to their free defaults when debug premium is disabled
    func resetPremiumSettingsToDefaults() {
        // Clear cached values that depend on premium status
        _cachedRingSize = nil
        _cachedLightColorId = nil
        _cachedCustomColor = nil
        
        // Reset premium settings to defaults (bypass premium gates for direct reset)
        updateSettings { settings in
            if settings.lightColorId != Self.defaultFreeColorId {
                settings.lightColorId = Self.defaultFreeColorId
            }
            
            if settings.isMirrorFlipped {
                settings.isMirrorFlipped = false
            }
            
            if settings.isSkinSmoothingEnabled {
                settings.isSkinSmoothingEnabled = false
            }
            
            if settings.isFlashSyncedWithRingLight {
                settings.isFlashSyncedWithRingLight = false
            }
            
            if settings.hdrModeRaw != CameraHDRMode.off.rawValue {
                settings.hdrModeRaw = CameraHDRMode.off.rawValue
            }
            
            if settings.photoQualityRaw != PhotoQuality.medium.rawValue {
                settings.photoQualityRaw = PhotoQuality.medium.rawValue
            }
            
            // Only reset timer if it's a premium option (5s or 10s)
            let currentTimer = TimerOption(rawValue: settings.selectedTimerRaw) ?? .three
            if Self.premiumTimerOptions.contains(currentTimer) {
                settings.selectedTimerRaw = TimerOption.three.rawValue
            }
            
            if settings.isCenterStageEnabled {
                settings.isCenterStageEnabled = false
            }
        }
        
        // Force sync to ensure changes are saved
        forceSync()
    }
}
