import { createKyselyMock } from "@tailor-platform/sdk/vitest";
import { describe, expect, it } from "vitest";
import type { Namespace } from "../generated/kysely-tailordb";
import { baseRootCategory } from "../testing/fixtures";
import { run } from "./getProductCategory";

describe("getProductCategory", () => {
  describe("by id", () => {
    it("returns category when found", async () => {
      const mock = createKyselyMock<Namespace["main-db"]>();
      mock.enqueueResults([baseRootCategory]);

      const result = await run(mock.db, { id: "category-1" });

      expect(result.ok).toBe(true);
      if (result.ok) {
        expect(result.value.category).toEqual(baseRootCategory);
      }
    });

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

      const result = await run(mock.db, { id: "nonexistent" });

      expect(result.ok).toBe(true);
      if (result.ok) {
        expect(result.value.category).toBeNull();
      }
    });
  });

  describe("by code", () => {
    it("returns category when found", async () => {
      const mock = createKyselyMock<Namespace["main-db"]>();
      mock.enqueueResults([baseRootCategory]);

      const result = await run(mock.db, { code: "CLOTHING" });

      expect(result.ok).toBe(true);
      if (result.ok) {
        expect(result.value.category).toEqual(baseRootCategory);
      }
    });

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

      const result = await run(mock.db, { code: "NONEXISTENT" });

      expect(result.ok).toBe(true);
      if (result.ok) {
        expect(result.value.category).toBeNull();
      }
    });
  });
});
