import React from "react";
import {
  checkAccessTokenIsValid,
  retrieveUserType,
} from "@/apicalls/cookies/token";
import { redirect } from "next/navigation";
import SelectList from "@/components/auth/select";

async function SelectPage() {
  const token_is_valid = await checkAccessTokenIsValid();
  const selection = await retrieveUserType();

  const isEmployee = selection?.userType == "employee";
  const isOccupant = selection?.userType == "occupant";

  const selectionList = selection?.selectionList;

  if (!selectionList || !token_is_valid) {
    redirect("/auth/login");
  }
  return (
    <>
      <div className="flex flex-col items-center justify-center h-screen">
        <div className="text-2xl font-bold">Select your company</div>
        <div className="flex flex-col items-center justify-center">
          {isEmployee && (
            <div className="text-sm text-gray-500 mt-4">
              You are logged in as an employee
            </div>
          )}
          {isOccupant && (
            <div className="text-sm text-gray-500 mt-4">
              You are logged in as an occupant
            </div>
          )}
          <SelectList
            isEmployee={isEmployee}
            isOccupant={isOccupant}
            selectionList={selectionList}
          />
        </div>
        <div className="text-sm text-gray-500 mt-4"></div>
      </div>
    </>
  );
}

export default SelectPage;
