"use server";

import { retrieveAvailableEndpoint } from "@/apicalls/checkEndpoint";
import { checkAccessTokenIsValid } from "@/apicalls/cookies/token";
import { decryptQuery, defaultPagination } from "@/apicalls/test";
import { retrieveHeadersAndValidationByEndpoint } from "@/apicalls/validations/validations";
import { redirect } from "next/navigation";
import CreatePageComponent from "./CreatePage";

export default async function BuildingCreatePage({
  searchParams,
}: {
  searchParams: any;
}) {
  if (!(await checkAccessTokenIsValid())) {
    redirect("/login/email");
  }

  const buildKey = "building";
  const searchParamsKeys = await searchParams;
  const endpointUrl = "/building/build/create";

  const queryEncrypt = await decryptQuery(searchParamsKeys?.q);
  const endpointAvailable = await retrieveAvailableEndpoint(endpointUrl);
  const validateAndHeaders = await retrieveHeadersAndValidationByEndpoint({
    endpoint: endpointUrl,
  });
  const validator = validateAndHeaders?.validated || {};
  const headers = validateAndHeaders?.headers || {};
  console.log("validateAndHeaders", validateAndHeaders);
  console.log("endpointAvailable", endpointAvailable);
  console.log("queryEncrypt", queryEncrypt);
  return (
    <div>
      <h1>Create Building</h1>
      <h1>{JSON.stringify(queryEncrypt)}</h1>
      <CreatePageComponent validator={validator} headers={headers} />
    </div>
  );
}
