package com.example.scanwich

import android.Manifest
import android.app.DatePickerDialog
import android.app.TimePickerDialog
import android.content.Context
import android.content.Intent
import android.content.SharedPreferences
import android.content.pm.PackageManager
import android.graphics.Bitmap
import android.graphics.BitmapFactory
import android.net.Uri
import android.os.Bundle
import android.util.Base64
import android.util.Log
import android.widget.Toast
import androidx.activity.ComponentActivity
import androidx.activity.compose.rememberLauncherForActivityResult
import androidx.activity.compose.setContent
import androidx.activity.result.contract.ActivityResultContracts
import androidx.compose.foundation.Image
import androidx.compose.foundation.background
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.*
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.items
import androidx.compose.foundation.rememberScrollState
import androidx.compose.foundation.text.KeyboardOptions
import androidx.compose.foundation.verticalScroll
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.automirrored.filled.KeyboardArrowLeft
import androidx.compose.material.icons.automirrored.filled.KeyboardArrowRight
import androidx.compose.material.icons.filled.*
import androidx.compose.material3.*
import androidx.compose.runtime.*
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.StrokeCap
import androidx.compose.ui.graphics.asImageBitmap
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.text.input.KeyboardType
import androidx.compose.ui.unit.dp
import androidx.core.content.ContextCompat
import androidx.core.content.edit
import androidx.exifinterface.media.ExifInterface
import androidx.navigation.compose.NavHost
import androidx.navigation.compose.composable
import androidx.navigation.compose.rememberNavController
import com.example.scanwich.ui.theme.ScanwichTheme
import com.google.android.gms.auth.api.signin.GoogleSignIn
import com.google.android.gms.auth.api.signin.GoogleSignInOptions
import com.google.android.gms.common.api.ApiException
import com.google.gson.annotations.SerializedName
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.launch
import okhttp3.OkHttpClient
import okhttp3.ResponseBody
import okhttp3.logging.HttpLoggingInterceptor
import retrofit2.Retrofit
import java.text.SimpleDateFormat
import java.util.*
import java.util.concurrent.TimeUnit
import retrofit2.converter.gson.GsonConverterFactory
import retrofit2.http.*
import java.io.ByteArrayOutputStream
import org.json.JSONObject

// --- API MODELS ---
data class N8nMealRequest(
    val imageBase64: String?,
    val mealName: String?,
    val prompt: String
)

interface N8nApi {
    @POST("webhook/v1/gemini-proxy")
    suspend fun analyzeMeal(
        @Header("X-API-KEY") apiKey: String,
        @Body request: N8nMealRequest
    ): ResponseBody
}

// --- STRAVA API ---
data class StravaActivity(
    val id: Long,
    val name: String,
    val type: String,
    val distance: Float,
    @SerializedName("moving_time") val movingTime: Int,
    @SerializedName("elapsed_time") val elapsedTime: Int,
    val calories: Float?,
    @SerializedName("start_date") val startDate: String,
    @SerializedName("start_date_local") val startDateLocal: String
)

data class StravaTokenResponse(
    @SerializedName("access_token") val accessToken: String,
    @SerializedName("refresh_token") val refreshToken: String,
    @SerializedName("expires_at") val expiresAt: Long
)

interface StravaApi {
    @GET("athlete/activities")
    suspend fun getActivities(
        @Header("Authorization") token: String,
        @Query("before") before: Long? = null,
        @Query("after") after: Long? = null,
        @Query("page") page: Int? = null,
        @Query("per_page") perPage: Int? = 30
    ): List<StravaActivity>

    @POST("oauth/token")
    suspend fun exchangeToken(
        @Query("client_id") clientId: String,
        @Query("client_secret") clientSecret: String,
        @Query("code") code: String,
        @Query("grant_type") grantType: String = "authorization_code"
    ): StravaTokenResponse

    @POST("oauth/token")
    suspend fun refreshToken(
        @Query("client_id") clientId: String,
        @Query("client_secret") clientSecret: String,
        @Query("refresh_token") refreshToken: String,
        @Query("grant_type") grantType: String = "refresh_token"
    ): StravaTokenResponse
}

// Helpers
object ApiClient {
    private val loggingInterceptor = HttpLoggingInterceptor().apply {
        level = HttpLoggingInterceptor.Level.BODY
    }

    private val okHttpClient = OkHttpClient.Builder()
        .addInterceptor(loggingInterceptor)
        .connectTimeout(60, TimeUnit.SECONDS)
        .readTimeout(60, TimeUnit.SECONDS)
        .writeTimeout(60, TimeUnit.SECONDS)
        .build()

    private val retrofitStrava = Retrofit.Builder()
        .baseUrl("https://www.strava.com/api/v3/")
        .addConverterFactory(GsonConverterFactory.create())
        .build()

    private val retrofitN8n = Retrofit.Builder()
        .baseUrl("https://n8n.marquis1987.com/")
        .client(okHttpClient)
        .addConverterFactory(GsonConverterFactory.create())
        .build()

    val stravaApi: StravaApi = retrofitStrava.create(StravaApi::class.java)
    val n8nApi: N8nApi = retrofitN8n.create(N8nApi::class.java)

    suspend fun getValidStravaToken(prefs: SharedPreferences): String? {
        val stravaToken = prefs.getString("strava_token", null) ?: return null
        val expiresAt = prefs.getLong("strava_expires_at", 0)
        val refreshToken = prefs.getString("strava_refresh_token", null)
        val clientId = prefs.getString("strava_client_id", "") ?: ""
        val clientSecret = prefs.getString("strava_client_secret", "") ?: ""

        val currentTime = System.currentTimeMillis() / 1000
        if (currentTime >= expiresAt && refreshToken != null && clientId.isNotBlank()) {
            try {
                val refreshResponse = stravaApi.refreshToken(clientId, clientSecret, refreshToken)
                prefs.edit {
                    putString("strava_token", refreshResponse.accessToken)
                    putString("strava_refresh_token", refreshResponse.refreshToken)
                    putLong("strava_expires_at", refreshResponse.expiresAt)
                }
                return refreshResponse.accessToken
            } catch (_: Exception) {
                return null
            }
        }
        return stravaToken
    }

    fun parseStravaDate(dateStr: String): Long {
        return try {
            val inputFormat = SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'", Locale.US)
            inputFormat.timeZone = TimeZone.getTimeZone("UTC")
            inputFormat.parse(dateStr)?.time ?: 0L
        } catch (_: Exception) { 0L }
    }

    fun estimateCaloriesFromDb(activity: SportActivity, weightKg: Double): Int {
        if (activity.calories != null && activity.calories > 0) return activity.calories.toInt()
        val met = when (activity.type.lowercase()) {
            "run" -> 10.0
            "ride" -> 8.0
            "walk" -> 3.5
            "hike" -> 6.0
            "swim" -> 7.0
            "weighttraining" -> 5.0
            "workout" -> 4.5
            else -> 5.0
        }
        val durationHours = activity.movingTime / 3600.0
        return (met * weightKg * durationHours).toInt()
    }
}

// --- UI COMPONENTS ---

