import { describe, expect, it } from "vitest";
import { createMockDb } from "../../../testing/index";
import type { DB } from "../generated/kysely-tailordb";
import { SerialNumberNotFoundError } from "../lib/errors.generated";
import { expectErr, expectOk } from "../testing/commandTestUtils";
import { baseAvailableSerial } from "../testing/fixtures";
import { run } from "./getSerialNumberHistory";

const ledgerEntry = {
  id: "ledger-1",
  sourceType: "GOODS_RECEIPT" as const,
  sourceId: "source-1",
  dcIndicator: "DR" as const,
  itemId: "item-1",
  storageLocationId: "location-1",
  quantity: 1,
  amount: null,
  executedAt: new Date("2024-01-02T00:00:00.000Z"),
  lotId: null,
  serialNumberId: "serial-1",
  createdAt: new Date("2024-01-01T00:00:00.000Z"),
};

describe("getSerialNumberHistory", () => {
  it("returns full history ordered by executedAt ascending", async () => {
    const { db, spies } = createMockDb<DB>();
    spies.select.mockReturnValueOnce(baseAvailableSerial);
    spies.select.mockReturnValueOnce([ledgerEntry]);

    const result = await run(db, { serialNumberId: "serial-1" });
    const { entries } = expectOk<{ entries: unknown[] }>(result as never);

    expect(entries).toHaveLength(1);
  });

  it("includes sourceType and storageLocationId per entry", async () => {
    const { db, spies } = createMockDb<DB>();
    spies.select.mockReturnValueOnce(baseAvailableSerial);
    spies.select.mockReturnValueOnce([ledgerEntry]);

    const result = await run(db, { serialNumberId: "serial-1" });
    const { entries } = expectOk<{
      entries: { sourceType: string; executedAt: Date }[];
    }>(result as never);

    expect(entries[0].sourceType).toBe("GOODS_RECEIPT");
    expect(entries[0].executedAt).toEqual(new Date("2024-01-02T00:00:00.000Z"));
  });

  it("includes storage location", async () => {
    const { db, spies } = createMockDb<DB>();
    spies.select.mockReturnValueOnce(baseAvailableSerial);
    spies.select.mockReturnValueOnce([ledgerEntry]);

    const result = await run(db, { serialNumberId: "serial-1" });
    const { entries } = expectOk<{
      entries: {
        storageLocationId: string;
      }[];
    }>(result as never);

    expect(entries[0].storageLocationId).toBe("location-1");
  });

  it("includes sourceType/sourceId for traceability", async () => {
    const { db, spies } = createMockDb<DB>();
    spies.select.mockReturnValueOnce(baseAvailableSerial);
    spies.select.mockReturnValueOnce([ledgerEntry]);

    const result = await run(db, { serialNumberId: "serial-1" });
    const { entries } = expectOk<{
      entries: {
        sourceType: string;
        sourceId: string;
      }[];
    }>(result as never);

    expect(entries[0].sourceType).toBe("GOODS_RECEIPT");
    expect(entries[0].sourceId).toBe("source-1");
  });

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

    const result = await run(db, { serialNumberId: "non-existent" });

    expectErr(result as never, SerialNumberNotFoundError);
  });

  it("returns empty history for newly created serial", async () => {
    const { db, spies } = createMockDb<DB>();
    spies.select.mockReturnValueOnce(baseAvailableSerial);
    spies.select.mockReturnValueOnce([]);

    const result = await run(db, { serialNumberId: "serial-1" });
    const { entries } = expectOk<{ entries: unknown[] }>(result as never);

    expect(entries).toEqual([]);
  });
});
