import { auth } from "@/auth";
import { db } from "@/lib/db";
import { redirect } from "next/navigation";
import { ImportForm } from "./import-form";
import type { Metadata } from "next";

export const metadata: Metadata = { title: "Import Purchase Order" };

export default async function ImportPoPage() {
  const session = await auth();
  if (!session?.user) redirect("/login");

  const { role } = session.user;
  if (!["MANAGER", "SUPERUSER", "ADMIN"].includes(role)) redirect("/dashboard");

  const [vessels, accounts, vendors] = await Promise.all([
    db.vessel.findMany({ where: { isActive: true }, orderBy: { name: "asc" } }),
    db.account.findMany({ where: { isActive: true }, orderBy: { name: "asc" } }),
    db.vendor.findMany({ orderBy: { name: "asc" } }),
  ]);

  return (
    <div className="max-w-6xl">
      <div className="mb-6">
        <h1 className="text-2xl font-semibold text-neutral-900">Import Purchase Order</h1>
        <p className="mt-1 text-sm text-neutral-500">
          Upload a Pelagia-format Excel PO file. Line items and vendor details are extracted automatically.
          You then select the vessel, account, and confirm before saving as a draft.
        </p>
      </div>
      <ImportForm vessels={vessels} accounts={accounts} vendors={vendors} />
    </div>
  );
}
