package com.example.hoophubskeleton

import android.content.Intent
import android.os.Bundle
import android.util.Log
import androidx.appcompat.app.AppCompatActivity
import androidx.navigation.NavController
import androidx.navigation.fragment.NavHostFragment
import androidx.navigation.ui.setupActionBarWithNavController
import com.google.firebase.auth.FirebaseAuth

class AuthHostActivity : AppCompatActivity() {

    private lateinit var navController: NavController

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        // Check if the user is already logged in
        if (FirebaseAuth.getInstance().currentUser != null) {
            // User is logged in, navigate to MainActivity
            val intent = Intent(this, MainActivity::class.java)
            intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK
            startActivity(intent)
            finish() // Close AuthHostActivity
            return
        }

        // If user is not logged in, set the content view to show login/signup fragments
        setContentView(R.layout.activity_auth_host)

        // Set up the NavController for the navigation graph
        val navHostFragment =
            supportFragmentManager.findFragmentById(R.id.nav_host_fragment) as NavHostFragment
        Log.d("AuthHostActivity", "NavHostFragment: $navHostFragment")
        Log.d("AuthHostActivity", "NavController: ${navHostFragment.navController}")
    }


    override fun onSupportNavigateUp(): Boolean {
        // Handle navigation when the user presses the Up button
        return navController.navigateUp() || super.onSupportNavigateUp()
    }
}
