import Typography from "@/shared/ui/components/Typography/Typography";
import "./Profile.scss";
import ProfileNav from "@/widgets/ProfileNav/ProfileNav";
import AuthGuard from "./AuthGuard";
import { AxiosError } from "axios";
import { apiInstance } from "@/shared/config/apiConfig";
import { getServerSession } from "next-auth";
import { authConfig } from "@/shared/config/authConfig";
import { IProfile } from "@/shared/types/profile-type";
import { Metadata } from "next";

export const metadata: Metadata = {
  title: "KG ROAD | Профиль",
  description:
    "Страница профиля KG ROAD",
};

const Profile = async ({
  children,
}: Readonly<{
  children: React.ReactNode;
}>) => {
  const session = await getServerSession(authConfig);

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

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

  const data = await getProfile();

  return (
    <div className="profile page-padding">
      <Typography element="h2">Личный кабинет</Typography>
      <ProfileNav report_count={data?.report_count as number} />

      <AuthGuard>{children}</AuthGuard>
    </div>
  );
};

export default Profile;