"use server";
import { fetchData, fetchDataWithToken } from "../api-fetcher";
import { cookies } from "next/headers";
import { baseUrl, cookieObject, tokenSecret } from "../basics";
import NextCrypto from "next-crypto";
// import { setAvailableEvents } from "../events/available";

const loginEndpoint = `${baseUrl}/authentication/login`;
const loginSelectEndpoint = `${baseUrl}/authentication/select`;

interface LoginViaAccessKeys {
  domain: string;
  accessKey: string;
  password: string;
  rememberMe: boolean;
}

interface LoginSelectEmployee {
  company_uu_id: string;
}

interface LoginSelectOccupant {
  selectedBuilding: any;
  build_part_uu_id: string;
  occupant_uu_id: string;
}

async function loginViaAccessKeys(payload: LoginViaAccessKeys) {
  const cookieStore = await cookies();
  const nextCrypto = new NextCrypto(tokenSecret);

  const tokenResponse: any = await fetchData(
    loginEndpoint,
    {
      domain: payload.domain,
      access_key: payload.accessKey,
      password: payload.password,
      remember_me: payload.rememberMe,
    },
    "POST",
    false
  );

  if (tokenResponse.status === 200) {
    const accessToken = await nextCrypto.encrypt(tokenResponse.access_token);
    const accessObject = await nextCrypto.encrypt(
      JSON.stringify(tokenResponse.access_object)
    );
    const userProfile = await nextCrypto.encrypt(
      JSON.stringify(tokenResponse.user)
    );
    const refreshToken = await nextCrypto.encrypt(tokenResponse.refresh_token);

    // const userType = await nextCrypto.encrypt(responseData.user_type);
    // cookieStore.set({
    //   name: "refreshToken",
    //   value: refreshToken,
    //   httpOnly: true,
    //   path: "/",
    // });

    cookieStore.set({
      name: "accessToken",
      value: accessToken,
      ...cookieObject,
    });

    cookieStore.set({
      name: "accessObject",
      value: accessObject,
      ...cookieObject,
    });
    cookieStore.set({
      name: "userProfile",
      value: JSON.stringify(userProfile),
      ...cookieObject,
    });
  }
  return tokenResponse;
}

async function loginSelectEmployee(payload: LoginSelectEmployee) {
  const cookieStore = await cookies();
  const nextCrypto = new NextCrypto(tokenSecret);

  const selectResponse: any = await fetchDataWithToken(
    loginSelectEndpoint,
    {
      company_uu_id: payload.company_uu_id,
    },
    "POST",
    false
  );
  if (selectResponse.status === 200) {
    const usersSelection = await nextCrypto.encrypt(
      JSON.stringify({
        company_uu_id: payload.company_uu_id,
        user_type: "employee",
      })
    );
    cookieStore.set({
      name: "userSelection",
      value: usersSelection,
      ...cookieObject,
    });
  }
  return selectResponse;
}

async function loginSelectOccupant(payload: LoginSelectOccupant) {
  const selectedBuilding = payload.selectedBuilding;
  const cookieStore = await cookies();
  const nextCrypto = new NextCrypto(tokenSecret);
  const selectResponse: any = await fetchDataWithToken(
    loginSelectEndpoint,
    {
      build_part_uu_id: payload.build_part_uu_id,
      occupant_uu_id: payload.occupant_uu_id,
    },
    "POST",
    false
  );

  if (selectResponse.status === 200) {
    const usersSelection = await nextCrypto.encrypt(
      JSON.stringify({
        company_uu_id: {
          build_part_uu_id: payload.build_part_uu_id,
          occupant_uu_id: payload.occupant_uu_id,
          build_id: selectedBuilding,
        },
        user_type: "occupant",
      })
    );
    cookieStore.set({
      name: "userSelection",
      value: usersSelection,
      ...cookieObject,
    });
    // await setAvailableEvents();
  }
  return selectResponse;
}

export { loginViaAccessKeys, loginSelectEmployee, loginSelectOccupant };
