import { DashboardPage } from "@/components/layout/dashboard-page";
import { withDashboardAccess } from "@/components/layout/dashboard-access";
import { PageHeader } from "@/components/shared/page";
import { Button } from "@/components/ui/button";
import Link from "next/link";
import { PaymentConfiguration } from "@/features/payment-channel/configuration";
import { PayoutConfiguration } from "@/features/payment-channel/payout-configuration";

export const metadata = { title: "Payment Channel Integration" };

type PaymentMode = "payin" | "payout";

function Page({
  mode,
  payinHref,
  payoutHref,
}: {
  mode: PaymentMode;
  payinHref: string;
  payoutHref: string;
}) {
  return (
    <div className="space-y-5">
      <PageHeader title="Payment Channel Integration" />
      <nav aria-label="Payment direction" className="flex flex-wrap gap-2">
        <Button asChild variant={mode === "payin" ? "default" : "outline"}>
          <Link
            href={payinHref}
            aria-current={mode === "payin" ? "page" : undefined}
          >
            Pay-in
          </Link>
        </Button>
        <Button asChild variant={mode === "payout" ? "default" : "outline"}>
          <Link
            href={payoutHref}
            aria-current={mode === "payout" ? "page" : undefined}
          >
            Payout
          </Link>
        </Button>
      </nav>
      <div>
        {mode === "payin" ? (
          <PaymentConfiguration />
        ) : (
          <PayoutConfiguration />
        )}
      </div>
    </div>
  );
}

export default withDashboardAccess("/payment-channel", async function DashboardRoute(props: {
  searchParams: Promise<Record<string, string | string[] | undefined>>;
}, role) {
  const query = await props.searchParams;
  const requestedMode = Array.isArray(query.mode) ? query.mode[0] : query.mode;
  const mode: PaymentMode = requestedMode === "payout" ? "payout" : "payin";
  return (
    <DashboardPage role={role}>
      <Page
        mode={mode}
        payinHref="/payment-channel?mode=payin"
        payoutHref="/payment-channel?mode=payout"
      />
    </DashboardPage>
  );
});
