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, baseInAppChannel } from "../testing/fixtures";
import { run } from "./deactivateNotificationChannel";

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

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

    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(false);
    }
    expect(mock.updates[0].updateValues()).toEqual(expect.objectContaining({ enabled: false }));
  });

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

    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(false);
    }
    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 deactivation skips the channel and creates no Notification row for it", async () => {
    const mock = createKyselyMock<Namespace[keyof Namespace]>();
    mock.enqueueResults(
      [{ ...baseEmailChannel, enabled: true }],
      [{ ...baseEmailChannel, enabled: false }],
    );

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

    expect(result.ok).toBe(true);
    if (result.ok) {
      // Dispatcher reads enabled=false and skips the channel on subsequent fan-out.
      expect(result.value.channel.enabled).toBe(false);
    }
    expect(mock.inserts.length).toBe(0);
  });

  it("already-persisted Notifications on prior events are not mutated", async () => {
    const mock = createKyselyMock<Namespace[keyof Namespace]>();
    mock.enqueueResults(
      [{ ...baseEmailChannel, enabled: true }],
      [{ ...baseEmailChannel, enabled: false }],
    );

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

    expect(result.ok).toBe(true);
    // The command updates only the NotificationChannel registry; Notifications remain untouched.
    expect(mock.updates.length).toBe(1);
  });

  it("disabling IN_APP is allowed and results in inbox events being skipped while the flag is off", async () => {
    const mock = createKyselyMock<Namespace[keyof Namespace]>();
    mock.enqueueResults(
      [{ ...baseInAppChannel, enabled: true }],
      [{ ...baseInAppChannel, enabled: false }],
    );

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

    expect(result.ok).toBe(true);
    if (result.ok) {
      expect(result.value.channel.channelId).toBe("IN_APP");
      expect(result.value.channel.enabled).toBe(false);
    }
  });
});
