import { writeFileSync, mkdirSync } from "fs";

// Helper: write a simple proxy route file
function writeProxy(path, content) {
  mkdirSync(path.replace(/\/[^/]+$/, ""), { recursive: true });
  writeFileSync(path, content);
}

// Reusable proxy template using proxyRequest
const proxyTemplate = (methods, backendPath, opts = "") => {
  const lines = [];
  lines.push(`import { NextRequest } from "next/server";`);
  lines.push(`import { proxyRequest } from "../../../../lib/backend";`);
  lines.push(``);
  for (const m of methods) {
    lines.push(`export async function ${m}(req: NextRequest) {`);
    lines.push(`  return proxyRequest(req, "${backendPath}"${opts ? `, ${opts}` : ""});`);
    lines.push(`}`);
    lines.push(``);
  }
  return lines.join("\n");
};

// ─── Updated existing proxies ────────────────────────────────────────────────

// auth/login — public, no bearer needed
writeProxy("app/api/auth/login/route.ts", `import { NextRequest } from "next/server";
import { proxyRequest } from "../../../../lib/backend";

export async function POST(req: NextRequest) {
  return proxyRequest(req, "auth/login");
}
`);

// auth/register — public
writeProxy("app/api/auth/register/route.ts", `import { NextRequest } from "next/server";
import { proxyRequest } from "../../../../lib/backend";

export async function POST(req: NextRequest) {
  return proxyRequest(req, "auth/register");
}
`);

// auth/profile — needs bearer
writeProxy("app/api/auth/profile/route.ts", `import { NextRequest } from "next/server";
import { proxyRequest } from "../../../../lib/backend";

export async function GET(req: NextRequest) {
  return proxyRequest(req, "auth/me");
}

export async function PATCH(req: NextRequest) {
  return proxyRequest(req, "auth/profile");
}
`);

// transactions/route.ts
writeProxy("app/api/transactions/route.ts", `import { NextRequest } from "next/server";
import { proxyRequest } from "../../../../lib/backend";

export async function GET(req: NextRequest) {
  return proxyRequest(req, "transactions");
}

export async function POST(req: NextRequest) {
  return proxyRequest(req, "transactions");
}
`);

// transactions/summary
writeProxy("app/api/transactions/summary/route.ts", `import { NextRequest } from "next/server";
import { proxyRequest } from "../../../../../lib/backend";

export async function GET(req: NextRequest) {
  return proxyRequest(req, "transactions/summary");
}
`);

// transactions/cashflow
writeProxy("app/api/transactions/cashflow/route.ts", `import { NextRequest } from "next/server";
import { proxyRequest } from "../../../../../lib/backend";

export async function GET(req: NextRequest) {
  return proxyRequest(req, "transactions/cashflow");
}
`);

// transactions/merchants
writeProxy("app/api/transactions/merchants/route.ts", `import { NextRequest } from "next/server";
import { proxyRequest } from "../../../../../lib/backend";

export async function GET(req: NextRequest) {
  return proxyRequest(req, "transactions/merchants");
}
`);

// transactions/sync
writeProxy("app/api/transactions/sync/route.ts", `import { NextRequest } from "next/server";
import { proxyRequest } from "../../../../../lib/backend";

export async function POST(req: NextRequest) {
  return proxyRequest(req, "transactions/sync");
}
`);

// transactions/manual
writeProxy("app/api/transactions/manual/route.ts", `import { NextRequest } from "next/server";
import { proxyRequest } from "../../../../../lib/backend";

export async function POST(req: NextRequest) {
  return proxyRequest(req, "transactions/manual");
}
`);

// transactions/[id]/derived
writeProxy("app/api/transactions/[id]/derived/route.ts", `import { NextRequest } from "next/server";
import { proxyRequest } from "../../../../../../lib/backend";

export async function PATCH(
  req: NextRequest,
  { params }: { params: { id: string } }
) {
  return proxyRequest(req, \`transactions/\${params.id}/derived\`);
}
`);

