import { createKyselyMock } from "@tailor-platform/sdk/vitest";
import { describe, expect, it } from "vitest";
import type { Namespace } from "../generated/kysely-tailordb";
import { InvariantViolationMultipleOpenError } from "../lib/errors.generated";
import { pendingRequest, revisionRequestedRequest } from "../testing/fixtures";
import { run } from "./getActiveApprovalRequest";

describe("getActiveApprovalRequest", () => {
  it("returns the open request when a PENDING request exists for the target", async () => {
    const mock = createKyselyMock<Namespace["main-db"]>();
    mock.enqueueResults([pendingRequest]);

    const result = await run(mock.db, {
      targetEntityType: pendingRequest.targetEntityType,
      targetEntityId: pendingRequest.targetEntityId,
    });

    expect(result.ok).toBe(true);
    if (result.ok) {
      expect(result.value.approvalRequest).toEqual(pendingRequest);
    }
  });

  it("returns the open request when a REVISION_REQUESTED request exists for the target", async () => {
    const mock = createKyselyMock<Namespace["main-db"]>();
    mock.enqueueResults([revisionRequestedRequest]);

    const result = await run(mock.db, {
      targetEntityType: revisionRequestedRequest.targetEntityType,
      targetEntityId: revisionRequestedRequest.targetEntityId,
    });

    expect(result.ok).toBe(true);
    if (result.ok) {
      expect(result.value.approvalRequest).toEqual(revisionRequestedRequest);
    }
  });

  it("returns null when no request exists for the target", async () => {
    const mock = createKyselyMock<Namespace["main-db"]>();
    mock.enqueueResults([]);

    const result = await run(mock.db, {
      targetEntityType: "Product",
      targetEntityId: "product-unknown",
    });

    expect(result.ok).toBe(true);
    if (result.ok) {
      expect(result.value.approvalRequest).toBeNull();
    }
  });

  it("returns null when only terminal requests exist for the target", async () => {
    const mock = createKyselyMock<Namespace["main-db"]>();
    // The status filter excludes terminal rows at the SQL level, so the
    // mocked select returns an empty array as the DB would.
    mock.enqueueResults([]);

    const result = await run(mock.db, {
      targetEntityType: "Product",
      targetEntityId: "product-300",
    });

    expect(result.ok).toBe(true);
    if (result.ok) {
      expect(result.value.approvalRequest).toBeNull();
    }
  });

  it("returns null when other targets have open requests but this target does not", async () => {
    const mock = createKyselyMock<Namespace["main-db"]>();
    // The targetEntityId filter narrows to this target, so the mocked
    // select returns an empty array as the DB would.
    mock.enqueueResults([]);

    const result = await run(mock.db, {
      targetEntityType: "Product",
      targetEntityId: "product-other",
    });

    expect(result.ok).toBe(true);
    if (result.ok) {
      expect(result.value.approvalRequest).toBeNull();
    }
  });

  it("raises INVARIANT_VIOLATION_MULTIPLE_OPEN when multiple non-terminal requests are found for the same target", async () => {
    const mock = createKyselyMock<Namespace["main-db"]>();
    mock.enqueueResults([pendingRequest, { ...pendingRequest, id: "request-pending-2" }]);

    const result = await run(mock.db, {
      targetEntityType: pendingRequest.targetEntityType,
      targetEntityId: pendingRequest.targetEntityId,
    });

    expect(result.ok).toBe(false);
    if (!result.ok) {
      expect(result.error).toBeInstanceOf(InvariantViolationMultipleOpenError);
    }
  });
});
