<script setup lang="ts">
import InputError from '@/components/InputError.vue';
import { Button } from '@/components/ui/crm-button';
import { Input } from '@/components/ui/crm-input';
import { PinInput, PinInputGroup, PinInputSlot } from '@/components/ui/pin-input';
import AuthLayout from '@/layouts/AuthLayout.vue';
import { store } from '@/routes/two-factor/login';
import { Form, Head } from '@inertiajs/vue3';
import { computed, ref } from 'vue';

interface AuthConfigContent {
    title: string;
    description: string;
    toggleText: string;
}

const authConfigContent = computed<AuthConfigContent>(() => {
    if (showRecoveryInput.value) {
        return {
            title: 'Wiederherstellungscode',
            description: 'Bitte bestätige den Zugriff auf Dein Konto, indem Du einen Deiner Notfall-Wiederherstellungscodes eingeben.',
            toggleText: 'mit Authentifizierungscode anmelden',
        };
    }

    return {
        title: 'Authentication Code',
        description: 'Bitte gib das Einmalkennwort aus Deiner Authentifizierungs-App ein.',
        toggleText: 'mit einem Wiederherstellungscode anmelden',
    };
});

const showRecoveryInput = ref<boolean>(false);

const toggleRecoveryMode = (clearErrors: () => void): void => {
    showRecoveryInput.value = !showRecoveryInput.value;
    clearErrors();
    code.value = [];
};

const code = ref<number[]>([]);
const codeValue = computed<string>(() => code.value.join(''));
</script>

<template>
    <AuthLayout :title="authConfigContent.title" :description="authConfigContent.description">

        <Head title="Two-Factor Authentication" />

        <div class="space-y-6">
            <template v-if="!showRecoveryInput">
                <Form v-bind="store.form()" class="space-y-4" reset-on-error @error="code = []"
                    #default="{ errors, processing, clearErrors }">
                    <input type="hidden" name="code" :value="codeValue" />
                    <div class="flex flex-col items-center justify-center space-y-3 text-center">
                        <div class="flex w-full items-center justify-center">
                            <PinInput id="otp" placeholder="" v-model="code" type="number" otp>
                                <PinInputGroup>
                                    <PinInputSlot v-for="(id, index) in 6" :key="id" :index="index"
                                        :disabled="processing" autofocus class="bg-background" />
                                </PinInputGroup>
                            </PinInput>
                        </div>
                        <InputError :message="errors.code" />
                    </div>
                    <Button type="submit" variant="action" class="w-full" :disabled="processing">Weiter</Button>
                    <div class="text-center text-sm text-muted-foreground">
                        <Button type="button" variant="link" @click="() => toggleRecoveryMode(clearErrors)">
                            {{ authConfigContent.toggleText }}
                        </Button>
                    </div>
                </Form>
            </template>

            <template v-else>
                <Form v-bind="store.form()" class="space-y-4" reset-on-error
                    #default="{ errors, processing, clearErrors }">
                    <Input name="recovery_code" type="text" placeholder="Enter recovery code"
                        :autofocus="showRecoveryInput" required />
                    <InputError :message="errors.recovery_code" />
                    <Button type="submit" class="w-full" :disabled="processing">Weiter</Button>

                    <div class="text-center text-sm text-muted-foreground">
                        <Button type="button" variant="link" @click="() => toggleRecoveryMode(clearErrors)">
                            {{ authConfigContent.toggleText }}
                        </Button>
                    </div>
                </Form>
            </template>
        </div>
    </AuthLayout>
</template>
