import type { CommandContext } from "@tailor-platform/erp-kit/core";
import { describe, expect, it, vi } from "vitest";
import { createMockDb } from "../../../testing/index";
import type { Transaction } from "../generated/kysely-tailordb";
import {
  StorageLocationNotFoundError,
  StorageLocationNotInactiveError,
  SiteInactiveError,
} from "../lib/errors.generated";
import {
  baseStorageLocation,
  baseInactiveLocation,
  baseActiveSite,
  baseInactiveSite,
} from "../testing/fixtures";
import { run, type OrganizationQueries } from "./reactivateStorageLocation";

const ok = <T>(value: T) => ({ ok: true as const, value });

function mockOrgQueries(site: unknown): Pick<OrganizationQueries, "getSite"> {
  return {
    getSite: vi.fn().mockResolvedValue(ok({ site })),
  } as unknown as Pick<OrganizationQueries, "getSite">;
}

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

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

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

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

  it("returns error when already active", async () => {
    const { db, spies } = createMockDb<Transaction>();
    spies.select.mockReturnValueOnce(baseStorageLocation); // already ACTIVE

    const result = await run(
      db,
      { id: baseStorageLocation.id },
      ctx,
      mockOrgQueries(baseActiveSite),
    );

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

  it("returns error when site is inactive", async () => {
    const { db, spies } = createMockDb<Transaction>();
    spies.select.mockReturnValueOnce(baseInactiveLocation); // location found, INACTIVE

    const result = await run(
      db,
      { id: baseInactiveLocation.id },
      ctx,
      mockOrgQueries(baseInactiveSite),
    );

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

  it("reactivates inactive location", async () => {
    const { db, spies } = createMockDb<Transaction>();
    const reactivatedLocation = { ...baseInactiveLocation, status: "ACTIVE" };
    spies.select.mockReturnValueOnce(baseInactiveLocation); // location found, INACTIVE
    spies.update.mockReturnValue(reactivatedLocation);

    const result = await run(
      db,
      { id: baseInactiveLocation.id },
      ctx,
      mockOrgQueries(baseActiveSite),
    );

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