import "server-only";
import { configureCanonicalAuthzedSchemaUrl } from "../../lib/authzed/schema-source";
import { exitAfterStdoutFlush } from "../authzed-health-process";
import { INVALID_CONFIGURATION_RESULT, INVALID_REQUEST_RESULT } from "../authzed-schema-results";

// Operator commands have a single-JSON output contract. Suppress application logging before loading
// runtime modules so retries cannot add diagnostic lines to stdout.
process.env.LOG_LEVEL = "fatal";

configureCanonicalAuthzedSchemaUrl(import.meta.url, "./schema.zed");

const HEALTH_INVALID_CONFIGURATION_RESULT = {
  code: "authzed_internal",
  latencyMs: 0,
  retryable: false,
  status: "unhealthy",
} as const;

const HEALTH_INVALID_REQUEST_RESULT = {
  code: "authzed_invalid_request",
  latencyMs: 0,
  retryable: false,
  status: "unhealthy",
} as const;

const writeResult = (result: object): void => {
  process.stdout.write(`${JSON.stringify(result)}\n`);
};

const closeDatabase = async (): Promise<void> => {
  const { prisma } = await import("@formbricks/database");

  await prisma.$disconnect();
};

const runHealthCommand = async (
  args: ReadonlyArray<string>,
  originalConsoleError: typeof console.error
): Promise<void> => {
  if (args.length !== 0) {
    console.error = originalConsoleError;
    writeResult(HEALTH_INVALID_REQUEST_RESULT);
    process.exitCode = 1;
    return;
  }

  const { runAuthzedHealthCli } = await import("../../lib/authzed/cli");
  console.error = originalConsoleError;
  process.exitCode = await runAuthzedHealthCli();
};

/** What every operator command runs with. */
interface TCommandContext {
  args: string[];
  originalConsoleError: typeof console.error;
  /** Marks that the command opened the database, so it is closed afterwards whatever happens. */
  markDatabaseUsed: () => void;
}

const runSchemaCommand = async ({ args, originalConsoleError }: TCommandContext): Promise<void> => {
  const { parseAuthzedSchemaCliCommand } = await import("../../lib/authzed/schema-cli-command");
  const schemaCommand = parseAuthzedSchemaCliCommand(args);

  if (!schemaCommand) {
    console.error = originalConsoleError;
    writeResult(INVALID_REQUEST_RESULT);
    process.exitCode = 1;
    return;
  }

  const { runAuthzedSchemaCli } = await import("../../lib/authzed/schema-cli");
  console.error = originalConsoleError;
  process.exitCode = await runAuthzedSchemaCli(schemaCommand);
};

const runBackfillCommand = async ({
  args,
  originalConsoleError,
  markDatabaseUsed,
}: TCommandContext): Promise<void> => {
  const { parseAuthzedBackfillCommand } = await import("../../lib/authzed/backfill-cli-command");
  const backfillCommand = parseAuthzedBackfillCommand(args);

  if (!backfillCommand) {
    console.error = originalConsoleError;
    writeResult(INVALID_REQUEST_RESULT);
    process.exitCode = 1;
    return;
  }

  markDatabaseUsed();
  const { runAuthzedBackfillCli } = await import("../../lib/authzed/backfill-cli");
  console.error = originalConsoleError;
  process.exitCode = await runAuthzedBackfillCli(backfillCommand);
};

const runOutboxCommand = async ({
  args,
  originalConsoleError,
  markDatabaseUsed,
}: TCommandContext): Promise<void> => {
  const { parseAuthzedOutboxCliCommand } = await import("../../lib/authzed/outbox-cli-command");
  const outboxCommand = parseAuthzedOutboxCliCommand(args);

  if (!outboxCommand) {
    console.error = originalConsoleError;
    writeResult(INVALID_REQUEST_RESULT);
    process.exitCode = 1;
    return;
  }

  markDatabaseUsed();
  const { runAuthzedOutboxCli } = await import("../../lib/authzed/outbox-cli");
  console.error = originalConsoleError;
  process.exitCode = await runAuthzedOutboxCli(outboxCommand);
};

const runUpgradeCommand = async ({
  args,
  originalConsoleError,
  markDatabaseUsed,
}: TCommandContext): Promise<void> => {
  if (args[0] === "online") {
    markDatabaseUsed();
    const { runKubernetesOnlineCli } = await import("../../lib/upgrade/online/cli");
    const result = await runKubernetesOnlineCli(args.slice(1), new URL("./", import.meta.url));
    console.error = originalConsoleError;
    writeResult(result);
    process.exitCode = result.status === "blocked" ? 1 : 0;
    return;
  }
  const { parseAuthzedUpgradeCliCommand } = await import("../../lib/authzed/upgrade-cli-command");
  const upgradeCommand = parseAuthzedUpgradeCliCommand(args);

  if (!upgradeCommand) {
    console.error = originalConsoleError;
    writeResult(INVALID_REQUEST_RESULT);
    process.exitCode = 1;
    return;
  }

  markDatabaseUsed();
  const { runAuthzedUpgradeCli } = await import("../../lib/authzed/upgrade-cli");
  console.error = originalConsoleError;
  process.exitCode = await runAuthzedUpgradeCli(upgradeCommand);
};

const run = async (): Promise<void> => {
  const [command, ...args] = process.argv.slice(2);
  const originalConsoleError = console.error;
  let shouldCloseDatabase = false;

  try {
    // Environment validation writes its own diagnostics before throwing. The operator command contract
    // is one sanitized JSON document, so suppress that duplicate output while loading runtime modules.
    console.error = () => {};

    const context: TCommandContext = {
      args,
      originalConsoleError,
      markDatabaseUsed: () => {
        shouldCloseDatabase = true;
      },
    };

    switch (command) {
      case "health": {
        await runHealthCommand(args, originalConsoleError);
        return;
      }
      case "schema":
        await runSchemaCommand(context);
        return;
      case "backfill":
        await runBackfillCommand(context);
        return;
      case "outbox":
        await runOutboxCommand(context);
        return;
      case "upgrade":
        await runUpgradeCommand(context);
        return;
      default:
        console.error = originalConsoleError;
        writeResult(INVALID_REQUEST_RESULT);
        process.exitCode = 1;
    }
  } catch {
    console.error = originalConsoleError;
    writeResult(command === "health" ? HEALTH_INVALID_CONFIGURATION_RESULT : INVALID_CONFIGURATION_RESULT);
    process.exitCode = 1;
  } finally {
    console.error = originalConsoleError;

    if (shouldCloseDatabase) {
      try {
        await closeDatabase();
      } catch {
        // Cleanup failures must not replace the command's sanitized result or exit code.
      }
    }
  }
};

void run().then(() => {
  if (process.argv[2] === "health") {
    exitAfterStdoutFlush(process.exitCode ?? 1);
  }
});
