package de.krisenvorrat.server.routes

import de.krisenvorrat.server.model.VersionInfo
import io.ktor.http.*
import io.ktor.server.application.*
import io.ktor.server.response.*
import io.ktor.server.routing.*

internal fun Route.versionRoutes(versionCode: Int, versionName: String) {
    get("/api/version") {
        val scheme = call.request.headers["X-Forwarded-Proto"] ?: "https"
        val host = call.request.headers["X-Forwarded-Host"]
            ?: call.request.headers["Host"]
            ?: "localhost"
        val apkUrl = "$scheme://$host/static/app-latest.apk"

        call.respond(VersionInfo(versionCode, versionName, apkUrl))
    }

    get("/") {
        val scheme = call.request.headers["X-Forwarded-Proto"] ?: "https"
        val host = call.request.headers["X-Forwarded-Host"]
            ?: call.request.headers["Host"]
            ?: "localhost"
        val apkUrl = "$scheme://$host/static/app-latest.apk"

        val html = buildHomepageHtml(versionName, versionCode, apkUrl)
        call.respondText(html, ContentType.Text.Html, HttpStatusCode.OK)
    }
}

private fun buildHomepageHtml(versionName: String, versionCode: Int, apkUrl: String): String = """
<!DOCTYPE html>
<html lang="de">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Krisenvorrat</title>
    <style>
        * { margin: 0; padding: 0; box-sizing: border-box; }
        body {
            font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
            background: #f5f5f5;
            color: #333;
            display: flex;
            justify-content: center;
            align-items: center;
            min-height: 100vh;
        }
        .card {
            background: white;
            border-radius: 16px;
            padding: 48px;
            box-shadow: 0 4px 24px rgba(0,0,0,0.1);
            text-align: center;
            max-width: 400px;
            width: 90%;
        }
        h1 { font-size: 28px; margin-bottom: 8px; color: #1a1a1a; }
        .version { color: #666; font-size: 14px; margin-bottom: 32px; }
        #qrcode { display: flex; justify-content: center; margin-bottom: 24px; }
        #qrcode canvas { border-radius: 8px; }
        .download-link {
            display: inline-block;
            background: #2196F3;
            color: white;
            text-decoration: none;
            padding: 12px 24px;
            border-radius: 8px;
            font-weight: 500;
            transition: background 0.2s;
        }
        .download-link:hover { background: #1976D2; }
        .hint { margin-top: 16px; font-size: 12px; color: #999; }
    </style>
</head>
<body>
    <div class="card">
        <h1>Krisenvorrat</h1>
        <p class="version">Version $versionName (Build $versionCode)</p>
        <div id="qrcode"></div>
        <a href="${apkUrl.replace("\"", "&quot;")}" class="download-link">APK herunterladen</a>
        <p class="hint">QR-Code scannen oder Link antippen, um die App zu installieren.</p>
    </div>
    <script src="https://cdn.jsdelivr.net/npm/qrcodejs@1.0.0/qrcode.min.js"></script>
    <script>
        new QRCode(document.getElementById("qrcode"), {
            text: ${quoteJsString(apkUrl)},
            width: 200,
            height: 200,
            correctLevel: QRCode.CorrectLevel.M
        });
    </script>
</body>
</html>
""".trimIndent()

private fun quoteJsString(value: String): String {
    val escaped = value
        .replace("\\", "\\\\")
        .replace("\"", "\\\"")
        .replace("<", "\\u003c")
        .replace(">", "\\u003e")
    return "\"$escaped\""
}
