import Foundation
struct CSVExportDestination: SyncDestination {
let id: UUID
var name: String
var isEnabled: Bool
var exportDirectory: URL
init(
id: UUID = UUID(),
name: String = "CSV Export",
isEnabled: Bool = true,
exportDirectory: URL
) {
self.id = id
self.name = name
self.isEnabled = isEnabled
self.exportDirectory = exportDirectory
}
func sync(data: [HealthDataPoint]) async throws {
let dateFormatter = ISO8601DateFormatter()
var csvContent = "metric,value,unit,timestamp\n"
for point in data {
let timestamp = dateFormatter.string(from: point.timestamp)
csvContent += "\(point.metricType.rawValue),\(point.value),\(point.unit),\(timestamp)\n"
}
let fileName = "healthpush-\(dateFormatter.string(from: Date())).csv"
let fileURL = exportDirectory.appendingPathComponent(fileName)
try csvContent.write(to: fileURL, atomically: true, encoding: .utf8)
}
func testConnection() async throws -> Bool {
// Verify we can write to the export directory
let testFile = exportDirectory.appendingPathComponent(".healthpush-test")
do {
try "test".write(to: testFile, atomically: true, encoding: .utf8)
try FileManager.default.removeItem(at: testFile)
return true
} catch {
return false
}
}
}