import {FormData} from "../formInterfaces";
import * as Yup from "yup";
type PunishmentsForUser = {
    punishments: string[]
};
type UserForPunishments = {
    username: string
};

export const minecraft_appeal: FormData = {
    steps: [
        {
            label: "What is your Minecraft username?",
            additional_info: "Use the username you had when you last tried to join the server.",
            name: "username",
            type: "text",
            min_length: 3,
            max_length: 16,
            required: true,
        },
        {
            label: "What is your email?",
            additional_info: "It does not have to be your minecraft email.",
            name: "email",
            type: "email",
            min_length: 3,
            max_length: 254,
            required: true,
        },
        {
            label: "What punishment would you like to appeal?",
            additional_info: "Please select it below.",
            name: "punishment",
            type: "dropdown",
            min_length: 10,
            max_length: 2000,
            required: true,
            drop_down: [],
            processInput: (input: string): Promise<string[]> => {
                return new Promise((resolve, reject) => {
                    const userForPunishments: UserForPunishments = {
                        username: input,
                    }
                    fetch(`${process.env.REACT_APP_BACKEND_BASE_URL}/api/appeal/retrieve-minecraft-punishments`, {
                        method: 'POST',
                        headers: {
                            'Content-Type': 'application/json',
                        },
                        body: JSON.stringify(userForPunishments)
                    })
                        .then(response => {
                            if (!response.ok) {
                                console.log(response)
                                reject(new Error('Invalid username'));
                            }
                            return response.json()
                        })
                        .then((punishmentsForUser: PunishmentsForUser) => {
                            resolve(punishmentsForUser.punishments);
                        })
                        .catch(error => {
                            console.error('Received an unexpected error: ' + error);
                            reject(error);
                        })
                });
            }
        },
        {
            label: "Why should your punishment be reduced or removed?",
            additional_info: "Please take your time writing this, we're more likely to accept an appeal if effort was put into it.",
            name: "appeal",
            type: "textarea",
            min_length: 2,
            max_length: 2000,
            required: true,
        },
    ],
    //TODO warning if no punishments
    backend: `${process.env.REACT_APP_BACKEND_BASE_URL}/api/appeal/minecraft`,
    userInput: {username: '', email: '', punishment: '', appeal: ''},
    spec: Yup.object().shape({
        username: Yup.string()
            .min(3, 'Username should be at least 3 characters')
            .max(16, 'Username should not exceed 16 characters')
            .matches(/^[a-zA-Z0-9_]*$/, 'Username should only include alphanumeric characters and underscore')
            .required('Username is required'),

        email: Yup.string()
            .email('Invalid email')
            .min(3, 'Email should be at least 3 characters')
            .max(254, 'Email should not exceed 254 characters')
            .required('Email is required'),

        punishment: Yup.string()
            .required('You are required to select a punishment'),

        appeal: Yup.string()
            .min(3, 'Your appeal needs to be at least 100 characters')
            .max(2000, 'Your appeal can not be longer than 2000 characters')
            .required()
    }),
    title: "Minecraft Appeal",
    backendFormName: "MinecraftAppeal",
};