// LoopFollow
// SettingsMigrationManager.swift

import Foundation

class SettingsMigrationManager {
    // MARK: - Current Version

    static let currentVersion = "1.0"

    // MARK: - Migration Methods

    static func migrateSettings(_ data: Data) -> CombinedSettingsExport? {
        // Try to decode with the current version
        do {
            let currentSettings = try JSONDecoder().decode(CombinedSettingsExport.self, from: data)
            LogManager.shared.log(category: .general, message: "Successfully decoded CombinedSettingsExport", isDebug: true)
            return currentSettings
        } catch {
            LogManager.shared.log(category: .general, message: "Failed to decode CombinedSettingsExport: \(String(describing: type(of: error)))", isDebug: true)

            // Try to decode as individual components
            return tryDecodeIndividualComponents(data)
        }
    }

    private static func tryDecodeIndividualComponents(_ data: Data) -> CombinedSettingsExport? {
        // Try to decode as AlarmSettingsExport
        if let alarmSettings = try? JSONDecoder().decode(AlarmSettingsExport.self, from: data) {
            LogManager.shared.log(category: .general, message: "Successfully decoded as AlarmSettingsExport", isDebug: true)
            return CombinedSettingsExport(
                alarms: alarmSettings,
                exportType: "Alarm Settings"
            )
        }

        // Try to decode as NightscoutSettingsExport
        if let nightscoutSettings = try? JSONDecoder().decode(NightscoutSettingsExport.self, from: data) {
            LogManager.shared.log(category: .general, message: "Successfully decoded as NightscoutSettingsExport", isDebug: true)
            return CombinedSettingsExport(
                nightscout: nightscoutSettings,
                exportType: "Nightscout Settings"
            )
        }

        // Try to decode as DexcomSettingsExport
        if let dexcomSettings = try? JSONDecoder().decode(DexcomSettingsExport.self, from: data) {
            LogManager.shared.log(category: .general, message: "Successfully decoded as DexcomSettingsExport", isDebug: true)
            return CombinedSettingsExport(
                dexcom: dexcomSettings,
                exportType: "Dexcom Settings"
            )
        }

        // Try to decode as RemoteSettingsExport
        if let remoteSettings = try? JSONDecoder().decode(RemoteSettingsExport.self, from: data) {
            LogManager.shared.log(category: .general, message: "Successfully decoded as RemoteSettingsExport", isDebug: true)
            return CombinedSettingsExport(
                remote: remoteSettings,
                exportType: "Remote Settings"
            )
        }

        LogManager.shared.log(category: .general, message: "Failed to decode as any known component", isDebug: true)
        return nil
    }

    // MARK: - Version Compatibility

    static func isCompatibleVersion(_ version: String) -> Bool {
        let currentVersionComponents = currentVersion.split(separator: ".").compactMap { Int($0) }
        let importVersionComponents = version.split(separator: ".").compactMap { Int($0) }

        // For now, accept any version (can be made more strict later)
        return true
    }

    static func getCompatibilityMessage(for version: String) -> String {
        return "Settings from version \(version) may not be fully compatible with current version \(currentVersion). Some features may not work as expected."
    }

    // MARK: - Error Handling

    enum SettingsImportError: Error {
        case unsupportedVersion(String)
        case migrationFailed(String)
        case corruptedData
        case incompatibleAlarmFormat
        case unknownError
    }
}
