<script setup lang="ts">
import characterConfig from '#shared/character-config.json';
import type { CharacterConfig } from '~/types/character';
const { data: characters, error, status } = await useFetch(`/api/character`, { params: { visibility: "public" } });
const config = characterConfig as CharacterConfig;
</script>

<template>
    <Head>
        <Title>d[any] - Liste des personnages</Title>
    </Head>
    <div class="flex flex-col">
        <div v-if="status === 'pending'" class="flex flex-1 justify-center align-center">
            <Loading size="large" />
        </div>
        <template v-else-if="status === 'success'">
            <div v-if="characters && characters.length > 0" class="grid p-6 2xl:grid-cols-3 lg:grid-cols-2 grid-cols-1 gap-4 w-full">
                <div class="flex flex-col w-[360px] border border-light-35 dark:border-dark-35" v-for="character of characters">
                    <NuxtLink :to="{ name: 'character-id', params: { id: character.id } }" class="group bg-light-10 dark:bg-dark-10 p-2 flex flex-col gap-2">
                        <div class="flex flex-row gap-8 ps-4 items-center">
                            <div class="flex flex-1 flex-col gap-2 justify-center">
                                <span class="text-lg font-bold group-hover:text-accent-blue">{{ character.name }}</span>
                                <span class="border-b w-full border-light-50 dark:border-dark-50"></span>
                                <div class="flex flex-row flex-1 items-stretch gap-4">
                                    <span class="text-sm">Niveau {{ character.level }}</span>
                                    <span class="w-px h-full bg-light-50 dark:bg-dark-50"></span>
                                    <span class="text-sm italic">{{ config.peoples[character.people!]?.name }}</span>
                                </div>
                            </div>
                            <div class="rounded-full w-[96px] h-[96px] border border-light-50 dark:border-dark-50 bg-light-100 dark:bg-dark-100 !bg-opacity-10"></div>
                        </div>
                    </NuxtLink>
                </div>
            </div>
            <div v-else class="flex flex-col gap-2 items-center flex-1">
                <span class="text-lg font-bold">Il n'existe pas encore de personnage public</span>
                Soyez le premier à partager vos créations !
                <NuxtLink class="inline-flex justify-center items-center outline-none leading-none transition-[box-shadow]
        text-light-100 dark:text-dark-100 bg-light-20 dark:bg-dark-20 border border-light-40 dark:border-dark-40
        hover:bg-light-25 dark:hover:bg-dark-25 hover:border-light-50 dark:hover:border-dark-50
        focus:bg-light-30 dark:focus:bg-dark-30 focus:border-light-50 dark:focus:border-dark-50 focus:shadow-raw focus:shadow-light-50 dark:focus:shadow-dark-50 py-2 px-4" :to="{ name: 'character-id-edit', params: { id: 'new' } }">Nouveau personnage</NuxtLink>
            </div>
        </template>
        <div v-else>
            <span>Erreur de chargement</span>
            <span>{{ error?.message }}</span>
        </div>
    </div>
</template>