import { useTranslation } from "~/providers/I18nProvider";
import { AreaRiservataLayout } from "~/components/Layout";
import { LoadingPage } from "~/components/loading";
import Head from "next/head";
import { api } from "~/utils/api";
import type { NextPageWithLayout } from "~/pages/_app";
import { Status500 } from "~/components/status-page";
import { useSession } from "~/providers/SessionProvider";
import type { UsersId } from "~/schemas/public/Users";
import { OrdiniTable } from "~/components/tables/orders-table";

const Ordini: NextPageWithLayout = () => {
  const { t } = useTranslation();
  const session = useSession();

  if (session.status == "pending") return <LoadingPage />;
  if (!session.user) return <Status500 />;
  return (
    <>
      <Head>
        <title>{t.heads.area_riservata_titolo}</title>
        <meta name="description" content={t.heads.area_riservata_description} />
      </Head>
      <SessionWrapper userId={session.user.id} />
    </>
  );
};
export default Ordini;

const SessionWrapper = ({ userId }: { userId: UsersId }) => {
  const { data, isLoading } = api.servizio.getOrdini.useQuery({
    userId,
  });

  if (isLoading) return <LoadingPage />;
  if (!data) return <Status500 />;

  return (
    <main className="mx-1 mt-2 flex flex-1 flex-col items-start gap-4 p-2 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 py-4">
            <OrdiniTable data={data} isAdmin={false} userId={userId} />
          </div>
        </div>
      </div>
    </main>
  );
};

Ordini.getLayout = function getLayout(page) {
  return <AreaRiservataLayout>{page}</AreaRiservataLayout>;
};
