import { spawn } from "node:child_process";
import fs from "node:fs";
import path from "node:path";
import { fileURLToPath } from "node:url";
import { safeHostname, selectHealthySolanaRpcUrl } from "./lib/solana-rpc-health.mjs";

const mode = process.argv[2];
const rawForwardedArgs = process.argv.slice(3);
const forwardedArgs = rawForwardedArgs[0] === "--" ? rawForwardedArgs.slice(1) : rawForwardedArgs;

if (mode !== "unit" && mode !== "integration") {
  console.error("Usage: node scripts/run-workspace-tests.mjs <unit|integration> [test-files...]");
  process.exit(1);
}

const rootDir = path.dirname(fileURLToPath(new URL("../package.json", import.meta.url)));
const localApiEnvPath = path.resolve(rootDir, "apps/sdp-api/.env.local");

function loadLocalEnvFile(filePath) {
  if (!fs.existsSync(filePath)) {
    return {};
  }

  const content = fs.readFileSync(filePath, "utf8");
  const values = {};

  for (const line of content.split("\n")) {
    const trimmed = line.trim();
    if (!trimmed || trimmed.startsWith("#")) {
      continue;
    }

    const [key, ...rest] = trimmed.split("=");
    if (!key) {
      continue;
    }

    const raw = rest.join("=");
    const quoted = raw.match(/^(['"])(.*)\1$/);
    values[key] = quoted ? quoted[2] : raw;
  }

  return values;
}

const localEnv = loadLocalEnvFile(localApiEnvPath);
// biome-ignore lint/security/noSecrets: Local Docker Postgres fallback for isolated tests.
const localTestDatabaseUrl = "postgresql://sdp:sdp@127.0.0.1:5432/sdp_test";
const testDatabaseUrl =
  process.env.TEST_DATABASE_URL ?? localEnv.TEST_DATABASE_URL ?? localTestDatabaseUrl;
const redisUrl = process.env.REDIS_URL ?? localEnv.REDIS_URL ?? "redis://127.0.0.1:6379";

const resolvedEnv = {
  ...localEnv,
  ...process.env,
  TEST_DATABASE_URL: testDatabaseUrl,
  REDIS_URL: redisUrl,
};

function run(command, args, options = {}) {
  return new Promise((resolve, reject) => {
    const child = spawn(command, args, {
      cwd: rootDir,
      stdio: "inherit",
      env: resolvedEnv,
      ...options,
    });

    child.on("error", reject);
    child.on("exit", (code, signal) => {
      if (signal) {
        reject(new Error(`Command terminated by signal ${signal}`));
        return;
      }

      if ((code ?? 1) !== 0) {
        reject(new Error(`Command failed with exit code ${code ?? 1}`));
        return;
      }

      resolve();
    });
  });
}

try {
  if (mode === "integration") {
    await configureIntegrationSolanaRpc(resolvedEnv);
  }

  if (mode === "integration") {
    await run("pnpm", ["--filter", "@sdp/api", "db:migrate:test"]);
  }

  if (mode === "integration" && forwardedArgs.length > 0) {
    await run("pnpm", [
      "--filter",
      "@sdp/api-integration",
      "exec",
      "vitest",
      "run",
      ...forwardedArgs,
    ]);
  } else {
    const filter =
      mode === "integration" ? "--filter=@sdp/api-integration" : "--filter=!@sdp/api-integration";
    await run("pnpm", [
      "exec",
      "turbo",
      "run",
      "test",
      filter,
      ...(forwardedArgs.length > 0 ? ["--", ...forwardedArgs] : []),
    ]);
  }
} catch (error) {
  console.error(error instanceof Error ? error.message : String(error));
  process.exit(1);
}

async function configureIntegrationSolanaRpc(env) {
  const selected = await selectHealthySolanaRpcUrl(env);
  if (!selected) {
    return;
  }

  env.SOLANA_RPC_URL = selected.url;
  env.SOLANA_RPC_DEFAULT_PROVIDER = "default";
  console.log(`Using ${selected.id} Solana RPC for integration (${safeHostname(selected.url)}).`);
}
