package com.optix.android.ai.providers

import com.optix.android.domain.model.AiMessage

// ---------------------------------------------------------------------------
// Unified LLM provider interface
// ---------------------------------------------------------------------------

interface LlmProvider {
    /** Single-shot generation — returns full response string. */
    suspend fun generate(
        messages: List<AiMessage>,
        schemas: List<ToolSchema> = emptyList(),
    ): String

    /** Streaming generation — emits tokens, tool calls, and a Done signal. */
    fun stream(
        messages: List<AiMessage>,
        schemas: List<ToolSchema> = emptyList(),
    ): kotlinx.coroutines.flow.Flow<ProviderEvent>
}

// ---------------------------------------------------------------------------
// Events emitted by streaming providers
// ---------------------------------------------------------------------------

sealed class ProviderEvent {
    data class Token(val text: String) : ProviderEvent()
    data class ToolCall(
        val id: String,
        val toolName: String,
        val arguments: Map<String, Any>,
    ) : ProviderEvent()
    /**
     * Emitted when the assistant produces a multi-step chain (JSON or structured
     * text) that should be executed in the NETWORK tab. [raw] is the original
     * assistant text containing the chain definition.
     */
    data class ToolChain(val raw: String) : ProviderEvent()
    data object Done : ProviderEvent()
}

// ---------------------------------------------------------------------------
// OpenAI-style tool schema (shared across all providers)
// ---------------------------------------------------------------------------

data class ToolSchema(
    val name: String,
    val description: String,
    val parameters: Map<String, ToolSchemaParam>,
)

data class ToolSchemaParam(
    val type: String,               // "string" | "integer" | "boolean" | "array"
    val description: String,
    val required: Boolean,
    val enum: List<String>? = null,
)