// accounts/route.ts
writeProxy("app/api/accounts/route.ts", `import { NextRequest } from "next/server";
import { proxyRequest } from "../../../../lib/backend";

export async function GET(req: NextRequest) {
  return proxyRequest(req, "accounts");
}
`);

// accounts/link
writeProxy("app/api/accounts/link/route.ts", `import { NextRequest } from "next/server";
import { proxyRequest } from "../../../../../lib/backend";

export async function GET(req: NextRequest) {
  return proxyRequest(req, "accounts/link-token");
}
`);

// accounts/manual
writeProxy("app/api/accounts/manual/route.ts", `import { NextRequest } from "next/server";
import { proxyRequest } from "../../../../../lib/backend";

export async function POST(req: NextRequest) {
  return proxyRequest(req, "accounts/manual");
}
`);

// rules/route.ts
writeProxy("app/api/rules/route.ts", `import { NextRequest } from "next/server";
import { proxyRequest } from "../../../../lib/backend";

export async function GET(req: NextRequest) {
  return proxyRequest(req, "rules");
}

export async function POST(req: NextRequest) {
  return proxyRequest(req, "rules");
}
`);

// rules/suggestions
writeProxy("app/api/rules/suggestions/route.ts", `import { NextRequest } from "next/server";
import { proxyRequest } from "../../../../../lib/backend";

export async function GET(req: NextRequest) {
  return proxyRequest(req, "rules/suggestions");
}
`);

// exports/csv
writeProxy("app/api/exports/csv/route.ts", `import { NextRequest } from "next/server";
import { proxyRequest } from "../../../../../lib/backend";

export async function GET(req: NextRequest) {
  return proxyRequest(req, "exports/csv");
}
`);

// tax/returns
writeProxy("app/api/tax/returns/route.ts", `import { NextRequest } from "next/server";
import { proxyRequest } from "../../../../../lib/backend";

export async function GET(req: NextRequest) {
  return proxyRequest(req, "tax/returns");
}

export async function POST(req: NextRequest) {
  return proxyRequest(req, "tax/returns");
}
`);

// tax/returns/[id]/export
writeProxy("app/api/tax/returns/[id]/export/route.ts", `import { NextRequest } from "next/server";
import { proxyRequest } from "../../../../../../lib/backend";

export async function GET(
  req: NextRequest,
  { params }: { params: { id: string } }
) {
  return proxyRequest(req, \`tax/returns/\${params.id}/export\`);
}
`);

// plaid/link-token
writeProxy("app/api/plaid/link-token/route.ts", `import { NextRequest } from "next/server";
import { proxyRequest } from "../../../../../lib/backend";

export async function POST(req: NextRequest) {
  return proxyRequest(req, "plaid/link-token");
}
`);

// plaid/exchange
writeProxy("app/api/plaid/exchange/route.ts", `import { NextRequest } from "next/server";
import { proxyRequest } from "../../../../../lib/backend";

export async function POST(req: NextRequest) {
  return proxyRequest(req, "plaid/exchange");
}
`);

// ─── New proxy routes ────────────────────────────────────────────────────────

// auth/refresh
mkdirSync("app/api/auth/refresh", { recursive: true });
writeFileSync("app/api/auth/refresh/route.ts", `import { NextRequest } from "next/server";
import { proxyRequest } from "../../../../lib/backend";

export async function POST(req: NextRequest) {
  return proxyRequest(req, "auth/refresh");
}
`);

// auth/logout
mkdirSync("app/api/auth/logout", { recursive: true });
writeFileSync("app/api/auth/logout/route.ts", `import { NextRequest } from "next/server";
import { proxyRequest } from "../../../../lib/backend";

export async function POST(req: NextRequest) {
  return proxyRequest(req, "auth/logout");
}
`);

// auth/me
mkdirSync("app/api/auth/me", { recursive: true });
writeFileSync("app/api/auth/me/route.ts", `import { NextRequest } from "next/server";
import { proxyRequest } from "../../../../lib/backend";

export async function GET(req: NextRequest) {
  return proxyRequest(req, "auth/me");
}
`);

