/**
 * Tests for install-event + diga-banner-dismiss DB layer.
 * Both are minimal one-shot updates — but the timestamp behaviour
 * must be correct (always-now, never-clear).
 */
import { describe, expect, it, vi, beforeEach } from "vitest";

const mocks = vi.hoisted(() => ({
  profile: { update: vi.fn() },
}));

vi.mock("../../server/utils/prisma", () => ({
  usePrisma: () => ({
    profile: mocks.profile,
  }),
}));

import {
  dismissDigaBanner,
  recordInstallEvent,
} from "../../server/db/profile";

const mockProfile = mocks.profile;

beforeEach(() => {
  vi.clearAllMocks();
});

describe("recordInstallEvent", () => {
  it("sets lastInstallAt = NOW for the given user", async () => {
    mockProfile.update.mockResolvedValueOnce({});
    await recordInstallEvent("user-1");

    expect(mockProfile.update).toHaveBeenCalledWith({
      where: { id: "user-1" },
      data: expect.objectContaining({ lastInstallAt: expect.any(Date) }),
    });
    const data = mockProfile.update.mock.calls[0]?.[0]?.data as {
      lastInstallAt: Date;
    };
    expect(Math.abs(data.lastInstallAt.getTime() - Date.now())).toBeLessThan(
      5_000,
    );
  });
});

describe("dismissDigaBanner", () => {
  it("sets digaBannerDismissedAt = NOW for the given user", async () => {
    mockProfile.update.mockResolvedValueOnce({});
    await dismissDigaBanner("user-1");

    expect(mockProfile.update).toHaveBeenCalledWith({
      where: { id: "user-1" },
      data: expect.objectContaining({
        digaBannerDismissedAt: expect.any(Date),
      }),
    });
  });
});
