<script lang="ts">
const mailSchema = z.object({
    to: z.string().email(),
    template: z.string(),
    data: z.string(),
});

const schemaList: Record<string, z.ZodObject<any> | null> = {
    'pull': null,
    'push': null,
    'mail': mailSchema,
}
</script>

<script setup lang="ts">
import { z } from 'zod/v4';
import { Icon } from '@iconify/vue/dist/iconify.js';
import { Toaster } from '#shared/components.util';

definePageMeta({
    rights: ['admin'],
})
const job = ref<string>('');

const payload = reactive<Record<string, any>>({
    data: JSON.stringify({ username: "Peaceultime", id: 1, timestamp: Date.now() }),
    to: 'clem31470@gmail.com',
});
const data = ref(), status = ref<'idle' | 'pending' | 'success' | 'error'>('idle'), success = ref(false), error = ref<Error | null>();
async function fetch()
{
    status.value = 'pending';
    data.value = null;
    error.value = null;
    success.value = false;

    try
    {
        const schema = schemaList[job.value];

        if(schema)
        {
            const parsedPayload = schema.parse(payload);
        }

        data.value = await $fetch(`/api/admin/jobs/${job.value}`, {
            method: 'POST',
            body: payload,
        });
        status.value = 'success';
        error.value = null;
        success.value = true;

        Toaster.add({ duration: 10000, content: data.value ?? 'Job executé avec succès', type: 'success', timer: true, });
    }
    catch(e)
    {
        status.value = 'error';
        error.value = e as Error;
        success.value = false;

        Toaster.add({ duration: 10000, content: error.value.message, type: 'error', timer: true, });
    }
}
</script>

<template>
    <Head>
        <Title>d[any] - Administration</Title>
    </Head>
    <div class="flex flex-col justify-start items-center p-4">
        <div class="flex flex-row justify-between items-center gap-8">
            <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>
            <ProseH2 class="text-center flex-1">Administration</ProseH2>
        </div>
        <div class="flex flex-row w-full gap-8">
            <Select label="Job" v-model="job">
                <SelectItem label="Récupérer les données d'Obsidian" value="pull" />
                <SelectItem label="Envoyer les données dans Obsidian" value="push" disabled />
                <SelectItem label="Envoyer un mail de test" value="mail" />
            </Select>
            <Select v-if="job === 'mail'" v-model="payload.template" label="Modèle" class="w-full" ><SelectItem label="Inscription" value="registration" /></Select>
        </div>
        <div v-if="job === 'mail'" class="flex justify-center items-center flex-col">
            <TextInput label="Destinataire" class="w-full" v-model="payload.to" />
            <textarea v-model="payload.data" class="w-[640px] bg-light-20 dark:bg-dark-20 border border-light-35 dark:border-dark-35 outline-none m-2 px-2"></textarea>
        </div>
        <Button class="self-center" @click="() => !!job && fetch()" :loading="status === 'pending'">
            <span>Executer</span>
        </Button>
    </div>
</template>