/* Copyright (C) Click & Push Accessibility, Inc - All Rights Reserved
 * Unauthorized copying of this file, via any medium is strictly prohibited
 * Proprietary and confidential
 * Written and maintained by the Click & Push Development team 
 * <dev@clicknpush.ca>, January 2022
 */

import axios from 'axios';
import * as Yup from "yup";
import { useAuth } from '../state/external/auth-provider';
import { API_URL } from '../utils/RequestUtils';

/**
 * String enum for type of registration validation request to send to server
 */
type RegisterCredsValidationType = 'usernamevalidation' | 'emailvalidation';

export interface RegisterCredsValues {
    email: string;
    username: string;
}

export const useValidation = () => {
  const {sendApiRequestAsync} = useAuth();
  const uniqueValidation = async (validationEndpoint: RegisterCredsValidationType, valueToValidate: string): Promise<boolean> => {
    try {
      const data = new FormData()
      data.append('input', valueToValidate)
      const response = await sendApiRequestAsync({
        axiosConfig: {
          method: 'POST',
          url: API_URL + '/api/' + validationEndpoint + '/',
          data: data,
        },
        authorized: false,
        errorMessage: 'Error validating input',
        loggingCategory: 'PROFILE',
      });
      if (response.data.valid) {
        return true;
      }
      else {
        return false;
      }
    } catch (error) {
      return false;
    }
  }

  const credsSchema = Yup.object({
    username: Yup
      .string()
      .trim()
      .required("You must enter a username.")
      .min(5, "Your username must be atleast 5 characters long.")
      .test("unique-username", "This username is already in use", async (value) => await uniqueValidation("usernamevalidation", value)),
    email: Yup
      .string()
      .trim()
      .required("You must enter an email.")
      .email("You must enter a valid email.")
      .test("unique-email", "This email is already in use", async (value) => await uniqueValidation("emailvalidation", value)),
  })
  
  const profileCredsSchema = Yup.object({
    username: Yup
      .string()
      .trim()
      .required("You must enter a username.")
      .min(5, "Your username must be atleast 5 characters long."),
    email: Yup
      .string()
      .trim()
      .required("You must enter an email.")
      .email("You must enter a valid email.")
  })
  
  const passwordSchema = Yup.object({
    password: Yup.string().trim().required("You must enter a password.").matches(/^(?=.*\d)(?=.*[a-z])(?=.*[A-Z])[\w~@#$%^&*+=`|{}:;!.?\"()\[\]-]{8,}$/, "Your password must be a minimum of 8 characters and contain 1 special character and 1 number."),
    confirmPassword: Yup.string().trim().required("You must confirm your password.").oneOf([Yup.ref('password'), null], "Your passwords must match.")
  })

  return {credsSchema, profileCredsSchema, passwordSchema}
}