class MainActivity : ComponentActivity() {
    private lateinit var dao: AppDao
    
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        dao = AppDatabase.getDatabase(this).appDao()
        handleStravaCallback(intent)
        setContent {
            ScanwichTheme {
                AuthWrapper(dao)
            }
        }
    }

    override fun onNewIntent(intent: Intent) {
        super.onNewIntent(intent)
        handleStravaCallback(intent)
    }

    private fun handleStravaCallback(intent: Intent) {
        val data: Uri? = intent.data
        if (data != null && data.toString().startsWith("coloricam://localhost")) {
            val code = data.getQueryParameter("code")
            if (code != null) {
                val prefs = getSharedPreferences("user_prefs", Context.MODE_PRIVATE)
                val clientId = prefs.getString("strava_client_id", "") ?: ""
                val clientSecret = prefs.getString("strava_client_secret", "") ?: ""
                
                if (clientId.isNotEmpty() && clientSecret.isNotEmpty()) {
                    CoroutineScope(kotlinx.coroutines.Dispatchers.IO).launch {
                        try {
                            val response = ApiClient.stravaApi.exchangeToken(clientId, clientSecret, code)
                            prefs.edit {
                                putString("strava_token", response.accessToken)
                                putString("strava_refresh_token", response.refreshToken)
                                putLong("strava_expires_at", response.expiresAt)
                            }
                        } catch (e: Exception) {
                            Log.e("StravaAuth", "Exchange failed: ${e.message}")
                        }
                    }
                }
            }
        }
    }
}

@Composable
@Suppress("DEPRECATION")
fun AuthWrapper(dao: AppDao) {
    val context = LocalContext.current
    val gso = remember {
        GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
            .requestEmail()
            .requestIdToken("652626507041-5n42q37adh1guuv9gibfcf5uvekgunbe.apps.googleusercontent.com")
            .build()
    }
    val googleSignInClient = remember { GoogleSignIn.getClient(context, gso) }
    var account by remember { mutableStateOf(GoogleSignIn.getLastSignedInAccount(context)) }
    
    val allowedEmails = listOf("marcandre.charest@gmail.com", "everousseau07@gmail.com")

    val launcher = rememberLauncherForActivityResult(ActivityResultContracts.StartActivityForResult()) { result ->
        val task = GoogleSignIn.getSignedInAccountFromIntent(result.data)
        try {
            val signedInAccount = task.getResult(ApiException::class.java)
            account = signedInAccount
            Log.d("Auth", "Connecté avec : ${signedInAccount.email}")
        } catch (e: ApiException) {
            Log.e("Auth", "Erreur Google Sign-In : ${e.statusCode}")
            val msg = when(e.statusCode) {
                10 -> "Erreur 10 : SHA-1 non reconnu dans Firebase. Assurez-vous d\u0027avoir ajouté le SHA-1 de TOUTES vos clés de signature."
                7 -> "Erreur 7 : Problème de réseau."
                12500 -> "Erreur 12500 : Problème de configuration Google Play Services."
                else -> "Erreur Google (Code ${e.statusCode})."
            }
            Toast.makeText(context, msg, Toast.LENGTH_LONG).show()
        }
    }

    val onLogout: () -> Unit = {
        googleSignInClient.signOut().addOnCompleteListener {
            account = null
        }
    }

    if (account == null) {
        LoginScreen { launcher.launch(googleSignInClient.signInIntent) }
    } else {
        val userEmail = account?.email?.lowercase() ?: ""
        if (allowedEmails.contains(userEmail)) {
            MainApp(dao, onLogout)
        } else {
            AccessDeniedScreen(onLogout)
        }
    }
}

@Composable
fun LoginScreen(onLoginClick: () -> Unit) {
    Column(
        modifier = Modifier.fillMaxSize().padding(16.dp),
        horizontalAlignment = Alignment.CenterHorizontally,
        verticalArrangement = Arrangement.Center
    ) {
        Text("Scan-Wich", style = MaterialTheme.typography.displayLarge, color = MaterialTheme.colorScheme.primary)
        Spacer(Modifier.height(32.dp))
        Button(onClick = onLoginClick, modifier = Modifier.fillMaxWidth().height(56.dp)) {
            Icon(Icons.Default.AccountCircle, null)
            Spacer(Modifier.width(8.dp))
            Text("Se connecter avec Google")
        }
    }
}

@Composable
fun AccessDeniedScreen(onLogout: () -> Unit) {
    Column(
        modifier = Modifier.fillMaxSize().padding(32.dp),
        horizontalAlignment = Alignment.CenterHorizontally,
        verticalArrangement = Arrangement.Center
    ) {
        Icon(Icons.Default.Warning, null, modifier = Modifier.size(80.dp), tint = MaterialTheme.colorScheme.error)
        Spacer(Modifier.height(16.dp))
        Text("Accès Refusé", style = MaterialTheme.typography.headlineMedium, color = MaterialTheme.colorScheme.error)
        Spacer(Modifier.height(8.dp))
        Text("Votre compte n\u0027est pas autorisé à utiliser cette application.", style = MaterialTheme.typography.bodyLarge)
        Spacer(Modifier.height(32.dp))
        Button(onClick = onLogout) { Text("Changer de compte") }
    }
}

@Composable
fun MainApp(dao: AppDao, onLogout: () -> Unit) {
    val context = LocalContext.current
    val prefs = remember { context.getSharedPreferences("user_prefs", Context.MODE_PRIVATE) }
    var showSetup by remember { mutableStateOf(!prefs.contains("target_calories")) }
    var isDiabetic by remember { mutableStateOf(prefs.getBoolean("is_diabetic", false)) }

    if (showSetup) {
        SetupScreen(prefs) { 
            showSetup = false 
            isDiabetic = prefs.getBoolean("is_diabetic", false)
        }
    } else {
        val navController = rememberNavController()
        Scaffold(
            bottomBar = {
                NavigationBar {
                    NavigationBarItem(icon = { Icon(Icons.Default.Home, "Scan") }, label = { Text("Scan") }, selected = false, onClick = { navController.navigate("capture") })
                    NavigationBarItem(icon = { Icon(Icons.Default.DateRange, "Historique") }, label = { Text("Historique") }, selected = false, onClick = { navController.navigate("history") })
                    NavigationBarItem(icon = { Icon(Icons.Default.Add, "Sport") }, label = { Text("Sport") }, selected = false, onClick = { navController.navigate("sport") })
                    if (isDiabetic) {
                        NavigationBarItem(icon = { Icon(Icons.Default.Favorite, "Glycémie") }, label = { Text("Glycémie") }, selected = false, onClick = { navController.navigate("glycemia") })
                    }
                    NavigationBarItem(icon = { Icon(Icons.Default.Settings, "Paramètres") }, label = { Text("Paramètres") }, selected = false, onClick = { navController.navigate("settings") })
                }
            }
        ) { innerPadding ->
            NavHost(navController, "capture", Modifier.padding(innerPadding)) {
                composable("capture") { CaptureScreen(dao, prefs, isDiabetic) }
                composable("history") { HistoryScreen(dao, prefs) }
                composable("sport") { SportScreen(dao, prefs) }
                composable("glycemia") { GlycemiaScreen(dao) }
                composable("settings") { SettingsScreen(prefs, onLogout) }
            }
        }
    }
}

