import { Authorized, Body, JsonController, Post, QueryParam } from 'routing-controllers';
import { OpenAPI } from 'routing-controllers-openapi';
import { Mailer } from '../Mailer';
import { locales } from '../models/LocaleEnum';
import { MailTypes } from '../models/MailTypeEnum';
import { ResetMail } from '../models/ResetMail';
import { SuccessResponse } from '../models/SuccessResponse';
import { WelcomeMail } from '../models/WelcomeMail';

/**
 * The mail controller handels all endpoints concerning Mail sending.
 */
@JsonController()
@Authorized()
@OpenAPI({ security: [{ "AuthToken": [] }] })
export class MailController {
    private mailer: Mailer = new Mailer();
    private initialized: boolean = false;

    @Post('/reset')
    @OpenAPI({ description: "Sends reset mails", parameters: [{ in: "query", name: "locale", schema: { type: "string", enum: ["de", "en"] } }] })
    async sendReset(@Body({ validate: true }) mailOptions: ResetMail, @QueryParam("locale") locale: locales) {
        if (!this.initialized) {
            await this.mailer.init();
            this.initialized = true;
        }
        try {
            this.mailer.sendResetMail(mailOptions.address, mailOptions.resetKey, locale?.toString())
        } catch (error) {
            throw error;
        }
        return new SuccessResponse(MailTypes.PASSWORD_RESET, locale);
    }

    @Post('/test')
    @OpenAPI({ description: "Sends test mails", parameters: [{ in: "query", name: "locale", schema: { type: "string", enum: ["de", "en"] } }] })
    async sendTest(@QueryParam("locale") locale: locales) {
        if (!this.initialized) {
            await this.mailer.init();
            this.initialized = true;
        }
        try {
            this.mailer.sendTestMail(locale?.toString())
        } catch (error) {
            console.log(error)
            throw error;
        }
        return new SuccessResponse(MailTypes.TEST, locale);
    }

    @Post('/registration')
    @OpenAPI({ description: "Sends registration welcome mails", parameters: [{ in: "query", name: "locale", schema: { type: "string", enum: ["de", "en"] } }] })
    async sendRegistrationWelcome(@Body({ validate: true }) mailOptions: WelcomeMail, @QueryParam("locale") locale: locales) {
        if (!this.initialized) {
            await this.mailer.init();
            this.initialized = true;
        }
        try {
            this.mailer.sendWelcomeMail(mailOptions.address, mailOptions.selfserviceToken, locale?.toString())
        } catch (error) {
            console.log(error)
            throw error;
        }
        return new SuccessResponse(MailTypes.RUNNER_WELCOME, locale);
    }

    @Post('/registration_forgot')
    @OpenAPI({ description: "Sends selfservice link forgotten mails", parameters: [{ in: "query", name: "locale", schema: { type: "string", enum: ["de", "en"] } }] })
    async sendSelfserviceForgotten(@Body({ validate: true }) mailOptions: WelcomeMail, @QueryParam("locale") locale: locales) {
        if (!this.initialized) {
            await this.mailer.init();
            this.initialized = true;
        }
        try {
            this.mailer.sendSelfserviceForgottenMail(mailOptions.address, mailOptions.selfserviceToken, locale?.toString())
        } catch (error) {
            console.log(error)
            throw error;
        }
        return new SuccessResponse(MailTypes.RUNNER_FORGOT, locale);
    }
}
