<template>
  <main class="min-h-screen flex items-center justify-center bg-gradient-to-br from-blue-50 to-indigo-100 p-6">
    <div class="w-full max-w-2xl">
      <div class="card bg-base-100 shadow-2xl">
        <div class="card-body">
          <h1 class="text-2xl font-bold mb-2">Choisir un profil</h1>
          <p class="text-sm text-base-content/70 mb-6">
            Sélectionnez votre profil pour accéder à l'application. La création et la gestion se font via le menu utilisateur.
          </p>

          <section class="space-y-4">
            <header class="flex items-center justify-between">
              <h2 class="font-semibold">Profils disponibles</h2>
              <button
                type="button"
                class="btn btn-ghost btn-xs"
                @click="refreshProfiles"
                :disabled="loadingProfiles"
              >
                <span v-if="loadingProfiles" class="loading loading-spinner loading-xs"></span>
                <span v-else>Rafraîchir</span>
              </button>
            </header>

            <div class="space-y-2 max-h-64 overflow-y-auto" v-if="profiles.length">
              <button
                v-for="profile in profiles"
                :key="profile.id"
                type="button"
                class="btn btn-outline btn-sm w-full justify-between"
                @click="selectProfile(profile.id)"
              >
                <span>{{ profile.firstName }} {{ profile.lastName }}</span>
                <IconLucideChevronRight class="w-4 h-4" aria-hidden="true" />
              </button>
            </div>
            <p v-else class="text-sm text-base-content/60">Aucun profil enregistré.</p>
          </section>

          <footer class="mt-6 flex justify-between items-center" v-if="activeProfile">
            <div class="text-sm text-base-content/70">
              Profil actuel :
              <span class="font-semibold">{{ activeProfile.firstName }} {{ activeProfile.lastName }}</span>
            </div>
            <button type="button" class="btn btn-outline btn-sm" @click="handleLogout">
              Déconnexion
            </button>
          </footer>
        </div>
      </div>
    </div>
  </main>
</template>

<script setup>
import { onMounted } from 'vue'
import { useRouter } from 'vue-router'
import { useProfiles } from '#imports'
import { useProfileSession } from '#imports'
import IconLucideChevronRight from '~icons/lucide/chevron-right'

const router = useRouter()
const { profiles, loadingProfiles, fetchProfiles } = useProfiles()
const { activeProfile, activateProfile, fetchCurrentProfile, logout } = useProfileSession()

const refreshProfiles = async () => {
  await fetchProfiles()
}

const selectProfile = async (profileId) => {
  try {
    await activateProfile(profileId)
    await fetchProfiles()
    await router.push('/')
  } catch (error) {
    console.error('Erreur lors de la sélection du profil', error)
  }
}

const handleLogout = async () => {
  await logout()
  await router.push('/profiles')
}

onMounted(async () => {
  await fetchProfiles()
  await fetchCurrentProfile()
})
</script>