// auth/verify-email
mkdirSync("app/api/auth/verify-email", { recursive: true });
writeFileSync("app/api/auth/verify-email/route.ts", `import { NextRequest } from "next/server";
import { proxyRequest } from "../../../../lib/backend";

export async function GET(req: NextRequest) {
  return proxyRequest(req, "auth/verify-email");
}
`);

// auth/forgot-password
mkdirSync("app/api/auth/forgot-password", { recursive: true });
writeFileSync("app/api/auth/forgot-password/route.ts", `import { NextRequest } from "next/server";
import { proxyRequest } from "../../../../lib/backend";

export async function POST(req: NextRequest) {
  return proxyRequest(req, "auth/forgot-password");
}
`);

// auth/reset-password
mkdirSync("app/api/auth/reset-password", { recursive: true });
writeFileSync("app/api/auth/reset-password/route.ts", `import { NextRequest } from "next/server";
import { proxyRequest } from "../../../../lib/backend";

export async function POST(req: NextRequest) {
  return proxyRequest(req, "auth/reset-password");
}
`);

// 2fa/generate
mkdirSync("app/api/2fa/generate", { recursive: true });
writeFileSync("app/api/2fa/generate/route.ts", `import { NextRequest } from "next/server";
import { proxyRequest } from "../../../../lib/backend";

export async function POST(req: NextRequest) {
  return proxyRequest(req, "2fa/generate");
}
`);

// 2fa/enable
mkdirSync("app/api/2fa/enable", { recursive: true });
writeFileSync("app/api/2fa/enable/route.ts", `import { NextRequest } from "next/server";
import { proxyRequest } from "../../../../lib/backend";

export async function POST(req: NextRequest) {
  return proxyRequest(req, "2fa/enable");
}
`);

// 2fa/disable
mkdirSync("app/api/2fa/disable", { recursive: true });
writeFileSync("app/api/2fa/disable/route.ts", `import { NextRequest } from "next/server";
import { proxyRequest } from "../../../../lib/backend";

export async function DELETE(req: NextRequest) {
  return proxyRequest(req, "2fa/disable");
}
`);

// stripe/subscription
mkdirSync("app/api/stripe/subscription", { recursive: true });
writeFileSync("app/api/stripe/subscription/route.ts", `import { NextRequest } from "next/server";
import { proxyRequest } from "../../../../lib/backend";

export async function GET(req: NextRequest) {
  return proxyRequest(req, "stripe/subscription");
}
`);

// stripe/checkout
mkdirSync("app/api/stripe/checkout", { recursive: true });
writeFileSync("app/api/stripe/checkout/route.ts", `import { NextRequest } from "next/server";
import { proxyRequest } from "../../../../lib/backend";

export async function POST(req: NextRequest) {
  return proxyRequest(req, "stripe/checkout");
}
`);

// stripe/portal
mkdirSync("app/api/stripe/portal", { recursive: true });
writeFileSync("app/api/stripe/portal/route.ts", `import { NextRequest } from "next/server";
import { proxyRequest } from "../../../../lib/backend";

export async function POST(req: NextRequest) {
  return proxyRequest(req, "stripe/portal");
}
`);

// transactions/import (multipart CSV upload)
mkdirSync("app/api/transactions/import", { recursive: true });
writeFileSync("app/api/transactions/import/route.ts", `import { NextRequest } from "next/server";
import { proxyRequest } from "../../../../../lib/backend";

export async function POST(req: NextRequest) {
  return proxyRequest(req, "transactions/import");
}
`);

// exports/sheets
mkdirSync("app/api/exports/sheets", { recursive: true });
writeFileSync("app/api/exports/sheets/route.ts", `import { NextRequest } from "next/server";
import { proxyRequest } from "../../../../../lib/backend";

export async function POST(req: NextRequest) {
  return proxyRequest(req, "exports/sheets");
}
`);

console.log("✅ All API proxy routes written (existing updated + new created)");
