package com.atridad.ascently

import android.content.Intent
import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.activity.enableEdgeToEdge
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.material3.Surface
import androidx.compose.runtime.*
import androidx.compose.ui.Modifier
import com.atridad.ascently.ui.AscentlyApp
import com.atridad.ascently.ui.theme.AscentlyTheme
import com.atridad.ascently.utils.MigrationManager

class MainActivity : ComponentActivity() {
    private var shortcutAction by mutableStateOf<String?>(null)
    private var lastUsedGymId by mutableStateOf<String?>(null)

    fun clearShortcutAction() {
        shortcutAction = null
        lastUsedGymId = null
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setTheme(R.style.Theme_Ascently)
        enableEdgeToEdge()

        // Perform migration from OpenClimb to Ascently if needed
        MigrationManager(this).migrateIfNeeded()

        shortcutAction = intent?.action
        lastUsedGymId = intent?.getStringExtra("LAST_USED_GYM_ID")

        setContent {
            AscentlyTheme {
                Surface(modifier = Modifier.fillMaxSize()) {
                    AscentlyApp(
                        shortcutAction = shortcutAction,
                        lastUsedGymId = lastUsedGymId,
                        onShortcutActionProcessed = { clearShortcutAction() },
                    )
                }
            }
        }
    }

    override fun onNewIntent(intent: Intent) {
        super.onNewIntent(intent)
        setIntent(intent)

        shortcutAction = intent.action
        lastUsedGymId = intent.getStringExtra("LAST_USED_GYM_ID")
    }
}
