import { describe, expect, it, vi } from "vitest";

vi.mock("../scoring-calculator", () => ({
  recalculateAffectedLeagues: vi.fn(),
}));

vi.mock("../qualifying-points", async (importOriginal) => {
  const actual = await importOriginal();
  return {
    ...(actual as Record<string, unknown>),
    recalculateParticipantQP: vi.fn(),
  };
});

import { deleteScoringEvent } from "../scoring-event";

describe("deleteScoringEvent", () => {
  it("decrements majorsCompleted for a processed zero-QP qualifying event", async () => {
    const setCalls: unknown[] = [];
    const deleteChain = { where: vi.fn().mockResolvedValue(undefined) };
    const updateChain = {
      set: vi.fn((values: unknown) => {
        setCalls.push(values);
        return { where: vi.fn().mockResolvedValue(undefined) };
      }),
    };
    const db = {
      query: {
        scoringEvents: {
          findFirst: vi.fn().mockResolvedValue({
            id: "event-1",
            sportsSeasonId: "sports-season-1",
            eventType: "major_tournament",
            isQualifyingEvent: true,
          }),
        },
        eventResults: {
          findMany: vi.fn().mockResolvedValue([
            {
              seasonParticipantId: "participant-1",
              placement: 20,
              qualifyingPointsAwarded: "0.00",
            },
          ]),
        },
        sportsSeasons: {
          findFirst: vi.fn().mockResolvedValue({
            id: "sports-season-1",
            majorsCompleted: 1,
          }),
        },
      },
      transaction: vi.fn(async (callback) => callback(db)),
      delete: vi.fn(() => deleteChain),
      update: vi.fn(() => updateChain),
    } as any;

    await deleteScoringEvent("event-1", db);

    expect(setCalls).toEqual(
      expect.arrayContaining([
        expect.objectContaining({ majorsCompleted: 0 }),
      ])
    );
  });
});
