package eu.gaudian.translator.view.hints

import android.content.Context
import androidx.compose.material3.MaterialTheme
import androidx.compose.runtime.Composable
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.tooling.preview.Preview
import eu.gaudian.translator.R

/**
 * Example of a migrated hint using the new markdown-based approach.
 *
 * This demonstrates how to migrate from the old LegacyHint format to the new
 * markdown-based format.
 *
 * Benefits:
 * - Easier to manage and translate (no code changes needed)
 * - Better separation of concerns
 * - Consistent styling across all hints
 * - Support for rich formatting (tables, code blocks, etc.)
 */

/**
 * Example 1: Loading markdown content from string (simple).
 */
@Composable
fun getApiKeyMarkdownHint(): String {
    return """
# How to Connect to an AI Model

This guide explains how to connect your app to an AI model using an API key.

## Getting Started

To use AI models in your app, you need to provide a valid API key.

> **Note:** Keep your API key secure and never share it publicly.

## Key Status Indicators

| Status | Icon | Meaning |
|--------|------|---------|
| Active | ✅ | Key is valid and working |
| Missing | ⚠️ | Key is not set |

## Troubleshooting

1. Verify the key is correct
2. Ensure proper permissions
3. Check your quota

```json
{
  "api_key": "sk-xxxxxxxxxxxx"
}
```
    """.trimIndent()
}

@Composable
fun ApiKeyMarkdownHint() {
    val content = getApiKeyMarkdownHint()
    MarkdownHint(
        markdownContent = content,
        title = stringResource(R.string.hint_how_to_connect_to_an_ai)
    )
}

/**
 * Example 2: Loading markdown from assets file (recommended for production).
 */
@Composable
fun loadMarkdownFromAssets(fileName: String): String {
    val context = LocalContext.current
    return try {
        context.assets.open("hints/$fileName").bufferedReader().use { it.readText() }
    } catch (e: Exception) {
        "Error loading markdown file: ${e.message}"
    }
}

/**
 * Data class for programmatic hint loading.
 */
data class MarkdownHintDefinition(
    val fileName: String,
    val titleRes: Int
) {
    fun loadContent(context: Context): String {
        return context.assets.open("hints/$fileName").bufferedReader().use { it.readText() }
    }
}

/**
 * Pre-defined hints ready for migration.
 */
object MarkdownHints {
    val API_KEY = MarkdownHintDefinition(
        fileName = "api_key_hint.md",
        titleRes = R.string.hint_how_to_connect_to_an_ai
    )
    val CATEGORY = MarkdownHintDefinition(
        fileName = "category_hint.md",
        titleRes = R.string.category_hint_intro
    )
    val LEARNING_STAGES = MarkdownHintDefinition(
        fileName = "learning_stages_hint.md",
        titleRes = R.string.learning_stages_title
    )
    val SORTING = MarkdownHintDefinition(
        fileName = "sorting_hint.md",
        titleRes = R.string.sorting_hint_title
    )
    val VOCABULARY_PROGRESS = MarkdownHintDefinition(
        fileName = "vocabulary_progress_hint.md",
        titleRes = R.string.hint_vocabulary_progress_hint_title
    )
}

/**
 * Preview for the migrated API Key hint.
 */
@Preview
@Composable
fun ApiKeyMarkdownHintPreview() {
    MaterialTheme {
        ApiKeyMarkdownHint()
    }
}

/**
 * Preview for loading from assets.
 */
@Preview
@Composable
fun LoadFromAssetsPreview() {
    MaterialTheme {
        val content = loadMarkdownFromAssets("example_hint.md")
        MarkdownHint(
            markdownContent = content,
            title = stringResource(R.string.hint_title_hints_overview)
        )
    }
}
