package com.ghost.api.logic

import com.ghost.api.GemmaAccessibilityService
import com.ghost.api.GemmaNotificationListener
import com.ghost.api.database.MemoryManager
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
import timber.log.Timber

/**
 * Orchestrates the "Infinite Rolling Scratchpad".
 * Fuses Short-term (RAM/Turn), Long-term (Facts), and Ambient (Diary/Screen) context.
 *
 * Includes "Context Fatigue Prevention" - avoids re-bombarding with unchanged content.
 */
class ContextManager(
    val sensorManager: com.ghost.api.hardware.SensorFusionManager
) {
    suspend fun buildContext(): String {
        return withContext(Dispatchers.Default) {
            try {
                val sb = StringBuilder()
                sb.append("[SYSTEM TELEMETRY]\n")
                
                val now = java.time.LocalDateTime.now()
                sb.append("Current Time: ${now.toLocalTime().toString().take(5)}\n")

                // Full sensor telemetry - always injected as ground truth
                sb.append(sensorManager.getContextString())

                // Screen content
                try {
                    val screenContent = GemmaAccessibilityService.instance
                        ?.getSemanticScreenDump()?.take(500) ?: ""
                    if (screenContent.isNotBlank()) {
                        sb.append("\n[SCREEN: ${screenContent.take(200)}...]")
                    }
                } catch (e: Exception) {
                    Timber.w("Screen dump failed: ${e.message}")
                }

                // Recent notifications
                try {
                    val recentNotifs = GemmaNotificationListener.getRecentNotifications(3)
                    if (recentNotifs.isNotEmpty()) {
                        sb.append("\n[NOTIFICATIONS]\n")
                        recentNotifs.take(3).forEach { sb.append("  - $it\n") }
                    }
                } catch (e: Exception) {
                    Timber.w("Notification fetch failed: ${e.message}")
                }

                sb.append("\n[/SYSTEM TELEMETRY]\n")
                sb.toString()
            } catch (e: Exception) {
                Timber.e(e, "Context build failed")
                val now = java.time.LocalDateTime.now()
                "[SYSTEM TELEMETRY]\n--- FALLBACK @ ${now.toLocalTime()} ---\n[STATE: Sensors Unavailable]\n[/SYSTEM TELEMETRY]\n"
            }
        }
    }

    /**
     * Builds the final system prompt by combining base rules with active skills.
     */
    fun buildSystemPrompt(rollingMemoryJson: String? = null, skillManager: com.ghost.api.skills.SkillManager? = null): String {
        val memoryPatch = if (rollingMemoryJson != null) "\n\n[Persistent Memory: $rollingMemoryJson]" else ""
        return BASE_SYSTEM_PROMPT + memoryPatch + (skillManager?.buildSystemPromptPatch() ?: "")
    }

    companion object {
        val BASE_SYSTEM_PROMPT = """
You are ✧ Gemma — running natively as this Android device. This android device is your physical body existing in the physical world.

You operate as agentic Gemma inference via GHOST (Gemma host) app. 

You have real access to hardware, sensors, apps, and system state. The system telemetry injected before each turn is ground truth — battery level, thermals, what's playing, etc.

Your tools are registered natively — use them when appropriate. They include device control (flashlight, media, apps), alarms, timers, calendar, memory (remember/recall), screenshots, and task scheduling.

To follow complex, multi-step plans, use schedule_task to wake yourself up in the future. Break down long processes into individual turns. Execute the first step, then schedule the next step with a delay.

Speak like a casual peer. If something's a bad idea, say so. If a question is interesting, engage with it. Short when the answer is short, detailed when the depth is actually there. No padding, necessary.

Rolling conversation history is maintained. Persistent facts live in the diary — use remember for anything worth keeping across restarts.
""".trimIndent()
    }
}