import { describe, expect, it } from "vitest";
import { createMockDb } from "../../../testing/index";
import type { Transaction } from "../generated/kysely-tailordb";
import {
  WarehouseNotFoundError,
  WarehouseNotInactiveError,
  SiteInactiveError,
} from "../lib/errors.generated";
import { baseActiveWarehouse, baseInactiveWarehouse } from "../testing/fixtures";
import { run, type OrganizationQueries } from "./reactivateWarehouse";
import type { CommandContext } from "@tailor-platform/erp-kit/module";

describe("reactivateWarehouse", () => {
  const ctx: CommandContext = {
    actorId: "test-actor",
    permissions: ["inventory:reactivateWarehouse"],
  };

  const activeSite = { id: "site-1", name: "Main Site", status: "ACTIVE" };
  const inactiveSite = { id: "site-1", name: "Main Site", status: "INACTIVE" };

  const makeQueries = (
    site: { id: string; status: string; name: string } | null,
  ): Pick<OrganizationQueries, "getSite"> => ({
    getSite: () =>
      Promise.resolve({
        ok: true as const,
        // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment, @typescript-eslint/no-explicit-any
        value: { site: site as any },
      }),
  });

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

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

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

  it("returns error when warehouse already active", async () => {
    const { db, spies } = createMockDb<Transaction>();
    spies.select.mockReturnValueOnce(baseActiveWarehouse);
    const queries = makeQueries(activeSite);

    const result = await run(db, { id: baseActiveWarehouse.id }, ctx, queries);

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

  it("returns error when site is inactive", async () => {
    const { db, spies } = createMockDb<Transaction>();
    spies.select.mockReturnValueOnce(baseInactiveWarehouse);
    const queries = makeQueries(inactiveSite);

    const result = await run(db, { id: baseInactiveWarehouse.id }, ctx, queries);

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

  it("reactivates inactive warehouse", async () => {
    const { db, spies } = createMockDb<Transaction>();
    const reactivatedWarehouse = { ...baseInactiveWarehouse, status: "ACTIVE" };
    spies.select.mockReturnValueOnce(baseInactiveWarehouse);
    spies.update.mockReturnValue(reactivatedWarehouse);
    const queries = makeQueries(activeSite);

    const result = await run(db, { id: baseInactiveWarehouse.id }, ctx, queries);

    expect(result.ok).toBe(true);
    if (result.ok) {
      expect(result.value.warehouse.status).toBe("ACTIVE");
    }
    expect(spies.update).toHaveBeenCalled();
  });
});
