import { describe, expect, it } from "vitest";
import type { ApplicationDetail } from "@ema-platform/api";
import { resolveActions } from "./actions";

const REASONS = {
	wrongStatus: "wrong status",
	notAssigned: "not assigned",
	noPermission: "no permission",
	needsFlags: "needs flags",
	needsCapital: "needs capital",
	needsInspection: "needs inspection",
	needsDocumentReviews: "needs document reviews",
	inspectionNotYetDue: "not yet due",
};

/** Held by someone else, so ownership gating is what the assertions turn on. */
function detailFor(licenseTypeKey: string): ApplicationDetail {
	return {
		application: {
			id: "app-1",
			status: "SUBMITTED",
			assignedOfficerId: "another-officer",
			licenseType: { key: licenseTypeKey },
		},
		availableEvents: ["claim", "assign", "request-adjustment"],
	} as unknown as ApplicationDetail;
}

function resolve(licenseTypeKey: string) {
	return resolveActions({
		detail: detailFor(licenseTypeKey),
		currentUserId: "me",
		can: () => true,
		reasons: REASONS,
		flaggedCount: 1,
		hasPendingInspection: false,
		inspectionNotYetDue: false,
		allDocumentsAccepted: true,
	});
}

describe("resolveActions — seafarer certificates skip the queue", () => {
	it("drops claim and assign for a CoC", () => {
		const ids = resolve("CERTIFICATE_OF_COMPETENCY").map((a) => a.id);
		expect(ids).not.toContain("claim");
		expect(ids).not.toContain("assign");
		expect(ids).not.toContain("assign-reviewer");
	});

	it("lets an officer act on a CoP another officer holds", () => {
		const adjust = resolve("CERTIFICATE_OF_PROFICIENCY").find(
			(a) => a.id === "request-adjustment",
		);
		expect(adjust?.enabled).toBe(true);
	});

	it("leaves an ordinary licence claimed and owner-gated", () => {
		const actions = resolve("FREIGHT_FORWARDER");
		expect(actions.map((a) => a.id)).toContain("claim");
		const adjust = actions.find((a) => a.id === "request-adjustment");
		expect(adjust?.enabled).toBe(false);
		expect(adjust?.disabledReason).toBe(REASONS.notAssigned);
	});
});
