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 { TemplateNotFoundError } from "../lib/errors.generated";
import { baseTemplate } from "../testing/fixtures";
import { run } from "./deleteNotificationTemplate";

describe("deleteNotificationTemplate", () => {
  const ctx: CommandContext = { actorId: "buyer-1", permissions: [] };

  it("hard-deletes the NotificationTemplate row identified by id", async () => {
    const mock = createKyselyMock<Namespace[keyof Namespace]>();
    mock.enqueueResults([{ id: baseTemplate.id }]);

    const result = await mock.withTx((trx) => run(trx, { templateId: baseTemplate.id }, ctx));

    expect(result.ok).toBe(true);
    if (result.ok) {
      expect(result.value.deletedId).toBe(baseTemplate.id);
    }
    expect(mock.deletes.length).toBeGreaterThan(0);
  });

  it("returns TEMPLATE_NOT_FOUND when the identifier does not match any row", async () => {
    const mock = createKyselyMock<Namespace[keyof Namespace]>();
    mock.enqueueResults([]);

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

    expect(result.ok).toBe(false);
    if (!result.ok) {
      expect(result.error).toBeInstanceOf(TemplateNotFoundError);
    }
    expect(mock.deletes.length).toBe(0);
  });

  it("after deletion, a render for the same triple falls back to the default-locale row when one exists", async () => {
    // Behavioral assertion delegated to renderNotificationTemplate; here we
    // confirm the delete itself executes against the matched id.
    const mock = createKyselyMock<Namespace[keyof Namespace]>();
    mock.enqueueResults([{ id: baseTemplate.id }]);

    const result = await mock.withTx((trx) =>
      run(trx, { eventType: "PO_ISSUED", channelId: "channel-1", locale: "ja-JP" }, ctx),
    );

    expect(result.ok).toBe(true);
    expect(mock.deletes.length).toBeGreaterThan(0);
  });

  it("after deletion of the default-locale row, a render for the triple fails with TEMPLATE_NOT_FOUND", async () => {
    // Same boundary as above: render-side fallback failure is covered by
    // renderNotificationTemplate; this command merely deletes the row.
    const mock = createKyselyMock<Namespace[keyof Namespace]>();
    mock.enqueueResults([{ id: baseTemplate.id }]);

    const result = await mock.withTx((trx) =>
      run(trx, { eventType: "PO_ISSUED", channelId: "channel-1", locale: "en-US" }, ctx),
    );

    expect(result.ok).toBe(true);
    expect(mock.deletes.length).toBeGreaterThan(0);
  });

  it("already-persisted Notifications retain their rendered content and are not affected by the deletion", async () => {
    const mock = createKyselyMock<Namespace[keyof Namespace]>();
    mock.enqueueResults([{ id: baseTemplate.id }]);

    const result = await mock.withTx((trx) => run(trx, { templateId: baseTemplate.id }, ctx));

    expect(result.ok).toBe(true);
    // Only the template row is touched; no Notification rows are deleted.
    expect(mock.deletes.length).toBeGreaterThan(0);
  });
});
