/* eslint-disable @typescript-eslint/no-explicit-any -- required for command queue */
import { checkSetup } from "@/lib/common/status";
import { wrapThrowsAsync } from "@/lib/common/utils";
import { UpdateQueue } from "@/lib/user/update-queue";
import { type Result } from "@/types/error";

export type TCommand = (
  ...args: any[]
) => Promise<Result<void, unknown>> | Result<void, unknown> | Promise<void>;

export enum CommandType {
  Setup,
  UserAction,
  GeneralAction,
}

interface InternalQueueItem {
  command: TCommand;
  type: CommandType;
  checkSetup: boolean;
  commandArgs: any[];
}

export class CommandQueue {
  private queue: InternalQueueItem[] = [];
  private running = false;
  private resolvePromise: (() => void) | null = null;
  private commandPromise: Promise<void> | null = null;
  private static instance: CommandQueue | null = null;

  public static getInstance(): CommandQueue {
    CommandQueue.instance ??= new CommandQueue();
    return CommandQueue.instance;
  }

  public add(
    command: TCommand,
    type: CommandType,
    shouldCheckSetupFlag = true,
    ...args: any[]
  ): Promise<Result<void, unknown>> {
    return new Promise((addResolve) => {
      try {
        const newItem: InternalQueueItem = {
          command,
          type,
          checkSetup: shouldCheckSetupFlag,
          commandArgs: args,
        };

        this.queue.push(newItem);

        if (!this.running) {
          this.commandPromise = new Promise((resolve) => {
            this.resolvePromise = resolve;
            void this.run();
          });
        }

        addResolve({ ok: true, data: undefined });
      } catch (error) {
        addResolve({ ok: false, error: error as Error });
      }
    });
  }

  public async wait(): Promise<void> {
    if (this.running) {
      await this.commandPromise;
    }
  }

  private async run(): Promise<void> {
    this.running = true;

    while (this.queue.length > 0) {
      const currentItem = this.queue.shift();

      if (currentItem) await executeQueueItem(currentItem);
    }

    this.running = false;
    if (this.resolvePromise) {
      this.resolvePromise();
      this.resolvePromise = null;
      this.commandPromise = null;
    }
  }
}

/** Runs one queued command, logging (never throwing) its failure so the queue keeps draining. */
const executeQueueItem = async (item: InternalQueueItem): Promise<void> => {
  if (item.checkSetup && !checkSetup().ok) {
    console.warn(`🧱 Formbricks - Setup not complete.`);
    return;
  }

  if (item.type === CommandType.GeneralAction) {
    // first check if there are pending updates in the update queue
    const updateQueue = UpdateQueue.getInstance();
    if (!updateQueue.isEmpty()) {
      console.log("🧱 Formbricks - Waiting for pending updates to complete before executing command");
      await updateQueue.processUpdates();
    }
  }

  const executeCommand = async (): Promise<Result<void, unknown>> => {
    return (await item.command.apply(null, item.commandArgs)) as Result<void, unknown>;
  };

  const result = await wrapThrowsAsync(executeCommand)();

  if (!result.ok) {
    console.error("🧱 Formbricks - Global error: ", result.error);
  } else if (!result.data.ok) {
    console.error("🧱 Formbricks - Global error: ", result.data.error);
  }
};
