package solutions.tretter.mindmachine.domain

import solutions.tretter.mindmachine.session.FlashSequenceRow
import solutions.tretter.mindmachine.session.defaultFlashSequence

enum class VisualPattern { FLASH, PULSE }
enum class SessionMode { AUDIO_VISUAL, AUDIO_ONLY, VISUAL_ONLY }
enum class RuntimeState { IDLE, COUNTDOWN, RUNNING, PAUSED, INTERRUPTED, COMPLETED, STOPPED, ERROR }

data class SessionPreset(
    val id: String,
    val name: String,
    val description: String,
    val defaultDurationSec: Int,
    val visualPatternType: VisualPattern,
    val flashIntervalMs: Int,
    val intensityPercent: Int,
    val carrierFrequencyHz: Float,
    val binauralDifferenceHz: Float,
    val cautionNote: String,
    val sortOrder: Int,
    val isUserProgram: Boolean = false,
    val flashSequence: List<FlashSequenceRow> = defaultFlashSequence(),
)

data class SessionConfig(
    val presetId: String,
    val durationSec: Int,
    val mode: SessionMode,
    val visualPatternType: VisualPattern,
    val flashIntervalMs: Int,
    val flashSequence: List<FlashSequenceRow> = defaultFlashSequence(),
    val monochromeFlashMode: Boolean = false,
    val intensityPercent: Int,
    val carrierFrequencyHz: Float,
    val binauralDifferenceHz: Float,
)

data class AppSettings(
    val safetyAcknowledged: Boolean = false,
    val safetyAcknowledgedVersion: Int = 0,
    val countdownSeconds: Int = 5,
    val forceAudioWithoutHeadphones: Boolean = false,
    val showImmersiveProgressBar: Boolean = true,
    val preferLowestRefreshRate: Boolean = true,
    val monochromeFlashMode: Boolean = false,
    // Default used only before DataStore emits (and as a fallback).
    // We want the first-run experience to be less blinding.
    val immersiveBrightnessPercent: Int = 50,
    val lastPresetId: String? = null,
    val noAds: Boolean = false,
)

object Presets {
    val builtIn = listOf(
        SessionPreset(
            "deep_release", "Deep Release", "Slow descent into calm with a soft ending", 18 * 60,
            VisualPattern.PULSE, 260, 52, 180f, 4.5f,
            "Stop if discomfort occurs.", 1
        ),
        SessionPreset(
            "laser_focus", "Laser Focus", "Progressive ramp to sharp attention and steady drive", 16 * 60,
            VisualPattern.FLASH, 120, 66, 220f, 12f,
            "Use in a safe seated place only.", 2
        ),
        SessionPreset(
            "creative_drift", "Creative Drift", "Alternating intensity waves for idea exploration", 22 * 60,
            VisualPattern.PULSE, 190, 58, 205f, 8f,
            "Use only in a safe resting environment.", 3
        ),
        SessionPreset(
            "reset_breath", "Reset Breath", "Gentle rhythmic arcs designed for breathing cadence", 12 * 60,
            VisualPattern.PULSE, 300, 46, 170f, 5.5f,
            "Do not use while driving or operating machinery.", 4
        ),
        SessionPreset(
            "night_rampdown", "Night Rampdown", "Long taper from busy mind to pre-sleep stillness", 28 * 60,
            VisualPattern.PULSE, 360, 40, 160f, 2.8f,
            "Do not use while doing other activities.", 5
        ),
        SessionPreset(
            "power_warmup", "Power Warmup", "Short energizing ladder for pre-work momentum", 9 * 60,
            VisualPattern.FLASH, 95, 68, 245f, 14f,
            "Use in a safe seated place only.", 6
        ),
        SessionPreset(
            "body_scan", "Body Scan", "Balanced mid-tempo sweep with a centered finish", 20 * 60,
            VisualPattern.PULSE, 210, 50, 190f, 6.8f,
            "Stop immediately for dizziness or discomfort.", 7
        ),
        SessionPreset(
            "storm_to_still", "Storm to Still", "Starts active, then settles into deep quiet", 24 * 60,
            VisualPattern.FLASH, 150, 61, 215f, 9f,
            "Use only in a safe resting environment.", 8
        )
    )
}

fun SessionPreset.toConfig(defaultMode: SessionMode = SessionMode.AUDIO_VISUAL) = SessionConfig(
    presetId = id,
    durationSec = defaultDurationSec,
    mode = defaultMode,
    visualPatternType = visualPatternType,
    flashIntervalMs = flashIntervalMs,
    flashSequence = flashSequence,
    monochromeFlashMode = false,
    intensityPercent = intensityPercent,
    carrierFrequencyHz = carrierFrequencyHz,
    binauralDifferenceHz = binauralDifferenceHz,
)
