package com.fabledsword.minstrel.shared.widgets

import androidx.compose.foundation.layout.Row
import androidx.compose.material3.DropdownMenu
import androidx.compose.material3.DropdownMenuItem
import androidx.compose.material3.Icon
import androidx.compose.material3.IconButton
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.hilt.navigation.compose.hiltViewModel
import androidx.lifecycle.compose.collectAsStateWithLifecycle
import androidx.navigation.NavHostController
import com.composables.icons.lucide.House
import com.composables.icons.lucide.LibraryBig
import com.composables.icons.lucide.Lucide
import com.composables.icons.lucide.EllipsisVertical
import com.composables.icons.lucide.Search as SearchIcon
import com.fabledsword.minstrel.nav.Admin
import com.fabledsword.minstrel.nav.Discover
import com.fabledsword.minstrel.nav.Home
import com.fabledsword.minstrel.nav.Library
import com.fabledsword.minstrel.nav.Playlists
import com.fabledsword.minstrel.nav.Search as SearchRoute
import com.fabledsword.minstrel.nav.Settings as SettingsRoute

/**
 * The universal navigation surface for every shell screen — Flutter's
 * `MainAppBarActions`. There is no bottom nav, drawer, or rail; every
 * top-level AppBar carries this row, and the kebab covers the rest.
 *
 *   [Home] [Library] [Search] [ ⋮ Playlists / Discover / Settings / Admin ]
 *
 * The icon for the screen the user is currently on is suppressed so
 * the action set looks contextual. The "Admin" overflow item is
 * sourced internally from [AppBarActionsViewModel] so admins see it
 * everywhere without each call site having to plumb the flag.
 *
 * `currentRouteName` is a soft string match against the destination
 * route name (route classes serialize to their FQN by default in
 * nav-compose); pass `Home::class.qualifiedName` etc. from the screen.
 */
@Composable
fun MainAppBarActions(
    navController: NavHostController,
    currentRouteName: String?,
    viewModel: AppBarActionsViewModel = hiltViewModel(),
) {
    val isAdmin by viewModel.isAdmin.collectAsStateWithLifecycle()
    Row {
        if (currentRouteName != Home::class.qualifiedName) {
            IconButton(onClick = { navController.navigate(Home) }) {
                Icon(Lucide.House, contentDescription = "Home")
            }
        }
        if (currentRouteName != Library::class.qualifiedName) {
            IconButton(onClick = { navController.navigate(Library) }) {
                Icon(Lucide.LibraryBig, contentDescription = "Library")
            }
        }
        if (currentRouteName != SearchRoute::class.qualifiedName) {
            IconButton(onClick = { navController.navigate(SearchRoute) }) {
                Icon(Lucide.SearchIcon, contentDescription = "Search")
            }
        }
        OverflowMenu(navController = navController, isAdmin = isAdmin)
    }
}

@Composable
private fun OverflowMenu(navController: NavHostController, isAdmin: Boolean) {
    var expanded by remember { mutableStateOf(false) }
    IconButton(onClick = { expanded = true }) {
        Icon(Lucide.EllipsisVertical, contentDescription = "More")
    }
    DropdownMenu(expanded = expanded, onDismissRequest = { expanded = false }) {
        DropdownMenuItem(
            text = { Text("Playlists") },
            onClick = { expanded = false; navController.navigate(Playlists) },
        )
        DropdownMenuItem(
            text = { Text("Discover") },
            onClick = { expanded = false; navController.navigate(Discover) },
        )
        DropdownMenuItem(
            text = { Text("Settings") },
            onClick = { expanded = false; navController.navigate(SettingsRoute) },
        )
        if (isAdmin) {
            DropdownMenuItem(
                text = { Text("Admin") },
                onClick = { expanded = false; navController.navigate(Admin) },
            )
        }
    }
}