@Composable
fun SetupScreen(prefs: SharedPreferences, onComplete: () -> Unit) {
    var age by remember { mutableStateOf(prefs.getInt("age", 25).toString()) }
    var heightCm by remember { mutableStateOf(prefs.getString("height_cm", "170") ?: "170") }
    var weight by remember { mutableStateOf(prefs.getString("weight_display", "70") ?: "70") }
    var isLbs by remember { mutableStateOf(prefs.getBoolean("is_lbs", false)) }
    var gender by remember { mutableStateOf(prefs.getString("gender", "Homme") ?: "Homme") }
    var activityLevel by remember { mutableStateOf(prefs.getString("activity_level", "Sédentaire") ?: "Sédentaire") }
    var goal by remember { mutableStateOf(prefs.getString("goal", "Maintenir le poids") ?: "Maintenir le poids") }
    var isDiabetic by remember { mutableStateOf(prefs.getBoolean("is_diabetic", false)) }

    val activityLevels = listOf("Sédentaire", "Légèrement actif", "Modérément actif", "Très actif", "Extrêmement actif")
    val goals = listOf("Maintenir le poids", "Perdre du poids")
    
    val activityMultipliers = mapOf(
        "Sédentaire" to 1.2,
        "Légèrement actif" to 1.375,
        "Modérément actif" to 1.55,
        "Très actif" to 1.725,
        "Extrêmement actif" to 1.9
    )

    Column(
        modifier = Modifier
            .fillMaxSize()
            .padding(24.dp)
            .verticalScroll(rememberScrollState()),
        horizontalAlignment = Alignment.CenterHorizontally
    ) {
        Text("Configuration du profil", style = MaterialTheme.typography.headlineLarge)
        Spacer(Modifier.height(24.dp))

        Text("Votre objectif :", style = MaterialTheme.typography.titleMedium, modifier = Modifier.align(Alignment.Start))
        goals.forEach { g ->
            Row(
                Modifier
                    .fillMaxWidth()
                    .clickable { goal = g }
                    .padding(vertical = 4.dp),
                verticalAlignment = Alignment.CenterVertically
            ) {
                RadioButton(selected = goal == g, onClick = { goal = g })
                Text(g)
            }
        }

        Spacer(Modifier.height(16.dp))

        Row(Modifier.fillMaxWidth(), verticalAlignment = Alignment.CenterVertically) {
            Text("Genre : ", Modifier.width(80.dp))
            RadioButton(selected = gender == "Homme", onClick = { gender = "Homme" })
            Text("Homme")
            Spacer(Modifier.width(16.dp))
            RadioButton(selected = gender == "Femme", onClick = { gender = "Femme" })
            Text("Femme")
        }

        Spacer(Modifier.height(16.dp))

        OutlinedTextField(
            value = age,
            onValueChange = { age = it },
            label = { Text("Âge") },
            keyboardOptions = KeyboardOptions(keyboardType = KeyboardType.Number),
            modifier = Modifier.fillMaxWidth()
        )

        Spacer(Modifier.height(16.dp))

        OutlinedTextField(
            value = heightCm,
            onValueChange = { heightCm = it },
            label = { Text("Taille (cm)") },
            keyboardOptions = KeyboardOptions(keyboardType = KeyboardType.Number),
            modifier = Modifier.fillMaxWidth()
        )

        Spacer(Modifier.height(16.dp))

        Row(verticalAlignment = Alignment.CenterVertically) {
            OutlinedTextField(
                value = weight,
                onValueChange = { weight = it },
                label = { Text(if (isLbs) "Poids (lbs)" else "Poids (kg)") },
                keyboardOptions = KeyboardOptions(keyboardType = KeyboardType.Number),
                modifier = Modifier.weight(1f)
            )
            Spacer(Modifier.width(16.dp))
            Switch(checked = isLbs, onCheckedChange = { isLbs = it })
            Text(if (isLbs) "lbs" else "kg")
        }

        Spacer(Modifier.height(16.dp))

        Text("Niveau d\u0027activité :", style = MaterialTheme.typography.titleMedium, modifier = Modifier.align(Alignment.Start))
        activityLevels.forEach { level ->
            Row(
                Modifier
                    .fillMaxWidth()
                    .clickable { activityLevel = level }
                    .padding(vertical = 4.dp),
                verticalAlignment = Alignment.CenterVertically
            ) {
                RadioButton(selected = activityLevel == level, onClick = { activityLevel = level })
                Text(level)
            }
        }

        Spacer(Modifier.height(16.dp))

        Row(verticalAlignment = Alignment.CenterVertically, modifier = Modifier.fillMaxWidth()) {
            Checkbox(checked = isDiabetic, onCheckedChange = { isDiabetic = it })
            Text("Je suis diabétique")
        }

        Spacer(Modifier.height(32.dp))

        Button(
            onClick = {
                val ageInt = age.toIntOrNull() ?: 0
                val height = heightCm.toDoubleOrNull() ?: 0.0
                var weightKg = weight.toDoubleOrNull() ?: 0.0
                val weightDisplay = weight
                if (isLbs) weightKg *= 0.453592

                val bmr = if (gender == "Homme") {
                    (10 * weightKg) + (6.25 * height) - (5 * ageInt) + 5
                } else {
                    (10 * weightKg) + (6.25 * height) - (5 * ageInt) - 161
                }
                
                val multiplier = activityMultipliers[activityLevel] ?: 1.2
                var targetCals = (bmr * multiplier).toInt()
                
                if (goal == "Perdre du poids") targetCals -= 500
                
                val targetCarbs = (targetCals * 0.5 / 4).toInt()
                val targetProtein = (targetCals * 0.2 / 4).toInt()
                val targetFat = (targetCals * 0.3 / 9).toInt()

                prefs.edit {
                    putString("target_calories", targetCals.toString())
                    putString("target_carbs", targetCarbs.toString())
                    putString("target_protein", targetProtein.toString())
                    putString("target_fat", targetFat.toString())
                    putString("weight_kg", weightKg.toString())
                    putString("weight_display", weightDisplay)
                    putBoolean("is_lbs", isLbs)
                    putString("height_cm", heightCm)
                    putBoolean("is_diabetic", isDiabetic)
                    putInt("age", ageInt)
                    putString("gender", gender)
                    putString("activity_level", activityLevel)
                    putString("goal", goal)
                }
                onComplete()
            },
            modifier = Modifier.fillMaxWidth().height(56.dp),
            enabled = age.isNotBlank() && heightCm.isNotBlank() && weight.isNotBlank()
        ) {
            Text("Sauvegarder le profil")
        }
    }
}

