"use client";

export const dynamic = "force-dynamic";

import { QueryClient, QueryClientProvider } from "@tanstack/react-query";
import { useState } from "react";
import { useParams } from "next/navigation";
import { MailWorkspace } from "@/components/mail-workspace";
import { resolveWebmailHost } from "@/lib/site-config";

type TenantParams = {
  tenant?: string;
  path?: string[];
};

export default function TenantMailPage() {
  const params = useParams<TenantParams>();
  const [client] = useState(
    () =>
      new QueryClient({
        defaultOptions: {
          queries: {
            refetchOnWindowFocus: false,
            retry: 1,
            staleTime: 20_000,
          },
        },
      })
  );
  const tenant = Array.isArray(params.tenant) ? params.tenant[0] : params.tenant || "";
  const entryDomain = tenant ? `mail.${tenant}` : resolveWebmailHost();

  return (
    <QueryClientProvider client={client}>
      <MailWorkspace tenantDomain={tenant} entryDomain={entryDomain} />
    </QueryClientProvider>
  );
}
