import { describe, expect, it } from "vitest";
import { createMockDb } from "../../../testing/index";
import type { Transaction } from "../generated/kysely-tailordb";
import {
  PartnerNotFoundError,
  PartnerNotActiveError,
  InvalidRoleError,
} from "../lib/errors.generated";
import {
  baseActivePartner,
  baseDraftPartner,
  baseInactivePartner,
  baseCustomerRole,
  baseSupplierRole,
} from "../testing/fixtures";
import { run } from "./assignRoleToPartner";
import type { CommandContext } from "@tailor-platform/erp-kit/module";

const ctx: CommandContext = {
  actorId: "test-actor",
  permissions: ["business-partner:assignRoleToPartner"],
};

describe("assignRoleToPartner", () => {
  it("assigns CUSTOMER to partner with no roles", async () => {
    const { db, spies } = createMockDb<Transaction>();
    spies.select.mockReturnValueOnce(baseActivePartner); // partner lookup
    spies.select.mockReturnValueOnce(undefined); // no existing role
    spies.insert.mockReturnValue({ ...baseCustomerRole, id: "role-new" });

    const result = await run(db, { partnerId: "partner-2", role: "CUSTOMER" }, ctx);

    expect(result.ok).toBe(true);
    if (result.ok) {
      expect(result.value.partnerRole).toBeDefined();
    }
  });

  it("assigns SUPPLIER to partner that already holds CUSTOMER", async () => {
    const { db, spies } = createMockDb<Transaction>();
    spies.select.mockReturnValueOnce(baseActivePartner); // partner lookup
    spies.select.mockReturnValueOnce(undefined); // no existing SUPPLIER role
    spies.insert.mockReturnValue({ ...baseSupplierRole, id: "role-new" });

    const result = await run(db, { partnerId: "partner-2", role: "SUPPLIER" }, ctx);

    expect(result.ok).toBe(true);
    if (result.ok) {
      expect(result.value.partnerRole).toBeDefined();
    }
  });

  it("idempotent when role already assigned", async () => {
    const { db, spies } = createMockDb<Transaction>();
    spies.select.mockReturnValueOnce(baseActivePartner); // partner lookup
    spies.select.mockReturnValueOnce(baseCustomerRole); // existing role found

    const result = await run(db, { partnerId: "partner-2", role: "CUSTOMER" }, ctx);

    expect(result.ok).toBe(true);
    if (result.ok) {
      expect(result.value.partnerRole).toEqual(baseCustomerRole);
    }
    expect(spies.insert).not.toHaveBeenCalled();
  });

  it("returns error when partner not found", async () => {
    const { db, spies } = createMockDb<Transaction>();
    spies.select.mockReturnValueOnce(undefined); // partner not found

    const result = await run(db, { partnerId: "nonexistent", role: "CUSTOMER" }, ctx);

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

  it("returns error when partner is INACTIVE", async () => {
    const { db, spies } = createMockDb<Transaction>();
    spies.select.mockReturnValueOnce(baseInactivePartner); // partner in INACTIVE

    const result = await run(db, { partnerId: "partner-3", role: "CUSTOMER" }, ctx);

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

  it("returns error when partner is DRAFT", async () => {
    const { db, spies } = createMockDb<Transaction>();
    spies.select.mockReturnValueOnce(baseDraftPartner); // partner in DRAFT

    const result = await run(db, { partnerId: "partner-1", role: "CUSTOMER" }, ctx);

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

  it("returns error when role is invalid", async () => {
    const { db } = createMockDb<Transaction>();

    const result = await run(db, { partnerId: "partner-2", role: "INVALID_ROLE" }, ctx);

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