//
//  DataConverter.swift
//  MyTatvaCare
//
//  Created by Nirav Ramani on 31/05/25.
//

import Foundation

func ReceiveDataToLog(data: GlucoseData) -> CGMLog {
    let cgmLogs = CGMLog(
        timeInMillis: Double(Date().timeIntervalSince1970 * 1000),
        countdownMinutes: (data.countdownMinutes as? Int) ?? 0,
        countdownDays: (data.countdownDays as? Int) ?? 0,
        hypoglycemiaEarlyWarnMinutes: 0,  // still missing, you can map if available
        showGlucoseMG: (data.gluADC as? Int) ?? 0,
        glucoseId: Int(truncating: data.glucoseId ?? 0),
        name: data.device?.advertise?.localName ?? "",
        bytes: [],  // still missing: need raw data source
        showGlucose: (data.glu as? Float) ?? 0,
        Ib: (data.blankCurrent as? Float) ?? 0,
        Iw: (data.operatingCurrent as? Float) ?? 0,
        countdownHours: (data.countdownHours as? Int) ?? 0,
        T: (data.temperature as? Float) ?? 0.0,
        year: (data.year as? Int32) ?? 0,
        month: (data.month as? Int32) ?? 0,
        day: (data.day as? Int32) ?? 0,
        hour: (data.hour as? Int32) ?? 0,
        minute: (data.minute as? Int32) ?? 0,
        
        trendObject: TrendObject(
            trendId: (data.trend as? Int) ?? 0,
            drawableId: 2103818652,  // customize if needed
            widgetImg: -1,
            apsChangeRate: "FLAT"  // you could map a real value if available
        ),

        glucoseStatusObject: GlucoseStatusObject(
            statusId: 1  // you may derive this from a field like `calibrationStatus`
        ),

        errorObject: ErrorObject(
            errorId: (data.errorCode as? Int) ?? 0,
            sound: "None"
        )
    )
    
    return cgmLogs
}