@OptIn(ExperimentalMaterial3Api::class)
@Composable
fun CaptureScreen(dao: AppDao, prefs: SharedPreferences, isDiabetic: Boolean) {
    val coroutineScope = rememberCoroutineScope()
    val context = LocalContext.current
    
    var capturedBitmap by remember { mutableStateOf<Bitmap?>(null) }
    var isAnalyzing by remember { mutableStateOf(false) }
    var currentMealData by remember { mutableStateOf<Triple<String, String, List<Int>>?>(null) }
    var mealDateTime by remember { mutableLongStateOf(System.currentTimeMillis()) }
    val sheetState = rememberModalBottomSheetState(skipPartiallyExpanded = false)
    var showBottomSheet by remember { mutableStateOf(false) }

    var manualMealName by remember { mutableStateOf("") }

    val cameraLauncher = rememberLauncherForActivityResult(ActivityResultContracts.TakePicturePreview()) { bitmap ->
        if (bitmap != null) {
            capturedBitmap = bitmap
            mealDateTime = System.currentTimeMillis()
            analyzeImage(bitmap, null, { isAnalyzing = it }, { data, error ->
                if (data != null) {
                    currentMealData = data
                    showBottomSheet = true
                } else {
                    Toast.makeText(context, "Analyse échouée : $error", Toast.LENGTH_LONG).show()
                }
            }, coroutineScope)
        }
    }

    val permissionLauncher = rememberLauncherForActivityResult(
        ActivityResultContracts.RequestPermission()
    ) { isGranted: Boolean ->
        if (isGranted) {
            cameraLauncher.launch(null)
        } else {
            Toast.makeText(context, "Permission caméra requise", Toast.LENGTH_SHORT).show()
        }
    }

    val galleryLauncher = rememberLauncherForActivityResult(ActivityResultContracts.GetContent()) { uri ->
        uri?.let {
            try {
                val inputStream = context.contentResolver.openInputStream(it)
                val bitmap = BitmapFactory.decodeStream(inputStream)
                capturedBitmap = bitmap
                
                // Extract EXIF date
                val exifStream = context.contentResolver.openInputStream(it)
                if (exifStream != null) {
                    val exif = ExifInterface(exifStream)
                    val dateStr = exif.getAttribute(ExifInterface.TAG_DATETIME)
                    mealDateTime = if (dateStr != null) {
                        SimpleDateFormat("yyyy:MM:dd HH:mm:ss", Locale.getDefault()).parse(dateStr)?.time ?: System.currentTimeMillis()
                    } else {
                        System.currentTimeMillis()
                    }
                    exifStream.close()
                }

                analyzeImage(bitmap, null, { isAnalyzing = it }, { data, error ->
                    if (data != null) {
                        currentMealData = data
                        showBottomSheet = true
                    } else {
                        Toast.makeText(context, "L\u0027IA n\u0027a pas pu identifier le repas.", Toast.LENGTH_LONG).show()
                    }
                }, coroutineScope)
            } catch (e: Exception) {
                Toast.makeText(context, "Erreur lors du chargement : ${e.message}", Toast.LENGTH_SHORT).show()
            }
        }
    }

    if (showBottomSheet && currentMealData != null) {
        ModalBottomSheet(
            onDismissRequest = { showBottomSheet = false },
            sheetState = sheetState,
            containerColor = MaterialTheme.colorScheme.surface,
            modifier = Modifier.fillMaxHeight(0.85f)
        ) {
            var mealType by remember { mutableStateOf("Déjeuner") }
            val calendar = Calendar.getInstance().apply { timeInMillis = mealDateTime }
            
            var editableName by remember { mutableStateOf(currentMealData!!.first) }
            var editableDesc by remember { mutableStateOf(currentMealData!!.second) }
            
            val editableCalories = currentMealData!!.third[0].toString()
            val editableCarbs = currentMealData!!.third[1].toString()
            val editableProtein = currentMealData!!.third[2].toString()
            val editableFat = currentMealData!!.third[3].toString()

            // Update local state if currentMealData changes (e.g. after resubmission)
            LaunchedEffect(currentMealData) {
                editableName = currentMealData!!.first
                editableDesc = currentMealData!!.second
            }

            Column(modifier = Modifier
                .padding(horizontal = 16.dp)
                .padding(bottom = 32.dp)
                .verticalScroll(rememberScrollState())
            ) {
                Text("Résumé du repas", style = MaterialTheme.typography.headlineSmall, fontWeight = FontWeight.Bold)
                Spacer(Modifier.height(16.dp))
                
                OutlinedTextField(
                    value = editableName, 
                    onValueChange = { editableName = it }, 
                    label = { Text("Nom du repas") }, 
                    modifier = Modifier.fillMaxWidth()
                )
                
                Row(modifier = Modifier.fillMaxWidth().padding(vertical = 8.dp), horizontalArrangement = Arrangement.spacedBy(8.dp)) {
                    OutlinedTextField(value = editableCalories, onValueChange = { }, label = { Text("Cal") }, modifier = Modifier.weight(1f), readOnly = true)
                    OutlinedTextField(value = editableCarbs, onValueChange = { }, label = { Text("Glu") }, modifier = Modifier.weight(1f), readOnly = true)
                    OutlinedTextField(value = editableProtein, onValueChange = { }, label = { Text("Pro") }, modifier = Modifier.weight(1f), readOnly = true)
                    OutlinedTextField(value = editableFat, onValueChange = { }, label = { Text("Lip") }, modifier = Modifier.weight(1f), readOnly = true)
                }

                OutlinedTextField(
                    value = editableDesc, 
                    onValueChange = { editableDesc = it }, 
                    label = { Text("Description / Précisions pour l\u0027IA") }, 
                    modifier = Modifier.fillMaxWidth(), 
                    minLines = 3
                )
                
                Button(
                    onClick = {
                        analyzeImage(capturedBitmap, "$editableName : $editableDesc", { isAnalyzing = it }, { data, error ->
                            if (data != null) {
                                currentMealData = data
                            } else {
                                Toast.makeText(context, "Analyse échouée : $error", Toast.LENGTH_LONG).show()
                            }
                        }, coroutineScope)
                    },
                    modifier = Modifier.fillMaxWidth().padding(top = 8.dp),
                    colors = ButtonDefaults.buttonColors(containerColor = MaterialTheme.colorScheme.secondary),
                    enabled = !isAnalyzing
                ) {
                    Icon(Icons.Default.Refresh, null)
                    Spacer(Modifier.width(8.dp))
                    Text("Ressoumettre à l\u0027IA")
                }

                Spacer(Modifier.height(16.dp))
                Text("Catégorie :", fontWeight = FontWeight.Bold)
                Row(modifier = Modifier.fillMaxWidth(), horizontalArrangement = Arrangement.SpaceBetween) {
                    listOf("Déjeuner", "Dîner", "Souper", "Collation").forEach { type ->
                        FilterChip(selected = mealType == type, onClick = { mealType = type }, label = { Text(type) })
                    }
                }

                Spacer(Modifier.height(16.dp))
                Button(onClick = {
                    DatePickerDialog(context, { _, y, m, d ->
                        calendar.set(y, m, d)
                        TimePickerDialog(context, { _, hh, mm ->
                            calendar.set(Calendar.HOUR_OF_DAY, hh)
                            calendar.set(Calendar.MINUTE, mm)
                            mealDateTime = calendar.timeInMillis
                        }, calendar.get(Calendar.HOUR_OF_DAY), calendar.get(Calendar.MINUTE), true).show()
                    }, calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH)).show()
                }, modifier = Modifier.fillMaxWidth(), colors = ButtonDefaults.buttonColors(containerColor = MaterialTheme.colorScheme.secondaryContainer, contentColor = MaterialTheme.colorScheme.onSecondaryContainer)) {
                    Icon(Icons.Default.DateRange, null)
                    Spacer(Modifier.width(8.dp))
                    Text("Date/Heure: " + SimpleDateFormat("dd/MM HH:mm", Locale.getDefault()).format(Date(mealDateTime)))
                }

                Spacer(Modifier.height(24.dp))
                Button(
                    onClick = {
                        coroutineScope.launch {
                            dao.insertMeal(Meal(
                                date = mealDateTime,
                                name = editableName,
                                analysisText = editableDesc,
                                totalCalories = editableCalories.toIntOrNull() ?: 0,
                                carbs = editableCarbs.toIntOrNull() ?: 0,
                                protein = editableProtein.toIntOrNull() ?: 0,
                                fat = editableFat.toIntOrNull() ?: 0,
                                type = mealType
                            ))
                            showBottomSheet = false
                            capturedBitmap = null
                            Toast.makeText(context, "Repas enregistré !", Toast.LENGTH_SHORT).show()
                        }
                    },
                    modifier = Modifier.fillMaxWidth().height(56.dp),
                    enabled = !isAnalyzing
                ) {
                    Icon(Icons.Default.Check, null)
                    Spacer(Modifier.width(8.dp))
                    Text("Confirmer et Enregistrer")
                }
            }
        }
    }

    Column(modifier = Modifier.fillMaxSize().padding(16.dp).verticalScroll(rememberScrollState())) {
        Text("Scan-Wich Analysis", style = MaterialTheme.typography.headlineMedium)
        Spacer(Modifier.height(16.dp))
        Row(modifier = Modifier.fillMaxWidth(), horizontalArrangement = Arrangement.SpaceEvenly) {
            Button(onClick = { 
                if (ContextCompat.checkSelfPermission(context, Manifest.permission.CAMERA) == PackageManager.PERMISSION_GRANTED) {
                    cameraLauncher.launch(null)
                } else {
                    permissionLauncher.launch(Manifest.permission.CAMERA)
                }
            }) { Icon(Icons.Default.Add, null); Text(" Caméra") }
            Button(onClick = { galleryLauncher.launch("image/*") }) { Icon(Icons.Default.Share, null); Text(" Galerie") }
        }

        capturedBitmap?.let {
            Spacer(Modifier.height(16.dp))
            Text("Image sélectionnée :", style = MaterialTheme.typography.labelMedium)
            Image(
                bitmap = it.asImageBitmap(),
                contentDescription = null,
                modifier = Modifier.fillMaxWidth().height(250.dp).clip(MaterialTheme.shapes.medium).background(Color.Gray)
            )
        }

        if (isAnalyzing) {
            Spacer(Modifier.height(32.dp))
            Column(horizontalAlignment = Alignment.CenterHorizontally, modifier = Modifier.fillMaxWidth()) {
                CircularProgressIndicator()
                Text("Analyse par l\u0027IA en cours...", modifier = Modifier.padding(top = 8.dp))
            }
        }

        Spacer(Modifier.height(32.dp))
        HorizontalDivider()
        Spacer(Modifier.height(16.dp))
        Text("Analyse par texte", style = MaterialTheme.typography.titleMedium)
        
        OutlinedTextField(
            value = manualMealName,
            onValueChange = { manualMealName = it },
            label = { Text("Qu\u0027avez-vous mangé ?") },
            placeholder = { Text("ex: Un sandwich au poulet et une pomme") },
            modifier = Modifier.fillMaxWidth()
        )
        
        Row(modifier = Modifier.fillMaxWidth().padding(top = 8.dp), horizontalArrangement = Arrangement.spacedBy(8.dp)) {
            Button(
                onClick = {
                    analyzeImage(null, manualMealName, { isAnalyzing = it }, { data, error ->
                        if (data != null) {
                            currentMealData = data
                            showBottomSheet = true
                        } else {
                            Toast.makeText(context, "Erreur AI: $error", Toast.LENGTH_LONG).show()
                        }
                    }, coroutineScope)
                },
                enabled = manualMealName.isNotBlank() && !isAnalyzing,
                modifier = Modifier.weight(1f)
            ) {
                Icon(Icons.Default.Refresh, null)
                Spacer(Modifier.width(4.dp))
                Text("Analyser via IA")
            }
            
            OutlinedButton(
                onClick = {
                    currentMealData = Triple(manualMealName, "Ajout manuel", listOf(0, 0, 0, 0))
                    mealDateTime = System.currentTimeMillis()
                    showBottomSheet = true
                },
                enabled = manualMealName.isNotBlank() && !isAnalyzing,
                modifier = Modifier.weight(1f)
            ) {
                Text("Direct (0 kcal)")
            }
        }
    }
}

