import { LoadingPage } from "~/components/loading";
import { AreaRiservataLayoutUserView } from "~/components/Layout";
import type { GetStaticProps } from "next";
import type { NextPageWithLayout } from "~/pages/_app";
import type { UsersId } from "~/schemas/public/Users";
import { api } from "~/utils/api";
import { zUserId } from "~/server/utils/zod_types";
import { ServizioContainer } from "~/components/servizio/main";
import { AnnunciRichiesti } from "~/components/servizio/servizio_annunci_accordions";

type RicercaUserProps = {
  userId: UsersId;
};

const RicercaUser: NextPageWithLayout<RicercaUserProps> = ({
  userId,
}: RicercaUserProps) => {
  const { data: servizi, isLoading: loadingServizi } =
    api.servizio.getAllServizioAnnunci.useQuery({ userId });

  return (
    <div className="space-y-5">
      <div className="items-center justify-between md:flex">
        <div className="flex items-center gap-3">
          <h3 className="text-2xl font-bold">Le tue ricerche</h3>
        </div>
      </div>
      <div className="flex flex-col space-y-5">
        <AnnunciRichiesti userId={userId} />
        {loadingServizi ? (
          <LoadingPage />
        ) : (
          servizi?.map((servizio, idx) => (
            <ServizioContainer
              key={idx}
              servizio={servizio}
              userId={userId}
              isAdmin
            />
          ))
        )}
      </div>
    </div>
  );
};

RicercaUser.getLayout = (page) => {
  return <AreaRiservataLayoutUserView>{page}</AreaRiservataLayoutUserView>;
};
export const getStaticProps: GetStaticProps = (context) => {
  const userId = context.params?.userId;
  if (typeof userId !== "string") {
    return {
      notFound: true,
    };
  }

  const parsed = zUserId.safeParse(userId);

  if (!parsed.success) {
    {
      return {
        notFound: true,
      };
    }
  }

  return {
    props: {
      userId: parsed.data,
    },
  };
};

export const getStaticPaths = () => {
  return { paths: [], fallback: "blocking" };
};

export default RicercaUser;
