<template>
    <div class="flex items-center justify-between">
        <h1 class="text-4xl font-bold uppercase text-primary-500">Liste des utilisateurs</h1>
    </div>

    <div v-if="auth.isAdmin" class="mt-7 border border-slate-200 mb-11">
        <div class="grid grid-cols-3 text-primary-700 gap-4 bg-slate-100 px-4 py-3 text-sm font-semibold uppercase tracking-wide">
            <div>Utilisateur</div>
            <div>Role</div>
            <div>Statut</div>
        </div>
        <div v-if="userList.length === 0" class="px-4 py-6 text-slate-400">
            Aucun utilisateur.
        </div>
        <template v-else>
            <div
                v-for="user in userList"
                :key="user.id"
                class="grid grid-cols-3 text-primary-700 gap-4 px-4 py-3 text-sm hover:bg-slate-50 cursor-pointer border-t border-slate-200 items-center"
                role="button"
                tabindex="0"
                @click="goToUser(user.id)"
                @keydown.enter="goToUser(user.id)"
            >
                <div>{{ user.username }}</div>
                <div>{{ getRoleLabels(user.roles) }}</div>
                <div>
                    <span
                        v-if="user.isLocked"
                        class="inline-block px-2 py-0.5 text-xs font-semibold rounded bg-red-100 text-red-700"
                    >Verrouillé</span>
                    <span
                        v-else
                        class="inline-block px-2 py-0.5 text-xs font-semibold rounded bg-green-100 text-green-700"
                    >Actif</span>
                </div>
            </div>
        </template>
    </div>
    <div v-else class="mt-7 border border-slate-200 mb-11 px-4 py-6 text-slate-400">
        Acces reserve aux administrateurs.
    </div>

    <div class="flex justify-center items-center">
        <NuxtLink
            to="/admin/user"
            class="inline-flex items-center mb-16 justify-center text-xl text-white uppercase bg-primary-500 h-[50px] px-8 rounded hover:opacity-80 gap-2"
            :class="auth.isAdmin ? '' : 'cursor-not-allowed opacity-60'"
            @click="handleAddClick"
        >
            <Icon name="mdi:plus" size="28" />
            Ajouter
        </NuxtLink>
    </div>
</template>

<script setup lang="ts">
import type { UserData } from "~/services/dto/user-data"
import { getAdminUsers } from "~/services/auth"
import { ROLE } from "~/utils/constants"
import { useAuthStore } from "~/stores/auth"

const userList = ref<UserData[]>([])
const router = useRouter()
const auth = useAuthStore()
const roleLabelByValue = new Map(ROLE.map((role) => [role.value, role.label]))

const goToUser = (id: number) => {
    if (!auth.isAdmin) return
    router.push(`/admin/user/${id}`)
}

const handleAddClick = (event: Event) => {
    if (auth.isAdmin) return
    event.preventDefault()
}

const getRoleLabels = (roles?: string[]) => {
    if (!roles || roles.length === 0) {
        return '---'
    }

    return roles
        .map((role) => roleLabelByValue.get(role) ?? role)
        .join(', ')
}

onMounted(async () => {
    if (!auth.isAdmin) return
    userList.value = await getAdminUsers()
})
</script>
