// @vitest-environment jsdom
import { describe, it, expect } from "vitest";
import type { Approval } from "@paperclipai/shared";
import { countPendingGates } from "./useGateIndicator";

const NOW = new Date("2026-04-11T12:00:00Z");

function makeApproval(overrides: Partial<Approval> = {}): Approval {
  return {
    id: `gate-${Math.random().toString(36).slice(2, 8)}`,
    companyId: "co-1",
    type: "generic",
    requestedByAgentId: null,
    requestedByUserId: null,
    status: "pending",
    payload: {},
    decisionNote: null,
    decidedByUserId: null,
    decidedAt: null,
    createdAt: NOW,
    updatedAt: NOW,
    ...overrides,
  } as Approval;
}

describe("countPendingGates", () => {
  it("returns 0 for undefined or empty lists", () => {
    expect(countPendingGates(undefined)).toBe(0);
    expect(countPendingGates([])).toBe(0);
  });

  it("counts approvals with status 'pending'", () => {
    const approvals = [
      makeApproval({ status: "pending" }),
      makeApproval({ status: "pending" }),
      makeApproval({ status: "approved" }),
    ];
    expect(countPendingGates(approvals)).toBe(2);
  });

  it("counts approvals with status 'revision_requested' as pending", () => {
    const approvals = [
      makeApproval({ status: "revision_requested" }),
      makeApproval({ status: "pending" }),
      makeApproval({ status: "approved" }),
      makeApproval({ status: "rejected" }),
    ];
    expect(countPendingGates(approvals)).toBe(2);
  });

  it("does not count approved or rejected approvals", () => {
    const approvals = [
      makeApproval({ status: "approved" }),
      makeApproval({ status: "rejected" }),
    ];
    expect(countPendingGates(approvals)).toBe(0);
  });

  it("derives 'hasPendingGates' correctly — count > 0 → true, count === 0 → false", () => {
    expect(countPendingGates([makeApproval({ status: "pending" })]) > 0).toBe(true);
    expect(countPendingGates([makeApproval({ status: "approved" })]) > 0).toBe(false);
  });
});
