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

import { ContactInformationProfileSchema } from "@/types/profile/profile-update-schema";
import ContactInformation from "@/components/pages/participant/profile/tabs/contact-information";
import { CORE_ENDPOINTS } from "@/constants/api-mappings/external-api-mapping";
import { getProfileData, getOptions } from "@/services/general-actions";
import { retrieveUserToken } from "@/lib/user-session";
import { INTERNAL_PAGES } from "@/constants/pages-mapping/internal-pages-mapping";

export const metadata: Metadata = {
  title: "Contact information | Profile",
};

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

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

  const participantData = await getProfileData(userToken);

  const countryOptions = await getOptions(
    CORE_ENDPOINTS.options.countries,
    userToken
  );

  const { id: userId } = participantData;
  const { participant, personalInformation } = participantData;
  const { isPhoneVerified } = participant;

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

  return (
    <ContactInformation
      countryOptions={countryOptions}
      isPhoneVerified={isPhoneVerified}
      profileData={profileData}
      userId={userId}
    />
  );
}
