import { createKyselyMock } from "@tailor-platform/sdk/vitest";
import { describe, expect, it } from "vitest";
import type { Namespace } from "../generated/kysely-tailordb";
import {
  CompanyNotFoundError,
  DuplicateChannelNameError,
  InvalidChannelTypeError,
} from "../lib/errors.generated";
import { commandCtx, expectErr, expectOk } from "../testing/commandTestUtils";
import { baseChannel, baseCompany } from "../testing/fixtures";
import { mockOrgQueries } from "../testing/moduleMocks";
import { run } from "./createChannel";

describe("createChannel", () => {
  it("creates a draft channel with valid type and company", async () => {
    const mock = createKyselyMock<Namespace["main-db"]>();
    const created = {
      ...baseChannel,
      id: "channel-9",
      name: "Web Store",
      code: "WEB",
      status: "DRAFT" as const,
    };
    mock.enqueueResults([], [created]);

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

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

  it("throws when required Channel fields are missing", async () => {
    const mock = createKyselyMock<Namespace["main-db"]>();
    mock.enqueueResults([]);

    const result = await mock.withTx((trx) =>
      run(
        trx,
        { companyId: baseCompany.id, type: "", name: "Broken Channel" },
        commandCtx,
        mockOrgQueries(baseCompany),
      ),
    );

    expectErr(result, InvalidChannelTypeError);
  });

  it("creates the Channel in DRAFT status", async () => {
    const mock = createKyselyMock<Namespace["main-db"]>();
    const created = { ...baseChannel, id: "channel-10", status: "DRAFT" as const };
    mock.enqueueResults([], [created]);

    const result = await mock.withTx((trx) =>
      run(
        trx,
        {
          companyId: baseCompany.id,
          type: baseChannel.type,
          name: baseChannel.name,
          code: baseChannel.code ?? undefined,
        },
        commandCtx,
        mockOrgQueries(baseCompany),
      ),
    );

    expectOk(result);
    expect(mock.inserts[0].insertValues()).toEqual(
      expect.objectContaining({
        companyId: baseCompany.id,
        status: "DRAFT",
      }),
    );
  });

  it("throws when the company does not exist", async () => {
    const mock = createKyselyMock<Namespace["main-db"]>();
    mock.enqueueResults([]);

    const result = await mock.withTx((trx) =>
      run(
        trx,
        { companyId: baseCompany.id, type: baseChannel.type, name: baseChannel.name },
        commandCtx,
        mockOrgQueries(null),
      ),
    );

    expectErr(result, CompanyNotFoundError);
  });

  it("throws when a conflicting channel name already exists", async () => {
    const mock = createKyselyMock<Namespace["main-db"]>();
    mock.enqueueResults([baseChannel]);

    const result = await mock.withTx((trx) =>
      run(
        trx,
        { companyId: baseCompany.id, type: baseChannel.type, name: baseChannel.name },
        commandCtx,
        mockOrgQueries(baseCompany),
      ),
    );

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