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

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

describe("assignRoleToPartner", () => {
  it("assigns CUSTOMER to partner with no roles", async () => {
    const mock = createKyselyMock<Namespace["main-db"]>();
    mock.enqueueResults([baseActivePartner], [], [{ ...baseCustomerRole, id: "role-new" }]);

    const result = await mock.withTx((trx) =>
      run(trx, { 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 mock = createKyselyMock<Namespace["main-db"]>();
    mock.enqueueResults([baseActivePartner], [], [{ ...baseSupplierRole, id: "role-new" }]);

    const result = await mock.withTx((trx) =>
      run(trx, { 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 mock = createKyselyMock<Namespace["main-db"]>();
    mock.enqueueResults([baseActivePartner], [baseCustomerRole]);

    const result = await mock.withTx((trx) =>
      run(trx, { partnerId: "partner-2", role: "CUSTOMER" }, ctx),
    );

    expect(result.ok).toBe(true);
    if (result.ok) {
      expect(result.value.partnerRole).toEqual(baseCustomerRole);
    }
    expect(mock.inserts).toHaveLength(0);
  });

  it("returns error when partner not found", async () => {
    const mock = createKyselyMock<Namespace["main-db"]>();
    mock.enqueueResults([]);

    const result = await mock.withTx((trx) =>
      run(trx, { 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 mock = createKyselyMock<Namespace["main-db"]>();
    mock.enqueueResults([baseInactivePartner]); // partner in INACTIVE

    const result = await mock.withTx((trx) =>
      run(trx, { 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 mock = createKyselyMock<Namespace["main-db"]>();
    mock.enqueueResults([baseDraftPartner]); // partner in DRAFT

    const result = await mock.withTx((trx) =>
      run(trx, { partnerId: "partner-1", role: "CUSTOMER" }, ctx),
    );

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