import { createKyselyMock } from "@tailor-platform/sdk/vitest";
import { describe, expect, it } from "vitest";
import type { Namespace } from "../generated/kysely-tailordb";
import { InvalidListingStatusError } from "../lib/errors.generated";
import { commandCtx, expectErr, expectOk } from "../testing/commandTestUtils";
import { baseListing, draftListing, pausedListing } from "../testing/fixtures";
import { run } from "./pauseListing";

describe("pauseListing", () => {
  it("pauses an active listing", async () => {
    const mock = createKyselyMock<Namespace["main-db"]>();
    mock.enqueueResults([baseListing], [pausedListing]);

    const result = await mock.withTx((trx) => run(trx, { listingId: baseListing.id }, commandCtx));

    const value = expectOk(result);
    expect(value.listing.status).toBe("PAUSED");
  });

  it("preserves identifiers when listing is paused", async () => {
    const mock = createKyselyMock<Namespace["main-db"]>();
    mock.enqueueResults([baseListing], [pausedListing]);

    const result = await mock.withTx((trx) => run(trx, { listingId: baseListing.id }, commandCtx));

    const value = expectOk(result);
    expect(value.listing.externalIdentifier).toBe(baseListing.externalIdentifier);
  });

  it("throws when listing is not active", async () => {
    const mock = createKyselyMock<Namespace["main-db"]>();
    mock.enqueueResults([draftListing]);

    const result = await mock.withTx((trx) => run(trx, { listingId: draftListing.id }, commandCtx));

    expectErr(result, InvalidListingStatusError);
  });
});
