package dev.zxq5.chatapp.android

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.foundation.layout.padding
import androidx.compose.material3.Scaffold
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.getValue
import androidx.compose.ui.Modifier
import androidx.lifecycle.viewmodel.compose.viewModel
import dev.zxq5.chatapp.android.core.data.TokenStore
import dev.zxq5.chatapp.android.data.repository.AuthRepository
import dev.zxq5.chatapp.android.data.repository.AuthState
import dev.zxq5.chatapp.android.data.repository.ChatRepository
import dev.zxq5.chatapp.android.data.repository.SettingsRepository
import dev.zxq5.chatapp.android.feature.auth.AuthViewModel
import dev.zxq5.chatapp.android.feature.chat.ChatViewModel
import dev.zxq5.chatapp.android.feature.chat.Screen
import dev.zxq5.chatapp.android.feature.settings.SettingsViewModel
import dev.zxq5.chatapp.android.feature.auth.AuthScreen
import dev.zxq5.chatapp.android.feature.chat.ChatScreen
import dev.zxq5.chatapp.android.feature.settings.SettingsScreen
import dev.zxq5.chatapp.android.ui.theme.ChatappTheme

class MainActivity : ComponentActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        
        val app = application as ChatApplication
        val tokenStore = app.tokenStore
        val authRepository = app.authRepository
        val chatRepository = app.chatRepository
        val settingsRepository = app.settingsRepository

        enableEdgeToEdge()
        setContent {
            ChatappTheme {
                val authViewModel: AuthViewModel = viewModel(factory = ViewModelFactory(authRepository))
                val chatViewModel: ChatViewModel = viewModel(factory = ViewModelFactory(chatRepository))
                val settingsViewModel: SettingsViewModel = viewModel(factory = ViewModelFactory(settingsRepository))
                
                val authState by authViewModel.authState.collectAsState()
                val currentScreen by chatViewModel.currentScreen.collectAsState()

                Scaffold(modifier = Modifier.fillMaxSize()) { innerPadding ->
                    androidx.compose.foundation.layout.Box(modifier = Modifier.padding(innerPadding)) {
                        when (authState) {
                            AuthState.Authenticated -> {
                                when (currentScreen) {
                                    Screen.CHAT -> ChatScreen(
                                        viewModel = chatViewModel,
                                        onNavigateToSettings = { chatViewModel.navigateTo(Screen.SETTINGS) },
                                        onLogout = { 
                                            authViewModel.logout()
                                            chatViewModel.clearChat()
                                        }
                                    )
                                    Screen.SETTINGS -> SettingsScreen(
                                        viewModel = settingsViewModel,
                                        onBack = { chatViewModel.navigateTo(Screen.CHAT) },
                                        onLogout = {
                                            authViewModel.logout()
                                            chatViewModel.clearChat()
                                        }
                                    )
                                }
                            }
                            AuthState.AwaitingTotp, AuthState.Unauthenticated -> {
                                AuthScreen(viewModel = authViewModel)
                            }
                        }
                    }
                }
            }
        }
    }
}
