#!/usr/bin/env node
/** Scaffold payment app routes — thin wrappers to @/modules/payment/features */
import fs from "node:fs";
import path from "node:path";
import { fileURLToPath } from "node:url";

const ROOT = path.join(path.dirname(fileURLToPath(import.meta.url)), "..");
const APP = path.join(ROOT, "app", "payment");

const ROUTES = [
  { dir: "hub", export: "PaymentHub" },
  { dir: "dashboard", export: "PaymentDashboard" },
  { dir: "overview", export: "PaymentOverview" },
  { dir: "kpis", export: "PaymentKpis" },
  { dir: "transactions", export: "PaymentTransactions" },
  { dir: "transactions/[id]", export: "PaymentTransactionDetail" },
  { dir: "ledger", export: "PaymentLedger" },
  { dir: "psp", export: "PaymentPsp" },
  { dir: "psp/health", export: "PaymentGatewayHealth" },
  { dir: "providers", export: "PaymentProviders" },
  { dir: "merchants", export: "PaymentMerchants" },
  { dir: "merchants/[id]", export: "PaymentMerchantDetail" },
  { dir: "merchants/approvals", export: "PaymentMerchantApprovals" },
  { dir: "requests", export: "PaymentRequests" },
  { dir: "requests/[id]", export: "PaymentRequestDetail" },
  { dir: "callbacks", export: "PaymentCallbacks" },
  { dir: "verification", export: "PaymentVerification" },
  { dir: "audit", export: "PaymentAudit" },
  { dir: "bundles", export: "PaymentBundles" },
  { dir: "feature-toggles", export: "PaymentFeatureToggles" },
  { dir: "capabilities", export: "PaymentCapabilities" },
  { dir: "settings", export: "PaymentSettings" },
  { dir: "metrics", export: "PaymentMetrics" },
  { dir: "monitoring", export: "PaymentMonitoring" },
  { dir: "api-explorer", export: "PaymentApiExplorer" },
  { dir: "openapi", export: "PaymentOpenApi" },
];

const LOADING = `import { LoadingState } from "@/components/ds";
export default function Loading() {
  return <LoadingState label="در حال بارگذاری تربت‌پی…" />;
}
`;

const ERROR = `"use client";
import { ErrorState } from "@/components/ds";
export default function Error({ error, reset }: { error: Error; reset: () => void }) {
  return <ErrorState message={error.message} onRetry={reset} />;
}
`;

function featureImport(exportName, dir) {
  const featureMap = {
    PaymentHub: "hub",
    PaymentDashboard: "dashboard",
    PaymentOverview: "overview",
    PaymentKpis: "kpis",
    PaymentTransactions: "transactions",
    PaymentTransactionDetail: "transactionDetail",
    PaymentLedger: "ledger",
    PaymentPsp: "psp",
    PaymentGatewayHealth: "gatewayHealth",
    PaymentProviders: "providers",
    PaymentMerchants: "merchants",
    PaymentMerchantDetail: "merchantDetail",
    PaymentMerchantApprovals: "merchantApprovals",
    PaymentRequests: "requests",
    PaymentRequestDetail: "requestDetail",
    PaymentCallbacks: "callbacks",
    PaymentVerification: "verification",
    PaymentAudit: "audit",
    PaymentBundles: "bundles",
    PaymentFeatureToggles: "featureToggles",
    PaymentCapabilities: "capabilities",
    PaymentSettings: "settings",
    PaymentMetrics: "metrics",
    PaymentMonitoring: "monitoring",
    PaymentApiExplorer: "apiExplorer",
    PaymentOpenApi: "openapi",
  };
  const feature = featureMap[exportName];
  return `"use client";
export { ${exportName} as default } from "@/modules/payment/features/${feature}";
`;
}

for (const { dir, export: exp } of ROUTES) {
  const routeDir = path.join(APP, dir);
  fs.mkdirSync(routeDir, { recursive: true });
  fs.writeFileSync(path.join(routeDir, "page.tsx"), featureImport(exp, dir));
  fs.writeFileSync(path.join(routeDir, "loading.tsx"), LOADING);
  fs.writeFileSync(path.join(routeDir, "error.tsx"), ERROR);
}

// layout
fs.writeFileSync(
  path.join(APP, "layout.tsx"),
  `"use client";

import { PaymentRootLayout } from "@/modules/payment/components/createPortalLayout";

export default PaymentRootLayout;
`
);

// root page redirect
fs.writeFileSync(
  path.join(APP, "page.tsx"),
  `import { redirect } from "next/navigation";

export default function PaymentIndexPage() {
  redirect("/payment/hub");
}
`
);

fs.writeFileSync(path.join(APP, "loading.tsx"), LOADING);
fs.writeFileSync(path.join(APP, "error.tsx"), ERROR);

console.log(`Scaffolded ${ROUTES.length} payment routes`);
