<template lang="pug">
.wait-message
    p.verify-message Thanks for signing up!
    p.verify-message We'll just need you to verify your email address to continue. Please check your email!
</template>

<script>
import { authenticator } from '../../services/auth.service.js'
import { createProfileForUserId } from '../../services/profile.service'
import { signupUser } from '../../services/user.service.js'

export default {
    name: 'Auth',
    props: {
        question: {
            required: true,
            type: Object,
            default: () => {},
        },
        answered: {
            type: Object,
            default: () => {},
        },
        responses: {
            type: Object,
            default: () => {},
        },
        survey: {
            required: true,
            type: Object,
            default: () => {},
        },
    },
    emits: ['update-answers'],
    async created() {
        // Establishes New User And Sends Auth Email
        try {
            this.doesUserHaveMinResponses(this.responses)
            const userPass = this.responses.find(
                response => response.response_key_id === 9,
            )
            const newUserId = await this.signupNewUser({
                ...this.answered,
                password: userPass.val,
            })
            await this.createProfileForNewUser(newUserId, this.responses)
            const sessionToken = await this.createToken({
                ...this.answered,
            })
            const sessionInfo = await authenticator.sendEmail({
                ...this.answered,
                sessionToken: sessionToken,
            })
            document.cookie = `siimee_session=${sessionInfo.hashedSessionToken}; max-age=600; path=/; secure`
        } catch (err) {
            // TODO: render an error page in this component displaying which
            // error occurred and how to reach out to staff
            console.error('ERROR :=>', err)
        }
    },
    methods: {
        doesUserHaveMinResponses(responses) {
            if (!this.survey.hasMinResponsesToCreateProfile(responses))
                throw new Error(
                    'User has not answered minimum amount of questions to create profile',
                )
        },
        async createToken(payload) {
            return await authenticator.createToken({
                payload,
            })
        },
        async signupNewUser(userInfo) {
            const newUser = await signupUser(userInfo)
            if (!newUser || newUser.error) {
                throw new Error(
                    'Error occured when signing up new User :=>',
                    newUser.error,
                )
            } else return newUser.user_id
        },
        async createProfileForNewUser(userId, responses) {
            try {
                await createProfileForUserId(userId, responses)
            } catch (err) {
                throw new Error(err)
            }
        },
    },
}
</script>

<style>
.wait-message {
    margin: 5rem auto;
    text-align: center;
    width: 90%;
    max-width: 35rem;
    font-size: 150%;
    font-weight: bold;
}
</style>
