import { Metadata } from "next";
import { redirect } from "next/navigation";

import { getProfileData } from "@/services/general-actions";
import ManualEmailUpdate from "@/components/pages/participant/profile/email-update/manual-email-update";
import { ContactInformationProfileSchema } from "@/types/profile/profile-update-schema";
import { retrieveUserToken } from "@/lib/user-session";
import { INTERNAL_PAGES } from "@/constants/pages-mapping/internal-pages-mapping";

export const metadata: Metadata = {
  title: "Email update | Profile",
};

export default async function ManualPhoneVerificationPage() {
  const userToken = await retrieveUserToken();

  if (!userToken) {
    redirect(INTERNAL_PAGES.participant.login);
  }

  const participantData = await getProfileData(userToken);
  const { id: userId } = participantData;
  const { firstName } = participantData.participant;

  const currentUserData: ContactInformationProfileSchema = {
    email: participantData.participant.email,
    countryId: participantData.personalInformation.countryId,
    phoneNumber: participantData.participant.phoneNumber,
    postCode: participantData.personalInformation.postCode,
    alternativeContactNumber:
      participantData.personalInformation.alternativeContactNumber,
    city: participantData.personalInformation.city,
    state: participantData.personalInformation.state,
    address: participantData.personalInformation.formattedAddress,
  };

  return (
    <div className="flex flex-col min-h-screen bg-participant-light dark:bg-participant-dark light">
      <ManualEmailUpdate
        phoneNumber={currentUserData.phoneNumber}
        userData={currentUserData}
        userEmail={currentUserData.email}
        userFirstName={firstName}
        userId={userId}
      />
    </div>
  );
}
