package com.anypreta.bountyboard.ui.nav

import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.automirrored.outlined.ListAlt
import androidx.compose.material.icons.outlined.AccountCircle
import androidx.compose.material.icons.outlined.AdminPanelSettings
import androidx.compose.material.icons.outlined.Assignment
import androidx.compose.material.icons.outlined.AutoAwesomeMosaic
import androidx.compose.material.icons.outlined.Business
import androidx.compose.material.icons.outlined.Chat
import androidx.compose.material.icons.outlined.EmojiEvents
import androidx.compose.material.icons.outlined.Groups
import androidx.compose.material.icons.outlined.Insights
import androidx.compose.material.icons.outlined.Notifications
import androidx.compose.material.icons.outlined.RateReview
import androidx.compose.material.icons.outlined.Settings
import androidx.compose.material.icons.outlined.Storefront
import androidx.compose.ui.graphics.vector.ImageVector
import com.anypreta.bountyboard.data.model.User

object Routes {
    const val BOARD = "board"
    const val MY_TASKS = "my_tasks"
    const val DEV_METRICS = "dev_metrics"
    const val LEADERBOARD = "leaderboard"

    const val CONSULTANT_BOARD = "consultant_board"
    const val REVIEWS = "reviews"
    const val POOL = "pool"
    const val CONSULTANT_METRICS = "consultant_metrics"

    const val ADMIN_USERS = "admin_users"
    const val ADMIN_CUSTOMERS = "admin_customers"
    const val ADMIN_SETTINGS = "admin_settings"
    const val ADMIN_AUDIT = "admin_audit"
    const val ADMIN_STATUS = "admin_status"

    const val MESSAGES = "messages"
    const val NOTIFICATIONS = "notifications"
    const val PROFILE = "profile"

    const val TASK = "task" // task/{id}
    const val CONVERSATION = "conversation" // conversation/{id}
    const val CUSTOMER_EDIT = "customer_edit" // customer_edit/{id}  (id=new for create)
    const val PUBLIC_PROFILE = "public_profile" // public_profile/{id}

    fun task(id: String) = "$TASK/$id"
    fun conversation(id: String) = "$CONVERSATION/$id"
    fun customerEdit(id: String) = "$CUSTOMER_EDIT/$id"
    fun publicProfile(id: String) = "$PUBLIC_PROFILE/$id"
}

data class NavDest(val route: String, val label: String, val icon: ImageVector)
data class NavSection(val title: String, val dests: List<NavDest>)

/** Build the role-scoped navigation sections for the drawer. */
fun navSectionsFor(user: User?): List<NavSection> {
    if (user == null) return emptyList()
    val sections = mutableListOf<NavSection>()
    val roles = user.roles

    if (roles.developer) {
        sections += NavSection(
            "Developer",
            listOf(
                NavDest(Routes.BOARD, "Bounty Board", Icons.Outlined.Storefront),
                NavDest(Routes.MY_TASKS, "My Tasks", Icons.Outlined.Assignment),
                NavDest(Routes.DEV_METRICS, "Metrics", Icons.Outlined.Insights),
            ),
        )
    }
    if (roles.consultant) {
        sections += NavSection(
            "Consultant",
            listOf(
                NavDest(Routes.CONSULTANT_BOARD, "Atomization", Icons.Outlined.AutoAwesomeMosaic),
                NavDest(Routes.REVIEWS, "Reviews", Icons.Outlined.RateReview),
                NavDest(Routes.POOL, "Developer Pool", Icons.Outlined.Groups),
                NavDest(Routes.CONSULTANT_METRICS, "Metrics", Icons.Outlined.Insights),
            ),
        )
    }
    if (roles.admin) {
        sections += NavSection(
            "Admin",
            listOf(
                NavDest(Routes.ADMIN_USERS, "Users", Icons.Outlined.AdminPanelSettings),
                NavDest(Routes.ADMIN_CUSTOMERS, "Projects", Icons.Outlined.Business),
                NavDest(Routes.ADMIN_STATUS, "Service Status", Icons.Outlined.Insights),
                NavDest(Routes.ADMIN_AUDIT, "Audit Log", Icons.AutoMirrored.Outlined.ListAlt),
                NavDest(Routes.ADMIN_SETTINGS, "Settings", Icons.Outlined.Settings),
            ),
        )
    }
    sections += NavSection(
        "Workspace",
        listOf(
            NavDest(Routes.MESSAGES, "Messages", Icons.Outlined.Chat),
            NavDest(Routes.LEADERBOARD, "Leaderboard", Icons.Outlined.EmojiEvents),
            NavDest(Routes.NOTIFICATIONS, "Notifications", Icons.Outlined.Notifications),
            NavDest(Routes.PROFILE, "Profile", Icons.Outlined.AccountCircle),
        ),
    )
    return sections
}

/** The landing screen for a user, by highest-privilege role. */
fun homeRouteFor(user: User?): String = when {
    user == null -> Routes.BOARD
    user.roles.developer -> Routes.BOARD
    user.roles.consultant -> Routes.CONSULTANT_BOARD
    user.roles.admin -> Routes.ADMIN_USERS
    else -> Routes.MESSAGES
}
