import { describe, expect, it } from "vitest";
import { createMockDb } from "../../../testing/index";
import type { Transaction } from "../generated/kysely-tailordb";
import {
  DepartmentNotFoundError,
  InvalidStateError,
  HasActiveChildrenError,
} from "../lib/errors.generated";
import { activeDepartment, inactiveDepartment } from "../testing/fixtures";
import { run } from "./deactivateDepartment";

describe("deactivateDepartment", () => {
  it("deactivates ACTIVE department with no sub-departments", async () => {
    const { db, spies } = createMockDb<Transaction>();
    const deactivated = {
      ...activeDepartment,
      status: "INACTIVE",
      updatedAt: new Date(),
    };

    spies.select
      .mockReturnValueOnce(activeDepartment) // find department
      .mockReturnValueOnce(undefined); // no active children
    spies.update.mockReturnValue(deactivated);

    const result = await run(db, {
      departmentId: activeDepartment.id,
    });

    expect(result.ok).toBe(true);
    if (result.ok) {
      expect(result.value.department.status).toBe("INACTIVE");
      expect(result.value.department.updatedAt).not.toBeNull();
    }
    expect(spies.update).toHaveBeenCalled();
  });

  it("deactivates ACTIVE department with only INACTIVE sub-departments", async () => {
    const { db, spies } = createMockDb<Transaction>();
    const deactivated = {
      ...activeDepartment,
      status: "INACTIVE",
      updatedAt: new Date(),
    };

    spies.select
      .mockReturnValueOnce(activeDepartment) // find department
      .mockReturnValueOnce(undefined); // no ACTIVE children (only inactive ones)
    spies.update.mockReturnValue(deactivated);

    const result = await run(db, {
      departmentId: activeDepartment.id,
    });

    expect(result.ok).toBe(true);
    if (result.ok) {
      expect(result.value.department.status).toBe("INACTIVE");
    }
  });

  it("throws when department does not exist", async () => {
    const { db, spies } = createMockDb<Transaction>();
    spies.select.mockReturnValue(undefined);

    const result = await run(db, {
      departmentId: "nonexistent-dept",
    });

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

  it("throws when department is already INACTIVE", async () => {
    const { db, spies } = createMockDb<Transaction>();
    spies.select.mockReturnValueOnce(inactiveDepartment);

    const result = await run(db, {
      departmentId: inactiveDepartment.id,
    });

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

  it("throws when department has active sub-departments", async () => {
    const { db, spies } = createMockDb<Transaction>();
    const activeChild = {
      id: "child-dept",
      code: "CHILD",
      name: "Child Dept",
      companyId: activeDepartment.companyId,
      parentDepartmentId: activeDepartment.id,
      status: "ACTIVE",
      createdAt: new Date(),
      updatedAt: null,
    };

    spies.select
      .mockReturnValueOnce(activeDepartment) // find department
      .mockReturnValueOnce(activeChild); // active child found

    const result = await run(db, {
      departmentId: activeDepartment.id,
    });

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