"use server";

import ProfileAvatar from "@/features/ProfileAvatar/ProfileAvatar";
import { apiInstance } from "@/shared/config/apiConfig";
import { authConfig } from "@/shared/config/authConfig";
import { IProfile } from "@/shared/types/profile-type";
import ProfileForm from "@/widgets/forms/ProfileForm/ProfileForm";
import { AxiosError } from "axios";
import { getServerSession } from "next-auth";
import React from "react";

const Personal = async () => {
  const session = await getServerSession(authConfig);
  const getProfile = async () => {
    const Authorization = `Bearer ${session?.access_token}`;
    const config = {
      headers: {
        Authorization,
      },
    };
    try {
      const response = await apiInstance.get<IProfile>(
        "/users/profile/",
        config
      );

      return response.data;
    } catch (error: unknown) {
      if (error instanceof AxiosError) console.log(error.message);
    }
  };

  const data = await getProfile();

  return (
    <div className="personal">
      <ProfileAvatar img={data?.image as string} />
      <ProfileForm
        id={data?.id as number}
        first_name={data?.first_name as string}
        last_name={data?.last_name as string}
        email={data?.email as string}
      />
    </div>
  );
};

export default Personal;
