package com.example.godeye.managers

import com.example.godeye.models.CameraSession
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.asStateFlow

class SessionManager {
    private val _sessions = MutableStateFlow<Map<String, CameraSession>>(emptyMap())
    val sessions: StateFlow<Map<String, CameraSession>> = _sessions.asStateFlow()

    fun createSession(sessionId: String, operatorId: String, cameraType: String) {
        val session = CameraSession(
            sessionId = sessionId,
            operatorId = operatorId,
            cameraType = cameraType,
            isActive = true,
            startTime = System.currentTimeMillis()
        )

        val currentSessions = _sessions.value.toMutableMap()
        currentSessions[sessionId] = session
        _sessions.value = currentSessions
    }

    fun endSession(sessionId: String, reason: String = "Session ended") {
        val currentSessions = _sessions.value.toMutableMap()
        currentSessions.remove(sessionId)
        _sessions.value = currentSessions
    }

    fun endAllSessions(reason: String = "All sessions ended") {
        _sessions.value = emptyMap()
    }
}
