import { AreaRiservataLayoutUserView } from "~/components/Layout";
import { LoadingPage } from "~/components/loading";
import { api } from "~/utils/api";
import type { NextPageWithLayout } from "~/pages/_app";
import type { GetStaticProps } from "next";
import { Status500 } from "~/components/status-page";
import type { UsersId } from "~/schemas/public/Users";
import { zUserId } from "~/server/utils/zod_types";
import { OrdiniTable } from "~/components/tables/orders-table";
type OrdiniUserProps = {
  userId: UsersId;
};

const OrdiniUser: NextPageWithLayout<OrdiniUserProps> = ({
  userId,
}: OrdiniUserProps) => {
  const { data, isLoading } = api.servizio.getOrdini.useQuery({
    userId,
  });

  if (isLoading) return <LoadingPage />;
  if (!data) return <Status500 />;
  return (
    <main className="flex flex-1 flex-col items-start gap-4 md:gap-8">
      <div className="grid w-full auto-rows-max items-start gap-2 md:gap-4">
        <div className="overflow-auto border-none">
          <h3 className="text-2xl font-bold">Ordini</h3>

          <div className="w-full">
            <OrdiniTable data={data} isAdmin userId={userId} />
          </div>
        </div>
      </div>
    </main>
  );
};

export default OrdiniUser;
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" };
};

OrdiniUser.getLayout = function getLayout(page) {
  return <AreaRiservataLayoutUserView>{page}</AreaRiservataLayoutUserView>;
};
