package com.nate.posedetection

import androidx.compose.runtime.Composable
import androidx.compose.runtime.compositionLocalOf

/**
 * Parameters for an unattended ("auto") experiment-mode run, supplied by the
 * Mac orchestrator (`tools/run_auto_experiment.sh`) via Android intent extras
 * and surfaced to Compose code through [LocalExperimentAutoSpec].
 *
 * The phone schedules its experiment buffer to start at [startAtWallMs]
 * (a `System.currentTimeMillis()` target chosen by the orchestrator) and
 * automatically stops it [durationMs] later. The orchestrator picks the same
 * wall-clock target for the QuickTime play moment, so the two start in
 * approximate sync without needing any explicit handshake. Any constant
 * Mac↔phone clock skew cancels out across runs and does not affect model A
 * vs. model B comparison.
 */
data class ExperimentLaunchSpec(
    /** Display name (or filename) of the model to select before starting. */
    val modelName: String,
    /** Wall-clock target (System.currentTimeMillis()) for the buffer start. */
    val startAtWallMs: Long,
    /** How long the buffer should run before auto-stop and JSON write. */
    val durationMs: Long,
    /** If true, the activity calls finish() after the JSON has been written. */
    val finishOnStop: Boolean = true,
)

/**
 * Composition-scoped slot for an [ExperimentLaunchSpec]. Android sets this
 * from `AppActivity.onCreate` when the launch intent carries the auto-mode
 * extras; iOS leaves it null.
 */
val LocalExperimentAutoSpec = compositionLocalOf<ExperimentLaunchSpec?> { null }

/**
 * Bridge for platform-specific side effects the auto driver needs:
 *
 *  * [log] writes a tagged line that the orchestrator scrapes via
 *    `adb logcat -s ExperimentAuto:I` to track progress (ready / started /
 *    stopped / error).
 *  * [finishActivity] tears the activity down once the JSON is written so
 *    the next run starts cold.
 */
interface ExperimentAutoBridge {
    fun log(msg: String)
    fun finishActivity()
}

@Composable
expect fun rememberExperimentAutoBridge(): ExperimentAutoBridge
