"use client";

import React, { useEffect, useState } from "react";
import { User } from "@/types/types";
import Container from "@/components/elements/Container";
import ModelHead from "@/components/models/ModelPage/ModelHead";
import useAxios from "@/hooks/useAxios";
import ModelContent from "@/components/models/ModelPage/ModelContent";





function Profile() {
  const [user, setUser] = useState<User | null>(null);
  const { request, loading } = useAxios();

  const fetchUser = async () => {
    try {
      const response = await request<{ user: User }>("GET", "/profile");
      setUser(response?.user ?? null);
    } catch (err) {
      console.error("خطا در دریافت اطلاعات کاربر:", err);
    }
  };

  useEffect(() => {
    fetchUser();
  }, []);

  // لودینگ
  if (loading || !user) {
    return (
      <div className="flex justify-center items-center h-screen">
        
      </div>
    );
  }

  return (
    <Container>
      <ModelHead
        item2={{
          postsCount: user.postsCount ?? 0,
          allProjectsCount: user.allProjectsCount ?? 0,
          successfulProjectsCount: user.successfulProjectsCount ?? 0,
        }}
        user={user}
      />

    <ModelContent user={user} searchParams={{}} />

    </Container>
  );
}

export default Profile;
