import { describe, expect, it } from "vitest";
import { createMockDb } from "../../../testing/index";
import type { Transaction } from "../generated/kysely-tailordb";
import {
  CompanyNotFoundError,
  DuplicateChannelNameError,
  InvalidChannelTypeError,
} from "../lib/errors.generated";
import { baseChannel, baseCompany } from "../testing/fixtures";
import { commandCtx, expectErr, expectOk } from "../testing/commandTestUtils";
import { run } from "./createChannel";

describe("createChannel", () => {
  it("creates a draft channel with valid type and company", async () => {
    const { db, spies } = createMockDb<Transaction>();
    const created = {
      ...baseChannel,
      id: "channel-9",
      name: "Web Store",
      code: "WEB",
      status: "DRAFT" as const,
    };
    spies.select.mockReturnValueOnce(baseCompany).mockReturnValueOnce([]);
    spies.insert.mockReturnValueOnce(created);

    const result = await run(
      db,
      { companyId: baseCompany.id, type: "WEBSTORE", name: "Web Store", code: "WEB" },
      commandCtx,
    );

    const value = expectOk(result);
    expect(value.channel).toEqual(created);
  });

  it("throws when required Channel fields are missing", async () => {
    const { db, spies } = createMockDb<Transaction>();
    spies.select.mockReturnValueOnce(baseCompany);

    const result = await run(
      db,
      { companyId: baseCompany.id, type: "", name: "Broken Channel" },
      commandCtx,
    );

    expectErr(result, InvalidChannelTypeError);
  });

  it("creates the Channel in DRAFT status", async () => {
    const { db, spies } = createMockDb<Transaction>();
    const created = { ...baseChannel, id: "channel-10", status: "DRAFT" as const };
    spies.select.mockReturnValueOnce(baseCompany).mockReturnValueOnce([]);
    spies.insert.mockReturnValueOnce(created);

    const result = await run(
      db,
      {
        companyId: baseCompany.id,
        type: baseChannel.type,
        name: baseChannel.name,
        code: baseChannel.code ?? undefined,
      },
      commandCtx,
    );

    expectOk(result);
    expect(spies.values).toHaveBeenCalledWith(
      expect.objectContaining({
        companyId: baseCompany.id,
        status: "DRAFT",
      }),
    );
  });

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

    const result = await run(
      db,
      { companyId: baseCompany.id, type: baseChannel.type, name: baseChannel.name },
      commandCtx,
    );

    expectErr(result, CompanyNotFoundError);
  });

  it("throws when a conflicting channel name already exists", async () => {
    const { db, spies } = createMockDb<Transaction>();
    spies.select.mockReturnValueOnce(baseCompany).mockReturnValueOnce([baseChannel]);

    const result = await run(
      db,
      { companyId: baseCompany.id, type: baseChannel.type, name: baseChannel.name },
      commandCtx,
    );

    expectErr(result, DuplicateChannelNameError);
  });
});