@Composable
fun DailyGoalChart(label: String, current: Int, target: Int, color: Color) {
    val progress = if (target > 0) current.toFloat() / target else 0f
    Column(horizontalAlignment = Alignment.CenterHorizontally, modifier = Modifier.width(80.dp)) {
        Box(contentAlignment = Alignment.Center, modifier = Modifier.size(60.dp)) {
            CircularProgressIndicator(
                progress = { 1f },
                modifier = Modifier.fillMaxSize(),
                color = color.copy(alpha = 0.2f),
                strokeWidth = 6.dp,
                strokeCap = StrokeCap.Round,
            )
            CircularProgressIndicator(
                progress = { progress.coerceAtMost(1f) },
                modifier = Modifier.fillMaxSize(),
                color = color,
                strokeWidth = 6.dp,
                strokeCap = StrokeCap.Round,
            )
            Text(
                text = "${(progress * 100).toInt()}%",
                style = MaterialTheme.typography.labelSmall,
                fontWeight = FontWeight.Bold
            )
        }
        Spacer(Modifier.height(4.dp))
        Text(text = label, style = MaterialTheme.typography.labelMedium)
        Text(text = "$current / $target", style = MaterialTheme.typography.labelSmall, color = Color.Gray)
    }
}

@Composable
fun HistoryScreen(dao: AppDao, prefs: SharedPreferences) {
    var selectedDate by remember { mutableStateOf(Calendar.getInstance()) }
    val startOfDay = selectedDate.clone() as Calendar
    startOfDay.set(Calendar.HOUR_OF_DAY, 0); startOfDay.set(Calendar.MINUTE, 0); startOfDay.set(Calendar.SECOND, 0); startOfDay.set(Calendar.MILLISECOND, 0)
    val endOfDay = startOfDay.clone() as Calendar
    endOfDay.add(Calendar.DAY_OF_MONTH, 1)

    val meals by dao.getMealsForDay(startOfDay.timeInMillis, endOfDay.timeInMillis).collectAsState(initial = emptyList())
    val sports by dao.getSportsForDay(startOfDay.timeInMillis, endOfDay.timeInMillis).collectAsState(initial = emptyList())
    val glycemiaList by dao.getGlycemiaForDay(startOfDay.timeInMillis, endOfDay.timeInMillis).collectAsState(initial = emptyList())
    
    val isDiabetic = prefs.getBoolean("is_diabetic", false)
    var selectedMealForDetail by remember { mutableStateOf<Meal?>(null) }
    val context = LocalContext.current
    val coroutineScope = rememberCoroutineScope()

    val tCal = prefs.getString("target_calories", "2000")?.toIntOrNull() ?: 2000
    val tCarb = prefs.getString("target_carbs", "250")?.toIntOrNull() ?: 250
    val tProt = prefs.getString("target_protein", "100")?.toIntOrNull() ?: 100
    val tFat = prefs.getString("target_fat", "60")?.toIntOrNull() ?: 60

    if (selectedMealForDetail != null) {
        AlertDialog(
            onDismissRequest = { selectedMealForDetail = null },
            title = { Text(selectedMealForDetail!!.name) },
            text = {
                Column(modifier = Modifier
                    .fillMaxWidth()
                    .heightIn(max = 450.dp)
                    .verticalScroll(rememberScrollState())
                ) {
                    Text("Type: ${selectedMealForDetail!!.type}", fontWeight = FontWeight.Bold)
                    Text("Calories: ${selectedMealForDetail!!.totalCalories} kcal")
                    Text("Macro: G ${selectedMealForDetail!!.carbs}g | P ${selectedMealForDetail!!.protein}g | L ${selectedMealForDetail!!.fat}g")
                    Text("Heure: ${SimpleDateFormat("HH:mm", Locale.getDefault()).format(Date(selectedMealForDetail!!.date))}")
                    Spacer(Modifier.height(8.dp))
                    Text("Analyse complète :", style = MaterialTheme.typography.labelMedium)
                    Text(selectedMealForDetail!!.analysisText)
                }
            },
            confirmButton = {
                TextButton(onClick = { selectedMealForDetail = null }) { Text("Fermer") }
            }
        )
    }

    Column(modifier = Modifier.fillMaxSize().padding(16.dp)) {
        Row(verticalAlignment = Alignment.CenterVertically, modifier = Modifier.fillMaxWidth(), horizontalArrangement = Arrangement.SpaceBetween) {
            IconButton(onClick = { 
                val newDate = selectedDate.clone() as Calendar
                newDate.add(Calendar.DAY_OF_MONTH, -1)
                selectedDate = newDate
            }) { Icon(Icons.AutoMirrored.Filled.KeyboardArrowLeft, null) }
            
            Text(
                text = SimpleDateFormat("dd MMMM yyyy", Locale.getDefault()).format(selectedDate.time),
                style = MaterialTheme.typography.titleLarge,
                modifier = Modifier.clickable {
                    DatePickerDialog(context, { _, y, m, d ->
                        val newDate = Calendar.getInstance()
                        newDate.set(y, m, d)
                        selectedDate = newDate
                    }, selectedDate.get(Calendar.YEAR), selectedDate.get(Calendar.MONTH), selectedDate.get(Calendar.DAY_OF_MONTH)).show()
                }
            )

            IconButton(onClick = { 
                val newDate = selectedDate.clone() as Calendar
                newDate.add(Calendar.DAY_OF_MONTH, 1)
                selectedDate = newDate
            }) { Icon(Icons.AutoMirrored.Filled.KeyboardArrowRight, null) }
        }

        Spacer(Modifier.height(16.dp))
        
        val totalIn = meals.sumOf { it.totalCalories }
        val totalOut = sports.sumOf { it.calories?.toInt() ?: 0 }
        val totalCarbs = meals.sumOf { it.carbs }
        val totalProt = meals.sumOf { it.protein }
        val totalFat = meals.sumOf { it.fat }

        Card(modifier = Modifier.fillMaxWidth(), colors = CardDefaults.cardColors(containerColor = MaterialTheme.colorScheme.surfaceVariant)) {
            Column(modifier = Modifier.padding(16.dp)) {
                Text("Objectifs Quotidiens", style = MaterialTheme.typography.titleMedium, fontWeight = FontWeight.Bold)
                Spacer(Modifier.height(12.dp))
                Row(modifier = Modifier.fillMaxWidth(), horizontalArrangement = Arrangement.SpaceEvenly) {
                    DailyGoalChart("Calories", totalIn, tCal, Color(0xFF4CAF50))
                    DailyGoalChart("Glucides", totalCarbs, tCarb, Color(0xFF2196F3))
                    DailyGoalChart("Protéines", totalProt, tProt, Color(0xFFFF9800))
                    DailyGoalChart("Lipides", totalFat, tFat, Color(0xFFE91E63))
                }
                Spacer(Modifier.height(12.dp))
                HorizontalDivider()
                Spacer(Modifier.height(8.dp))
                Row(modifier = Modifier.fillMaxWidth(), horizontalArrangement = Arrangement.SpaceBetween) {
                    Text("Sport (Brûlées):")
                    Text("$totalOut kcal", fontWeight = FontWeight.Bold, color = MaterialTheme.colorScheme.primary)
                }
            }
        }

        Spacer(Modifier.height(16.dp))
        
        LazyColumn(Modifier.weight(1f)) {
            val mealCategories = listOf("Déjeuner", "Dîner", "Souper", "Collation")
            mealCategories.forEach { category ->
                val categoryMeals = meals.filter { it.type == category }
                val beforeGly = glycemiaList.find { it.moment == "Avant $category" }
                val afterGly = glycemiaList.find { it.moment == "Après $category" }

                if (categoryMeals.isNotEmpty() || beforeGly != null || afterGly != null) {
                    item {
                        Text(category, style = MaterialTheme.typography.titleMedium, color = MaterialTheme.colorScheme.secondary, modifier = Modifier.padding(top = 12.dp, bottom = 4.dp))
                    }
                    
                    if (isDiabetic && beforeGly != null) {
                        item {
                            Surface(color = Color.Blue.copy(alpha = 0.1f), modifier = Modifier.fillMaxWidth().clip(MaterialTheme.shapes.small)) {
                                Row(verticalAlignment = Alignment.CenterVertically, modifier = Modifier.padding(8.dp)) {
                                    Text("🩸 Glycémie Avant: ${beforeGly.value} mmol/L", modifier = Modifier.weight(1f), style = MaterialTheme.typography.bodyMedium)
                                    IconButton(onClick = { 
                                        coroutineScope.launch {
                                            dao.deleteGlycemia(beforeGly)
                                            Toast.makeText(context, "Glycémie supprimée", Toast.LENGTH_SHORT).show()
                                        }
                                    }) { Icon(Icons.Default.Delete, null, modifier = Modifier.size(20.dp)) }
                                }
                            }
                        }
                    }

                    items(categoryMeals) { meal ->
                        ListItem(
                            headlineContent = { Text(meal.name) },
                            supportingContent = { Text("${meal.totalCalories} kcal - G:${meal.carbs}g P:${meal.protein}g L:${meal.fat}g") },
                            trailingContent = { 
                                IconButton(onClick = { 
                                    coroutineScope.launch {
                                        dao.deleteMeal(meal)
                                        Toast.makeText(context, "Repas supprimé", Toast.LENGTH_SHORT).show()
                                    }
                                }) { Icon(Icons.Default.Delete, null) } 
                            },
                            modifier = Modifier.clickable { selectedMealForDetail = meal }
                        )
                    }

                    if (isDiabetic && afterGly != null) {
                        item {
                            Surface(color = Color.Green.copy(alpha = 0.1f), modifier = Modifier.fillMaxWidth().clip(MaterialTheme.shapes.small)) {
                                Row(verticalAlignment = Alignment.CenterVertically, modifier = Modifier.padding(8.dp)) {
                                    Text("🩸 Glycémie Après: ${afterGly.value} mmol/L", modifier = Modifier.weight(1f), style = MaterialTheme.typography.bodyMedium)
                                    IconButton(onClick = { 
                                        coroutineScope.launch {
                                            dao.deleteGlycemia(afterGly)
                                            Toast.makeText(context, "Glycémie supprimée", Toast.LENGTH_SHORT).show()
                                        }
                                    }) { Icon(Icons.Default.Delete, null, modifier = Modifier.size(20.dp)) }
                                }
                            }
                        }
                    }
                }
            }
            
            if (sports.isNotEmpty()) {
                item {
                    Text("Activités Sportives", style = MaterialTheme.typography.titleMedium, color = MaterialTheme.colorScheme.primary, modifier = Modifier.padding(top = 16.dp, bottom = 4.dp))
                }
                items(sports) { sport ->
                    ListItem(
                        headlineContent = { Text(sport.name) },
                        supportingContent = { Text("${sport.type} - ${sport.calories?.toInt()} kcal brûlées") },
                        trailingContent = { Icon(Icons.Default.Check, tint = Color.Green, contentDescription = null) }
                    )
                }
            }
        }
    }
}

