import type { CommandContext } from "@tailor-platform/erp-kit/core";
import { createKyselyMock } from "@tailor-platform/sdk/vitest";
import { describe, expect, it } from "vitest";
import type { Namespace } from "../generated/kysely-tailordb";
import { ChannelNotFoundError } from "../lib/errors.generated";
import { baseEmailChannel, baseDisabledEmailChannel } from "../testing/fixtures";
import { run } from "./activateNotificationChannel";

const ctx: CommandContext = { actorId: "admin-1", permissions: [] };

describe("activateNotificationChannel", () => {
  it("flips enabled from false to true on an existing NotificationChannel row", async () => {
    const mock = createKyselyMock<Namespace[keyof Namespace]>();
    mock.enqueueResults(
      [{ ...baseDisabledEmailChannel }],
      [{ ...baseDisabledEmailChannel, enabled: true }],
    );

    const result = await mock.withTx((trx) => run(trx, { channelId: "EMAIL" }, ctx));

    expect(result.ok).toBe(true);
    if (result.ok) {
      expect(result.value.channel.enabled).toBe(true);
    }
    expect(mock.updates[0].updateValues()).toEqual(expect.objectContaining({ enabled: true }));
  });

  it("returns the existing row unchanged when enabled is already true (idempotent)", async () => {
    const mock = createKyselyMock<Namespace[keyof Namespace]>();
    mock.enqueueResults([{ ...baseEmailChannel, enabled: true }]);

    const result = await mock.withTx((trx) => run(trx, { channelId: "EMAIL" }, ctx));

    expect(result.ok).toBe(true);
    if (result.ok) {
      expect(result.value.channel.enabled).toBe(true);
    }
    expect(mock.updates.length).toBe(0);
  });

  it("returns CHANNEL_NOT_FOUND when the channelId does not exist", async () => {
    const mock = createKyselyMock<Namespace[keyof Namespace]>();
    mock.enqueueResults([]);

    const result = await mock.withTx((trx) => run(trx, { channelId: "EMAIL" }, ctx));

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

  it("the next dispatch after activation includes the channel in fan-out", async () => {
    const mock = createKyselyMock<Namespace[keyof Namespace]>();
    mock.enqueueResults(
      [{ ...baseDisabledEmailChannel }],
      [{ ...baseDisabledEmailChannel, enabled: true }],
    );

    const result = await mock.withTx((trx) => run(trx, { channelId: "EMAIL" }, ctx));

    expect(result.ok).toBe(true);
    if (result.ok) {
      // After activation the row's enabled flag is true so dispatcher fan-out will include it.
      expect(result.value.channel.enabled).toBe(true);
    }
  });

  it("activation does not retroactively persist Notifications for events emitted while the channel was disabled", async () => {
    const mock = createKyselyMock<Namespace[keyof Namespace]>();
    mock.enqueueResults(
      [{ ...baseDisabledEmailChannel }],
      [{ ...baseDisabledEmailChannel, enabled: true }],
    );

    const result = await mock.withTx((trx) => run(trx, { channelId: "EMAIL" }, ctx));

    expect(result.ok).toBe(true);
    // Activation never inserts Notification rows; only the registry row is touched.
    expect(mock.inserts.length).toBe(0);
  });
});
