<template>
  <div
    class="relative min-h-screen overflow-hidden bg-[#f5f5f5] px-3 py-8 font-sans text-[#0c0a09] sm:px-5 sm:py-12 md:px-8 md:py-16">
    <div
      class="pointer-events-none absolute left-1/2 top-6 h-[360px] w-[min(94vw,760px)] -translate-x-1/2 rounded-[40px] bg-[radial-gradient(circle_at_20%_30%,rgba(167,229,211,0.62),transparent_28%),radial-gradient(circle_at_72%_24%,rgba(168,200,232,0.56),transparent_30%),radial-gradient(circle_at_52%_78%,rgba(232,184,196,0.5),transparent_34%)] blur-2xl sm:top-8 sm:h-[420px] sm:rounded-[48px]"
      aria-hidden="true"></div>

    <main class="relative mx-auto grid w-full max-w-[960px] gap-6 sm:gap-8" aria-labelledby="home-title">
      <header class="flex min-w-0 flex-col gap-5 md:flex-row md:items-end md:justify-between">
        <div class="min-w-0">
          <p
            class="m-0 inline-flex rounded-full bg-[rgba(168,200,232,0.56)] px-3 py-1 text-xs font-semibold uppercase leading-[1.4] tracking-[0.96px] text-[#0c0a09]">
            Conta
          </p>
          <h1 id="home-title"
            class="my-4 max-w-[680px] break-words font-serif text-[34px] font-light leading-[1.08] tracking-[-0.96px] text-[#0c0a09] sm:my-5 sm:text-[40px] md:text-[48px]">
            Olá, você está autenticado!
          </h1>
          <p
            class="m-0 max-w-[540px] text-[15px] font-normal leading-[1.47] tracking-[0.15px] text-[#4e4e4e] sm:text-base sm:leading-6 sm:tracking-[0.16px]">
            Estes são os dados retornados pelo perfil da sua sessão atual.
          </p>
        </div>

        <div class="flex shrink-0 flex-col items-start gap-3 sm:flex-row sm:items-center">
          <NuxtLink to="/ranking-jogos"
            class="inline-flex h-10 w-fit items-center justify-center rounded-full border border-[#d6d3d1] bg-transparent px-5 text-[15px] font-medium leading-none text-[#0c0a09] transition hover:border-[#0c0a09]">
            Ver rankings
          </NuxtLink>
          <NuxtLink to="/perfil-gamer"
            class="inline-flex h-10 w-fit items-center justify-center rounded-full border border-[#d6d3d1] bg-transparent px-5 text-[15px] font-medium leading-none text-[#0c0a09] transition hover:border-[#0c0a09]">
            Perfil Gamer
          </NuxtLink>
          <NuxtLink to="/favoritos"
            class="inline-flex h-10 w-fit items-center justify-center rounded-full border border-[#d6d3d1] bg-transparent px-5 text-[15px] font-medium leading-none text-[#0c0a09] transition hover:border-[#0c0a09]">
            Meus favoritos
          </NuxtLink>
          <button type="button" :disabled="isLeaving" @click="sair"
            class="inline-flex h-10 w-fit items-center justify-center gap-2 rounded-full bg-[#292524] px-5 text-[15px] font-medium leading-none text-white transition hover:bg-[#0c0a09] active:bg-[#0c0a09] disabled:cursor-not-allowed disabled:opacity-70">
            <Icon v-if="isLeaving" name="mdi:loading" class="animate-spin text-base" />
            <span v-else>Sair</span>
          </button>
        </div>
      </header>

      <section
        class="grid min-w-0 gap-5 rounded-xl border border-[#e7e5e4] bg-white p-4 shadow-[0_4px_16px_rgba(0,0,0,0.04)] sm:rounded-2xl sm:p-6 md:p-8"
        aria-label="Dados do perfil autenticado">
        <div>
          <span
            class="inline-flex min-h-6 items-center rounded-full bg-[rgba(167,229,211,0.62)] px-2.5 py-1 text-xs font-semibold uppercase leading-none tracking-[0.96px] text-[#0c0a09]">
            Perfil
          </span>
          <h2
            class="mt-4 font-serif text-[26px] font-light leading-[1.13] tracking-[-0.32px] text-[#0c0a09] sm:text-3xl">
            Dados da conta
          </h2>
        </div>

        <dl class="grid min-w-0 gap-3 sm:gap-4">
          <div v-for="item in profileRows" :key="item.label"
            class="grid min-w-0 gap-1 border-t border-[#e7e5e4] pt-3 sm:grid-cols-[140px_minmax(0,1fr)] sm:gap-5 sm:pt-4 md:grid-cols-[160px_minmax(0,1fr)]">
            <dt class="text-xs font-semibold uppercase leading-[1.4] tracking-[0.96px] text-[#777169]">
              {{ item.label }}
            </dt>
            <dd
              class="m-0 min-w-0 text-[15px] leading-[1.47] tracking-[0.15px] text-[#0c0a09] sm:text-base sm:leading-6 sm:tracking-[0.16px]"
              :class="item.isLong ? 'break-all' : 'break-words'">
              {{ item.value }}
            </dd>
          </div>
        </dl>
      </section>
    </main>
  </div>
</template>

<script setup>
definePageMeta({
  middleware: 'auth'
})

const { $toast } = useNuxtApp()

const token = useCookie('token', {
  secure: true,
  sameSite: 'lax',
  maxAge: 900
})

const isLeaving = ref(false)

const clearToken = () => {
  token.value = null
}

const { data: profile, error } = await useAsyncData('profile-me', () =>
  $fetch('/profile/me', {
    headers: {
      Authorization: `Bearer ${token.value}`
    }
  })
)

if (error.value) {
  clearToken()

  if (import.meta.client) {
    $toast.error('Sua sessão expirou. Faça login novamente.', { duration: 8000 })
  }

  await navigateTo('/login')
}

const profileRows = computed(() => [
  {
    label: 'user_id',
    value: profile.value?.user_id ?? profile.value?.id ?? '-'
  },
  {
    label: 'email',
    value: profile.value?.email ?? '-'
  },
  {
    label: 'created_at',
    value: profile.value?.created_at ?? '-'
  },
  {
    label: 'updated_at',
    value: profile.value?.updated_at ?? '-'
  },
  {
    label: 'jwt_token',
    value: token.value ?? '-',
    isLong: true
  }
])

async function sair() {
  isLeaving.value = true
  clearToken()
  $toast.success('Sessão encerrada com sucesso.', { duration: 8000 })
  await navigateTo('/login')
}
</script>