private fun analyzeImage(
    bitmap: Bitmap?, 
    textDescription: String?,
    setAnalyzing: (Boolean) -> Unit, 
    onResult: (Triple<String, String, List<Int>>?, String?) -> Unit,
    scope: CoroutineScope
) {
    setAnalyzing(true)
    
    var base64: String? = null
    if (bitmap != null) {
        val outputStream = ByteArrayOutputStream()
        bitmap.compress(Bitmap.CompressFormat.JPEG, 80, outputStream)
        base64 = Base64.encodeToString(outputStream.toByteArray(), Base64.NO_WRAP)
    }
    
    val prompt = if (bitmap != null && textDescription == null) {
        "Analyze this food image in FRENCH. Provide ONLY: 1. Name, 2. Summary description in FRENCH, 3. Macros. Format EXACTLY as: {\"name\": \"...\", \"description\": \"...\", \"calories\": int, \"carbs\": int, \"protein\": int, \"fat\": int}"
    } else if (bitmap != null && textDescription != null) {
        "Analyze this food image in FRENCH, taking into account these corrections or details: \u0027$textDescription\u0027. Provide ONLY: 1. Name, 2. Summary description in FRENCH, 3. Macros. Format EXACTLY as: {\"name\": \"...\", \"description\": \"...\", \"calories\": int, \"carbs\": int, \"protein\": int, \"fat\": int}"
    } else {
        "Analyze this meal description in FRENCH: \u0027$textDescription\u0027. Estimate the macros. Provide ONLY a JSON object. Format EXACTLY as: {\"name\": \"...\", \"description\": \"...\", \"calories\": int, \"carbs\": int, \"protein\": int, \"fat\": int}"
    }
    
    scope.launch {
        try {
            val response = ApiClient.n8nApi.analyzeMeal(
                apiKey = BuildConfig.N8N_API_KEY,
                request = N8nMealRequest(
                    imageBase64 = base64, 
                    mealName = textDescription, 
                    prompt = prompt
                )
            )
            val responseStr = response.string()
            
            val jsonStartIndex = responseStr.indexOf("{")
            val jsonEndIndex = responseStr.lastIndexOf("}") + 1
            
            if (jsonStartIndex != -1 && jsonEndIndex > jsonStartIndex) {
                try {
                    val jsonPart = responseStr.substring(jsonStartIndex, jsonEndIndex)
                    val json = JSONObject(jsonPart)
                    onResult(Triple(
                        json.optString("name", textDescription ?: "Repas"),
                        json.optString("description", "Analyse réussie"),
                        listOf(
                            json.optInt("calories", 0),
                            json.optInt("carbs", 0),
                            json.optInt("protein", 0),
                            json.optInt("fat", 0)
                        )
                    ), null)
                    return@launch
                } catch (_: Exception) { }
            }
            onResult(null, "Format de réponse inconnu")
        } catch (e: Exception) {
            onResult(null, e.localizedMessage ?: "Erreur réseau")
        } finally {
            setAnalyzing(false)
        }
    }
}

