import { fetchBomCombo } from "@/app/api/bom";
import { fetchPrinterCombo } from "@/app/api/settings/printer";
import { fetchAllJobTypes, type SearchJoResultRequest } from "@/app/api/jo/actions";
import GeneralLoading from "@/components/General/GeneralLoading";
import PageTitleBar from "@/components/PageTitleBar";
import JoWorkbenchSearch from "@/components/JoWorkbench/JoWorkbenchSearch";
import { I18nProvider, getServerI18n } from "@/i18n";
import { Metadata } from "next";
import React, { Suspense } from "react";

export const metadata: Metadata = {
  title: "Job Order",
};

const Jo: React.FC = async () => {
  const { t } = await getServerI18n("jo");
  const today = new Date();
  const todayStr = today.toISOString().split("T")[0];
  const defaultInputs: SearchJoResultRequest = {
    code: "",
    itemName: "",
    planStart: `${todayStr}T00:00`,
    planStartTo: `${todayStr}T23:59:59`,
    joSearchStatus: "all",
  };
  const [bomCombo, printerCombo, jobTypes] = await Promise.all([
    fetchBomCombo(),
    fetchPrinterCombo(),
    fetchAllJobTypes(),
  ]);

  return (
    <>
      <PageTitleBar title={t("Search Job Order/ Create Job Order")} className="mb-4" />
      <I18nProvider namespaces={["jo", "common", "purchaseOrder", "dashboard"]}>
        <Suspense fallback={<GeneralLoading />}>
          <JoWorkbenchSearch
            defaultInputs={defaultInputs}
            bomCombo={bomCombo ?? []}
            printerCombo={printerCombo ?? []}
            jobTypes={jobTypes ?? []}
          />
        </Suspense>
      </I18nProvider>
    </>
  );
};

export default Jo;