package com.example.godeye.models

import android.os.Build

/**
 * Информация об устройстве для регистрации на сервере
 */
data class DeviceInfo(
    val model: String = Build.MODEL,
    val androidVersion: String = Build.VERSION.RELEASE,
    val appVersion: String = "1.0.0", // Заменяем BuildConfig на хардкод для упрощения
    val availableCameras: List<String>
)

/**
 * Активная сессия с оператором
 */
data class CameraSession(
    val sessionId: String,
    val operatorId: String,
    val cameraType: String,
    val startTime: Long,
    var isActive: Boolean = true,
    var webRTCConnected: Boolean = false
)

/**
 * Запрос доступа к камере от оператора
 */
data class CameraRequest(
    val sessionId: String,
    val operatorId: String,
    val cameraType: String,
    val timestamp: Long = System.currentTimeMillis()
)

/**
 * Ответ на запрос доступа к камере
 */
data class CameraResponse(
    val sessionId: String,
    val accepted: Boolean,
    val reason: String? = null
)

/**
 * WebRTC Offer/Answer данные
 */
data class WebRTCMessage(
    val sessionId: String,
    val type: String, // "offer", "answer", "ice-candidate"
    val sdp: String? = null,
    val candidate: String? = null,
    val sdpMid: String? = null,
    val sdpMLineIndex: Int? = null
)

/**
 * События Socket.IO
 */
sealed class SocketEvent {
    data class RegisterAndroid(
        val deviceId: String,
        val deviceInfo: DeviceInfo
    ) : SocketEvent()

    data class CameraRequest(
        val sessionId: String,
        val operatorId: String,
        val cameraType: String
    ) : SocketEvent()

    data class CameraResponse(
        val sessionId: String,
        val accepted: Boolean,
        val reason: String? = null
    ) : SocketEvent()

    data class CameraDisconnect(
        val sessionId: String
    ) : SocketEvent()

    data class CameraSwitch(
        val sessionId: String,
        val newCameraType: String
    ) : SocketEvent()

    data class WebRTCOffer(
        val sessionId: String,
        val offer: String
    ) : SocketEvent()

    data class WebRTCAnswer(
        val sessionId: String,
        val answer: String
    ) : SocketEvent()

    data class WebRTCIceCandidate(
        val sessionId: String,
        val candidate: String,
        val sdpMid: String,
        val sdpMLineIndex: Int
    ) : SocketEvent()
}

/**
 * Состояния подключения
 */
enum class ConnectionState {
    DISCONNECTED,
    CONNECTING,
    CONNECTED,
    ERROR,
    RECONNECTING
}

/**
 * Состояния WebRTC соединения
 */
enum class WebRTCConnectionState {
    NEW,
    CONNECTING,
    CONNECTED,
    DISCONNECTED,
    FAILED,
    CLOSED
}

/**
 * Типы ошибок приложения
 */
sealed class AppError {
    object NetworkError : AppError()
    object CameraPermissionDenied : AppError()
    object AudioPermissionDenied : AppError()
    object CameraNotAvailable : AppError()
    object WebRTCConnectionFailed : AppError()
    data class SocketError(val message: String) : AppError()
    data class CameraError(val message: String) : AppError()
    data class UnknownError(val throwable: Throwable) : AppError()
}

/**
 * UI состояние главного экрана
 */
data class MainScreenState(
    val deviceId: String = "",
    val serverUrl: String = "",
    val connectionState: ConnectionState = ConnectionState.DISCONNECTED,
    val activeSessions: List<CameraSession> = emptyList(),
    val isLoading: Boolean = false,
    val error: AppError? = null,
    val showCameraRequest: CameraRequest? = null
)