private fun syncStravaActivities(dao: AppDao, prefs: SharedPreferences, scope: CoroutineScope, context: Context) {
    scope.launch {
        val token = ApiClient.getValidStravaToken(prefs)
        if (token == null) {
            Toast.makeText(context, "Veuillez connecter Strava dans les paramètres", Toast.LENGTH_LONG).show()
            return@launch
        }
        try {
            val activities = ApiClient.stravaApi.getActivities("Bearer $token")
            val weightKg = prefs.getString("weight_kg", "70")?.toDoubleOrNull() ?: 70.0
            
            val sportActivities = activities.map {
                SportActivity(
                    id = it.id,
                    name = it.name,
                    type = it.type,
                    distance = it.distance,
                    movingTime = it.movingTime,
                    calories = it.calories ?: ApiClient.estimateCaloriesFromDb(
                        SportActivity(it.id, it.name, it.type, it.distance, it.movingTime, null, 0L), 
                        weightKg
                    ).toFloat(),
                    date = ApiClient.parseStravaDate(it.startDate)
                )
            }
            dao.insertSports(sportActivities)
            Toast.makeText(context, "${activities.size} activités synchronisées !", Toast.LENGTH_SHORT).show()
        } catch (e: Exception) {
            Log.e("StravaSync", "Error: ${e.message}")
            Toast.makeText(context, "Erreur de synchronisation Strava", Toast.LENGTH_SHORT).show()
        }
    }
}

@Composable
fun SportScreen(dao: AppDao, prefs: SharedPreferences) {
    val sports by dao.getAllSports().collectAsState(initial = emptyList())
    val coroutineScope = rememberCoroutineScope()
    val context = LocalContext.current

    Column(modifier = Modifier.fillMaxSize().padding(16.dp)) {
        Text("Activités Sportives", style = MaterialTheme.typography.headlineMedium)
        
        Button(
            onClick = { syncStravaActivities(dao, prefs, coroutineScope, context) },
            modifier = Modifier.fillMaxWidth().padding(vertical = 8.dp)
        ) {
            Icon(Icons.Default.Refresh, null)
            Spacer(Modifier.width(8.dp))
            Text("Synchroniser Strava")
        }

        Spacer(Modifier.height(8.dp))
        
        LazyColumn(Modifier.weight(1f)) {
            items(sports) { activity ->
                Card(modifier = Modifier.fillMaxWidth().padding(vertical = 4.dp)) {
                    Column(modifier = Modifier.padding(16.dp)) {
                        Row(horizontalArrangement = Arrangement.SpaceBetween, modifier = Modifier.fillMaxWidth()) {
                            Text(activity.name, fontWeight = FontWeight.Bold, modifier = Modifier.weight(1f))
                            Text(SimpleDateFormat("dd/MM HH:mm", Locale.getDefault()).format(Date(activity.date)), style = MaterialTheme.typography.bodySmall)
                        }
                        Text("${activity.type} - ${(activity.distance / 1000).format(2)} km")
                        Text("${activity.calories?.toInt() ?: 0} kcal brûlées", color = MaterialTheme.colorScheme.primary, fontWeight = FontWeight.Bold)
                    }
                }
            }
        }
    }
}

