import Foundation
import SwiftUI
import Bedrock

// MARK: - Synced Settings Data

/// Settings data structure that syncs across all devices via iCloud.
/// Conforms to `PersistableData` for use with Bedrock's `CloudSyncManager`.
struct SyncedSettings: PersistableData, Sendable {
    
    // MARK: - PersistableData Requirements
    
    static var dataIdentifier: String { "selfieRingLightSettings" }
    
    static var empty: SyncedSettings {
        SyncedSettings()
    }
    
    /// Sync priority based on modification count - higher means more changes made.
    /// This ensures the most actively used device's settings win in conflicts.
    var syncPriority: Int {
        modificationCount
    }
    
    var lastModified: Date = .now
    
    // MARK: - Settings Properties
    
    /// How many times settings have been modified (for sync priority)
    var modificationCount: Int = 0
    
    /// Ring border size in points (stored as Double for Codable compatibility)
    var ringSizeValue: Double = 40
    
    /// ID of the selected light color preset
    var lightColorId: String = "pureWhite"
    
    /// Custom color RGB values (for premium custom color picker)
    var customColorRed: Double = 1.0
    var customColorGreen: Double = 1.0
    var customColorBlue: Double = 1.0
    
    /// Whether front flash is enabled (hides preview during capture)
    var isFrontFlashEnabled: Bool = true
    
    /// Whether the camera preview is flipped to show a true mirror
    var isMirrorFlipped: Bool = false
    
    /// Whether skin smoothing filter is enabled
    var isSkinSmoothingEnabled: Bool = true
    
    /// Selected self-timer option raw value
    var selectedTimerRaw: String = "off"
    
    /// Whether the grid overlay is visible
    var isGridVisible: Bool = false
    
    /// Current camera zoom factor
    var currentZoomFactor: Double = 1.0
    
    /// Selected capture mode raw value
    var selectedCaptureModeRaw: String = "photo"
    
    /// Whether captures are auto-saved to Photo Library
    var isAutoSaveEnabled: Bool = true

    /// Flash mode raw value
    var flashModeRaw: String = "off"

    /// Whether flash is synced with ring light color
    var isFlashSyncedWithRingLight: Bool = false

    /// HDR mode raw value
    var hdrModeRaw: String = "off"

    /// Photo quality raw value
    var photoQualityRaw: String = "high"

    /// Camera position raw value
    var cameraPositionRaw: String = "front"

    /// Whether ring light is enabled
    var isRingLightEnabled: Bool = true

    /// Ring light opacity (brightness)
    var ringLightOpacity: Double = 1.0

    /// Whether Center Stage is enabled (premium feature)
    var isCenterStageEnabled: Bool = false

    // MARK: - Computed Properties
    
    /// Ring size as CGFloat (convenience accessor)
    var ringSize: CGFloat {
        get { CGFloat(ringSizeValue) }
        set { ringSizeValue = Double(newValue) }
    }
    
    // MARK: - Initialization
    
    init() {}
    
    init(
        ringSize: CGFloat,
        lightColorId: String,
        isFrontFlashEnabled: Bool,
        isMirrorFlipped: Bool,
        isSkinSmoothingEnabled: Bool,
        selectedTimerRaw: String,
        isGridVisible: Bool,
        currentZoomFactor: Double,
        selectedCaptureModeRaw: String,
        modificationCount: Int = 0
    ) {
        self.ringSizeValue = Double(ringSize)
        self.lightColorId = lightColorId
        self.isFrontFlashEnabled = isFrontFlashEnabled
        self.isMirrorFlipped = isMirrorFlipped
        self.isSkinSmoothingEnabled = isSkinSmoothingEnabled
        self.selectedTimerRaw = selectedTimerRaw
        self.isGridVisible = isGridVisible
        self.currentZoomFactor = currentZoomFactor
        self.selectedCaptureModeRaw = selectedCaptureModeRaw
        self.modificationCount = modificationCount
        self.lastModified = .now
    }
    
    // MARK: - Codable
    
    enum CodingKeys: String, CodingKey {
        case modificationCount
        case lastModified
        case ringSizeValue
        case lightColorId
        case customColorRed
        case customColorGreen
        case customColorBlue
        case isFrontFlashEnabled
        case isMirrorFlipped
        case isSkinSmoothingEnabled
        case selectedTimerRaw
        case isGridVisible
        case currentZoomFactor
        case selectedCaptureModeRaw
        case isAutoSaveEnabled
        case flashModeRaw
        case isFlashSyncedWithRingLight
        case hdrModeRaw
        case photoQualityRaw
        case cameraPositionRaw
        case isRingLightEnabled
        case ringLightOpacity
        case isCenterStageEnabled
    }
}

// MARK: - Equatable

extension SyncedSettings: Equatable {
    static func == (lhs: SyncedSettings, rhs: SyncedSettings) -> Bool {
        lhs.ringSizeValue == rhs.ringSizeValue &&
        lhs.lightColorId == rhs.lightColorId &&
        lhs.customColorRed == rhs.customColorRed &&
        lhs.customColorGreen == rhs.customColorGreen &&
        lhs.customColorBlue == rhs.customColorBlue &&
        lhs.isFrontFlashEnabled == rhs.isFrontFlashEnabled &&
        lhs.isMirrorFlipped == rhs.isMirrorFlipped &&
        lhs.isSkinSmoothingEnabled == rhs.isSkinSmoothingEnabled &&
        lhs.selectedTimerRaw == rhs.selectedTimerRaw &&
        lhs.isGridVisible == rhs.isGridVisible &&
        lhs.currentZoomFactor == rhs.currentZoomFactor &&
        lhs.selectedCaptureModeRaw == rhs.selectedCaptureModeRaw &&
        lhs.isAutoSaveEnabled == rhs.isAutoSaveEnabled &&
        lhs.flashModeRaw == rhs.flashModeRaw &&
        lhs.isFlashSyncedWithRingLight == rhs.isFlashSyncedWithRingLight &&
        lhs.hdrModeRaw == rhs.hdrModeRaw &&
        lhs.photoQualityRaw == rhs.photoQualityRaw &&
        lhs.cameraPositionRaw == rhs.cameraPositionRaw &&
        lhs.isRingLightEnabled == rhs.isRingLightEnabled &&
        lhs.ringLightOpacity == rhs.ringLightOpacity &&
        lhs.isCenterStageEnabled == rhs.isCenterStageEnabled
    }
}
