import { baseAPI } from "@/Shared/API/baseAPI";
import { IFetch } from "@/Shared/types";
import axios from "axios";
import { create } from "zustand";

interface SignUpStore extends IFetch {
  register: (
    email: string,
    password: string,
    confirmPassword: string
  ) => Promise<void>;
  cleanRedirect: () => void;
  emailError: string;
  passwordError: string;
  confirmPasswordError: string;
  matchPasswordError: string;
  redirect: boolean;
}

export const useSignUp = create<SignUpStore>((set) => ({
  loading: false,
  error: "",
  emailError: "",
  passwordError: "",
  confirmPasswordError: "",
  matchPasswordError: "",
  redirect: false,
  register: async (
    email: string,
    password: string,
    confirmPassword: string
  ) => {
    if (!email.trim()) {
      set({ passwordError: "" });
      set({ confirmPasswordError: "" });
      set({ matchPasswordError: "" });
      set({ emailError: "Пожалуйста введите почту" });

      return;
    }
    if (!password.trim()) {
      set({ emailError: "" });
      set({ confirmPasswordError: "" });
      set({ matchPasswordError: "" });
      set({ passwordError: "Пожалуйста введите пароль" });
      return;
    }

    if (!confirmPassword.trim()) {
      set({ emailError: "" });
      set({ passwordError: "" });
      set({ matchPasswordError: "" });
      set({ confirmPasswordError: "Пожалуйста введите пароль" });
      return;
    }

    if (validatePassword(password)) {
      set({ emailError: "" });
      set({ passwordError: "" });
      set({ matchPasswordError: "" });
      set({ confirmPasswordError: "" });
      set({ error: "Минимум 8 символов, 1 заглавная буква и цифра" });

      return;
    }

    if (password !== confirmPassword) {
      set({ emailError: "" });
      set({ confirmPasswordError: "" });
      set({ passwordError: "" });
      set({ matchPasswordError: "Пароли не совпадают" });
      return;
    }

    const user = {
      email,
      password,
      password2: confirmPassword,
    };

    try {
      set({ loading: true });

      const response = await axios.post(
        `${baseAPI}/users/register/`,
        user
      );

      localStorage.setItem("tokens", JSON.stringify(response.data));

      set({ emailError: "" });
      set({ passwordError: "" });
      set({ confirmPasswordError: "" });
      set({ matchPasswordError: "" });
      set({ error: "" });
      set({ redirect: true });
    } catch (error: any) {
      set({ emailError: "" });
      set({ passwordError: "" });
      set({ confirmPasswordError: "" });
      set({ matchPasswordError: "" });
      set({ error: error.message });
    } finally {
      set({ loading: false });
    }
  },
  cleanRedirect: () => {
    set({ redirect: false });
  },
}));

const validatePassword = (password: string) => {
  const regex = /[A-Z]/;
  const digitRegex = /\d/;
  if (password.length < 8) return true;
  console.log("1");
  if (!regex.test(password) || !digitRegex.test(password))
    return true;

  return false;
};