fun Float.format(digits: Int) = "%.${digits}f".format(this)

@Composable
fun SettingsScreen(prefs: SharedPreferences, onLogout: () -> Unit) {
    var isEditing by remember { mutableStateOf(false) }
    val context = LocalContext.current
    
    var stravaClientId by remember { mutableStateOf(prefs.getString("strava_client_id", "") ?: "") }
    var stravaClientSecret by remember { mutableStateOf(prefs.getString("strava_client_secret", "") ?: "") }
    val isStravaConnected = prefs.contains("strava_token")

    if (isEditing) { SetupScreen(prefs) { isEditing = false } } else {
        val targetCals = prefs.getString("target_calories", "0")
        Column(modifier = Modifier.fillMaxSize().padding(16.dp).verticalScroll(rememberScrollState())) {
            Text("Mon Profil", style = MaterialTheme.typography.headlineMedium)
            Spacer(Modifier.height(16.dp))
            Card(modifier = Modifier.fillMaxWidth()) {
                Column(modifier = Modifier.padding(16.dp)) {
                    ProfileItem("Objectif", prefs.getString("goal", "") ?: "")
                    ProfileItem("Cible Calorique", "$targetCals kcal")
                    ProfileItem("Diabétique", if (prefs.getBoolean("is_diabetic", false)) "Oui" else "Non")
                }
            }
            Spacer(Modifier.height(8.dp))
            Button(onClick = { isEditing = true }, modifier = Modifier.fillMaxWidth()) { Icon(Icons.Default.Edit, null); Text(" Modifier le profil") }
            
            Spacer(Modifier.height(32.dp))
            Text("Configuration Strava", style = MaterialTheme.typography.titleMedium)
            Spacer(Modifier.height(8.dp))
            
            OutlinedTextField(
                value = stravaClientId,
                onValueChange = { stravaClientId = it; prefs.edit { putString("strava_client_id", it) } },
                label = { Text("Strava Client ID") },
                modifier = Modifier.fillMaxWidth()
            )
            OutlinedTextField(
                value = stravaClientSecret,
                onValueChange = { stravaClientSecret = it; prefs.edit { putString("strava_client_secret", it) } },
                label = { Text("Strava Client Secret") },
                modifier = Modifier.fillMaxWidth()
            )
            
            Spacer(Modifier.height(8.dp))
            
            if (!isStravaConnected) {
                Button(
                    onClick = {
                        if (stravaClientId.isBlank() || stravaClientSecret.isBlank()) {
                            Toast.makeText(context, "Entrez votre ID et Secret Client", Toast.LENGTH_SHORT).show()
                        } else {
                            val intent = Intent(Intent.ACTION_VIEW, Uri.parse(
                                "https://www.strava.com/oauth/mobile/authorize?client_id=$stravaClientId&redirect_uri=coloricam://localhost&response_type=code&approval_prompt=auto&scope=read,activity:read_all"
                            ))
                            context.startActivity(intent)
                        }
                    },
                    modifier = Modifier.fillMaxWidth()
                ) {
                    Text("Connecter Strava")
                }
            } else {
                OutlinedButton(
                    onClick = { 
                        prefs.edit { 
                            remove("strava_token")
                            remove("strava_refresh_token")
                        }
                        Toast.makeText(context, "Strava déconnecté", Toast.LENGTH_SHORT).show()
                    },
                    modifier = Modifier.fillMaxWidth()
                ) {
                    Text("Déconnecter Strava (Connecté)")
                }
            }

            Spacer(Modifier.height(32.dp))
            Button(onClick = onLogout, colors = ButtonDefaults.buttonColors(containerColor = MaterialTheme.colorScheme.error), modifier = Modifier.fillMaxWidth()) { Text("Déconnexion") }
        }
    }
}

@Composable
fun ProfileItem(label: String, value: String) {
    Row(modifier = Modifier.fillMaxWidth().padding(vertical = 4.dp), horizontalArrangement = Arrangement.SpaceBetween) {
        Text(label, fontWeight = FontWeight.Bold); Text(value)
    }
}

@Composable
fun GlycemiaScreen(dao: AppDao) {
    val coroutineScope = rememberCoroutineScope()
    val context = LocalContext.current
    var glycemiaValue by remember { mutableStateOf("") }
    var moment by remember { mutableStateOf("Avant Déjeuner") }
    var selectedDateTime by remember { mutableLongStateOf(System.currentTimeMillis()) }
    val calendar = Calendar.getInstance().apply { timeInMillis = selectedDateTime }

    val moments = listOf(
        "Avant Déjeuner", "Après Déjeuner",
        "Avant Dîner", "Après Dîner",
        "Avant Souper", "Après Souper"
    )

    Column(modifier = Modifier.fillMaxSize().padding(16.dp).verticalScroll(rememberScrollState())) {
        Text("Suivi de Glycémie", style = MaterialTheme.typography.headlineMedium)
        Spacer(Modifier.height(16.dp))

        OutlinedTextField(
            value = glycemiaValue,
            onValueChange = { glycemiaValue = it },
            label = { Text("Valeur (mmol/L)") },
            keyboardOptions = KeyboardOptions(keyboardType = KeyboardType.Number),
            modifier = Modifier.fillMaxWidth()
        )

        Spacer(Modifier.height(16.dp))
        Text("Moment :", style = MaterialTheme.typography.titleMedium)
        moments.forEach { m ->
            Row(verticalAlignment = Alignment.CenterVertically, modifier = Modifier.clickable { moment = m }) {
                RadioButton(selected = moment == m, onClick = { moment = m })
                Text(m)
            }
        }

        Spacer(Modifier.height(16.dp))
        Button(onClick = {
            DatePickerDialog(context, { _, y, m, d ->
                calendar.set(y, m, d)
                TimePickerDialog(context, { _, hh, mm ->
                    calendar.set(Calendar.HOUR_OF_DAY, hh)
                    calendar.set(Calendar.MINUTE, mm)
                    selectedDateTime = calendar.timeInMillis
                }, calendar.get(Calendar.HOUR_OF_DAY), calendar.get(Calendar.MINUTE), true).show()
            }, calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH)).show()
        }, modifier = Modifier.fillMaxWidth()) {
            Text("Date/Heure: " + SimpleDateFormat("dd/MM HH:mm", Locale.getDefault()).format(Date(selectedDateTime)))
        }

        Spacer(Modifier.height(24.dp))
        Button(
            onClick = {
                val value = glycemiaValue.toDoubleOrNull()
                if (value != null) {
                    coroutineScope.launch {
                        dao.insertGlycemia(Glycemia(date = selectedDateTime, value = value, moment = moment))
                        glycemiaValue = ""
                        Toast.makeText(context, "Glycémie enregistrée !", Toast.LENGTH_SHORT).show()
                    }
                }
            },
            enabled = glycemiaValue.isNotBlank(),
            modifier = Modifier.fillMaxWidth()
        ) {
            Text("Enregistrer")
        }
    }
}
