export async function checkName(username) {
    const response = await fetch('https://docs.black-phoenix.ru/api/users/check_existing_username', {
      method:'POST',
      credentials:"include",
      headers:{
        'Content-Type': 'application/json',
      },
      body: JSON.stringify({
        "username": username
      })
    })
    if(response.status === 200){
      return ""
    }
    else if(response.status === 409){
      let data = "ник занят"
      return data
    }
    else if(response.status === 422){
      let data = await response.json();
      return data
    }
    else{
      console.log(response.status)
    }
}

export async function checkMail(mail) {
  const response = await fetch('https://docs.black-phoenix.ru/api/users/check_existing_email', {
    method:'POST',
    credentials:"include",
    headers:{
      'Content-Type': 'application/json'
    },
    body: JSON.stringify({
      "email": mail
    })
  })
       if(response.status === 200){
        return ""
  }

  else if(response.status === 409){
    return "почта занята"
  }

  else if(response.status === 422){
    return "не похоже на почту"
  }
}

export async function VerificationEmail(Code){
  const response = await fetch('https://docs.black-phoenix.ru/api/users/email_verification',{
  method:'POST',
  credentials:'include',
  headers:{
    'Content-Type' : 'application/json'
  },
  body: JSON.stringify({
      "user_code": Code
    })
  })

  if(response.status === 200){
    window.location.href = '/'
    return ""
  }
  else if(response.status === 422){
    return "Неправильный код"
  }
}


  export async function handleRegister(username,password,email,date_of_birth){
    const response = await fetch('https://docs.black-phoenix.ru/api/users/register',{
      method:'POST',
      credentials:"include",
      headers:{
        'Content-Type': 'application/json'
      },
      body: JSON.stringify({
        "email": email,
        "username": username,
        "password": password,
        "date_of_birth": date_of_birth
      })
    })

    if(response.status === 201){
      const data = await response.json();
      console.log(data)
      return data;
    }
    else if(response.status === 422){
      console.log(response.status)
      let data = "Validation Error"
      return data
    }
    
  }