@file:Suppress("HardCodedStringLiteral")

package eu.gaudian.translator.model.communication

import android.content.Context
import eu.gaudian.translator.model.LanguageModel
import eu.gaudian.translator.utils.ProviderConfigParser
import kotlinx.serialization.Serializable
import kotlinx.serialization.Transient
import java.nio.file.NoSuchFileException

@Serializable
data class ApiProvider(
    val key: String,
    val displayName: String,
    val baseUrl: String,
    val endpoint: String,
    val websiteUrl: String,
    val models: List<LanguageModel>,
    val isCustom: Boolean = false
) {
    @Transient
    var hasValidKey: Boolean = false
    val defaultModel: String? get() = models.firstOrNull()?.modelId

    companion object {

        /**
         * Loads providers from the JSON configuration file with fallback to hardcoded providers
         * @param context Android context for accessing assets
         * @return List of ApiProvider instances
         */
        fun loadProviders(context: Context): List<ApiProvider> {
            val providersFromJson = ProviderConfigParser.loadProvidersFromAssets(context)
            return providersFromJson.ifEmpty {
                // Fallback to hardcoded providers if JSON loading fails
                getHardcodedProviders()
                throw NoSuchFileException("providers.json not found in assets")
            }
        }

        /**
         * Returns the hardcoded default providers list
         * @return List of ApiProvider instances
         */
        private fun getHardcodedProviders(): List<ApiProvider> {
            return listOf(
                ApiProvider(
                    key = "mistral",
                    displayName = "Mistral AI",
                    baseUrl = "https://api.mistral.ai/",
                    endpoint = "v1/chat/completions",
                    websiteUrl = "https://mistral.ai",
                    models = listOf(
                        LanguageModel("mistral-small-latest", "Mistral Small Latest", "mistral", "Fast and efficient for simple tasks."),
                        LanguageModel("open-mistral-nemo", "Mistral Nemo", "mistral", "Advanced model with native function calling."),
                    )
                ),
                ApiProvider(
                    key = "openai",
                    displayName = "OpenAI",
                    baseUrl = "https://api.openai.com/",
                    endpoint = "v1/chat/completions",
                    websiteUrl = "https://platform.openai.com/",
                    models = listOf(
                        LanguageModel("gpt-5-nano", "GPT 5 Nano", "openai", "Fast and cheap model sufficient for most tasks."),
                    )
                ),
                ApiProvider(
                    key = "anthropic",
                    displayName = "Anthropic",
                    baseUrl = "https://api.anthropic.com/",
                    endpoint = "v1/messages",
                    websiteUrl = "https://www.anthropic.com/",
                    models = listOf(
                    )
                ),
                ApiProvider(
                    key = "deepseek",
                    displayName = "DeepSeek",
                    baseUrl = "https://api.deepseek.com/",
                    endpoint = "chat/completions",
                    websiteUrl = "https://www.deepseek.com/",
                    models = listOf(
                        LanguageModel("deepseek-chat", "DeepSeek Chat", "deepseek", "Specialized in code and reasoning.")
                    )
                ),
                ApiProvider(
                    key = "gemini",
                    displayName = "Google Gemini",
                    baseUrl = "https://generativelanguage.googleapis.com/",
                    endpoint = "v1beta/models/gemini-2.5-flash:generateContent",
                    websiteUrl = "https://ai.google/",
                    models = listOf(
                        LanguageModel("gemini-2.5-flash", "Gemini 2.5 Flash", "gemini", "Current default: Fast, grounded, strong at conversational and search tasks."),
                        LanguageModel("gemini-2.5-flash-lite", "Gemini 2.5 Flash Lite", "gemini", "Fastest and most cost-efficient Gemini model for high throughput and low-latency needs.")
                    )
                ),
                ApiProvider(
                    key = "openrouter",
                    displayName = "OpenRouter",
                    baseUrl = "https://openrouter.ai/api/",
                    endpoint = "v1/chat/completions",
                    websiteUrl = "https://openrouter.ai",
                    models = listOf(
                    )
                ),
                ApiProvider(
                    key = "groq",
                    displayName = "Groq",
                    baseUrl = "https://api.groq.com/openai/",
                    endpoint = "v1/chat/completions",
                    websiteUrl = "https://groq.com/",
                    models = listOf(
                        LanguageModel("llama-3.1-8b-instant", "Llama 3.1 8B", "groq", "Powerful Llama 3 model running at extreme speed."),
                    )
                ),
                ApiProvider(
                    key = "perplexity",
                    displayName = "Perplexity",
                    baseUrl = "https://api.perplexity.ai/",
                    endpoint = "chat/completions",
                    websiteUrl = "https://www.perplexity.ai/",
                    models = listOf(
                        LanguageModel("sonar", "Sonar Small Online", "perplexity", "A faster online model for quick, up-to-date answers."), // default
                        LanguageModel("sonar-pro", "Sonar Pro", "perplexity", "Advanced search-focused model for richer context and longer answers."),
                    )
                ),
                ApiProvider(
                    key = "xai",
                    displayName = "xAI Grok",
                    baseUrl = "https://api.x.ai/",
                    endpoint = "v1/chat/completions",
                    websiteUrl = "https://x.ai",
                    models = listOf(
                        LanguageModel("grok-4-fast", "Grok 4 Fast", "xai", "Fast and flexible model from xAI.")
                    )
                )
            )
        }
    }
}
