<template>
    <Head>
        <Title>Connexion</Title>
    </Head>
    <div class="flex flex-1 flex-col justify-center items-center">
        <div class="flex gap-8 items-center">
            <span class="border border-transparent hover:border-light-35 dark:hover:border-dark-35 p-1 cursor-pointer" @click="() => $router.go(-1)"><Icon icon="radix-icons:arrow-left" class="text-light-50 dark:text-dark-50 w-6 h-6"/></span>
            <ProseH4>Connexion</ProseH4>
        </div>
        <form @submit.prevent="() => submit()" class="flex flex-1 flex-col justify-center items-stretch">
            <TextInput type="text" label="Utilisateur ou email" autocomplete="username" v-model="state.usernameOrEmail"/>
            <TextInput type="password" label="Mot de passe" autocomplete="current-password" v-model="state.password"/>
            <Button class="border border-light-35 dark:border-dark-35 self-center" :loading="status === 'pending'">Se connecter</Button>
            <NuxtLink class="mt-4 text-center block text-sm font-semibold tracking-wide hover:text-accent-blue" :to="{ path: `/user/register`, force: true }">Pas de compte ?</NuxtLink>
        </form>
    </div>
</template>

<script setup lang="ts">
import type { ZodError } from 'zod';
import { schema, type Login } from '~/schemas/login';
import { Icon } from '@iconify/vue/dist/iconify.js';

definePageMeta({
    layout: 'login',
    usersGoesTo: '/user/profile',
});

const { add: addToast, clear: clearToasts } = useToast();

const state = reactive<Login>({
    usernameOrEmail: '',
    password: ''
});

const { data: result, status, error, refresh } = await useFetch('/api/auth/login', {
    body: state,
    immediate: false,
    method: 'POST',
    watch: false,
    ignoreResponseError: true,
})

const toastMessage = ref('');

async function submit()
{
    if(state.usernameOrEmail === "")
        return addToast({ content: 'Veuillez saisir un nom d\'utilisateur ou un email', timer: true, duration: 10000 });
    if(state.password === "")
        return addToast({ content: 'Veuillez saisir un mot de passe', timer: true, duration: 10000 });

    const data = schema.safeParse(state);

    if(data.success)
    {
        await refresh();

        const login = result.value;
        if(!login || !login.success)
        {
            handleErrors(login?.error ?? error.value!);
        }
        else if(status.value === 'success' && login.success)
        {
            clearToasts();
            addToast({ duration: 10000, content: 'Vous êtes maintenant connecté', timer: true, type: 'success' });
            await navigateTo('/user/profile');
        }
    }
    else
    {
        handleErrors(data.error);
    }
}
function handleErrors(error: Error | ZodError)
{
    if(!error)
        return;
    
    status.value = 'error';

    if(error.hasOwnProperty('issues'))
    {
        for(const err of (error as ZodError).issues)
        {
            return addToast({ content: err.message, timer: true, duration: 10000, type: 'error' });
        }
    }
    else
    {
        return addToast({ content: error?.message ?? 'Une erreur est survenue', timer: true, duration: 10000, type: 'error' });
    }
}
</script>