//
//  DrivingInsightsExtensions.swift
//  SentianceDrivingInsights
//
//  Created by Mohammed Aouf Zouag on 17/09/2025.
//

import SENTSDK
import RNSentianceCore

extension SENTSDK.DrivingEvent: SentianceNamespaceWrappable {}
extension SENTSDK.CallWhileMovingEvent: SentianceNamespaceWrappable {}
extension SENTSDK.HarshDrivingEvent: SentianceNamespaceWrappable {}
extension SENTSDK.WrongWayDrivingEvent: SentianceNamespaceWrappable {}
extension SENTSDK.PhoneUsageEvent: SentianceNamespaceWrappable {}
extension SENTSDK.SpeedingEvent: SentianceNamespaceWrappable {}
extension SENTSDK.CallEvent: SentianceNamespaceWrappable {}

extension SENTSDK.HandsFreeState: SentianceNamespaceWrappable {}
extension SENTSDK.CallState: SentianceNamespaceWrappable {}
extension SENTSDK.HarshDrivingEventType: SentianceNamespaceWrappable {}

extension SentianceTypeWrapper where T: SENTSDK.DrivingEvent {
  func asDrivingEventDictionary() -> [String: Any] {
    return [
      "startTime": value.startDate.description,
      "startTimeEpoch": value.startDate.sentiance.millisecondsSince1970,
      "endTime": value.endDate.description,
      "endTimeEpoch": value.endDate.sentiance.millisecondsSince1970,
      "waypoints": value.waypoints.map {$0.sentiance.asDictionary()},
    ]
  }
}

extension SentianceTypeWrapper where T == SENTSDK.WrongWayDrivingEvent {
  func asDictionary() -> [String: Any] {
    return value.sentiance.asDrivingEventDictionary()
  }
}

extension SentianceTypeWrapper where T == SENTSDK.PhoneUsageEvent {
  func asDictionary() -> [String: Any?] {
    var dict = value.sentiance.asDrivingEventDictionary()
    dict["callState"] = value.callState.sentiance.toJsString()
    return dict
  }
}

extension SentianceTypeWrapper where T == SENTSDK.HarshDrivingEvent {
  func asDictionary() -> [String: Any] {
    var dict = value.sentiance.asDrivingEventDictionary()
    dict["magnitude"] = value.magnitude
    dict["confidence"] = value.confidence
    dict["type"] = value.type.sentiance.toJsString()
    return dict
  }
}

extension SentianceTypeWrapper where T == SENTSDK.SpeedingEvent {
  func asDictionary() -> [String: Any] {
    return value.sentiance.asDrivingEventDictionary()
  }
}

extension SentianceTypeWrapper where T == SENTSDK.CallWhileMovingEvent {
  func asDictionary() -> [String: Any] {
    var dict = value.sentiance.asDrivingEventDictionary()
    if value.maxTraveledSpeedInMps != nil {
      dict["maxTravelledSpeedInMps"] = value.maxTraveledSpeedInMps
    }
    if value.minTraveledSpeedInMps != nil {
      dict["minTravelledSpeedInMps"] = value.minTraveledSpeedInMps
    }
    return dict
  }
}

extension SentianceTypeWrapper where T == SENTSDK.CallEvent {
  func asDictionary() -> [String: Any?] {
    var dict = value.sentiance.asDrivingEventDictionary()
    dict["maxTraveledSpeedInMps"] = value.maxTraveledSpeedInMps
    dict["minTraveledSpeedInMps"] = value.minTraveledSpeedInMps
    dict["handsFreeState"] = value.handsFreeState.sentiance.toJsString()
    return dict
  }
}

extension SentianceTypeWrapper where T == SENTSDK.HandsFreeState {
  func toJsString() -> String {
    switch value {
    case .unavailable: return "UNAVAILABLE"
    case .handheld: return "HANDHELD"
    case .handsFree: return "HANDS_FREE"
    }
  }
}

extension SentianceTypeWrapper where T == SENTSDK.CallState {
  func toJsString() -> String {
    switch value {
    case .noCall: return "NO_CALL"
    case .callInProgress: return "CALL_IN_PROGRESS"
    case .unavailable: return "UNAVAILABLE"
    }
  }
}

extension SentianceTypeWrapper where T == SENTSDK.HarshDrivingEventType {
  func toJsString() -> String {
    switch value {
    case .acceleration: return "ACCELERATION"
    case .braking: return "BRAKING"
    case .turn: return "TURN"
    }
  }
}
