package com.nate.posedetection

import android.os.Build
import androidx.compose.runtime.Composable
import androidx.compose.runtime.remember
import androidx.compose.ui.platform.LocalContext
import java.io.File

private class AndroidExperimentLogger(
    private val externalFilesDir: File?,
) : ExperimentLogger {
    override val deviceLabel: String = "${Build.MANUFACTURER} ${Build.MODEL}".trim()

    override fun write(filename: String, json: String): String? {
        val baseDir = externalFilesDir ?: return null
        val logsDir = File(baseDir, "experiment_logs").apply { mkdirs() }
        val outFile = File(logsDir, filename)
        return try {
            outFile.writeText(json)
            outFile.absolutePath
        } catch (t: Throwable) {
            null
        }
    }
}

@Composable
actual fun rememberExperimentLogger(): ExperimentLogger {
    val context = LocalContext.current
    return remember(context) {
        AndroidExperimentLogger(context.getExternalFilesDir(null))
    }
}
