import { describe, expect, it } from 'vitest';

import {
  buildPlayerPropCandidateAuditReport,
  renderPlayerPropCandidateAuditMarkdown,
} from '../player-prop-candidate-audit';

describe('player prop candidate audit', () => {
  it('summarizes zero-candidate and thin-team findings by league', () => {
    const report = buildPlayerPropCandidateAuditReport({
      auditedAt: '2026-04-01T16:00:00.000Z',
      events: [
        {
          league: 'wnba',
          eventId: 'evt-1',
          matchup: 'LAS @ IND',
          startsAt: '2026-04-01T23:00:00.000Z',
          home: {
            teamName: 'Indiana Fever',
            teamShort: 'IND',
            candidateCount: 0,
            twoWayCandidateCount: 0,
            oneWayCandidateCount: 0,
            gapFilledCandidateCount: 0,
            topStatTypes: [],
          },
          away: {
            teamName: 'Las Vegas Aces',
            teamShort: 'LAS',
            candidateCount: 1,
            twoWayCandidateCount: 0,
            oneWayCandidateCount: 1,
            gapFilledCandidateCount: 0,
            topStatTypes: [{ statType: 'points', count: 1 }],
          },
        },
        {
          league: 'nba',
          eventId: 'evt-2',
          matchup: 'UTA @ PHX',
          startsAt: '2026-04-01T23:30:00.000Z',
          home: {
            teamName: 'Phoenix Suns',
            teamShort: 'PHX',
            candidateCount: 3,
            twoWayCandidateCount: 2,
            oneWayCandidateCount: 1,
            gapFilledCandidateCount: 1,
            topStatTypes: [{ statType: 'points', count: 2 }],
          },
          away: {
            teamName: 'Utah Jazz',
            teamShort: 'UTA',
            candidateCount: 2,
            twoWayCandidateCount: 2,
            oneWayCandidateCount: 0,
            gapFilledCandidateCount: 0,
            topStatTypes: [{ statType: 'assists', count: 1 }],
          },
        },
      ],
    });

    expect(report.summary).toMatchObject({
      events: 2,
      teams: 4,
      zeroCandidateTeams: 1,
      zeroCandidateEvents: 0,
      noTwoWayTeams: 1,
      thinTeams: 1,
      twoWayCandidates: 4,
      oneWayCandidates: 2,
      avgCandidatesPerTeam: 1.5,
    });
    expect(report.issueEventCount).toBe(1);
    expect(report.passed).toBe(false);
    expect(report.events[0]?.issues).toEqual(['home_zero_candidates', 'away_no_two_way_candidates', 'away_thin_surface']);
    expect(report.leagues).toEqual(expect.arrayContaining([
      expect.objectContaining({ league: 'wnba', zeroCandidateTeams: 1, noTwoWayTeams: 1, thinTeams: 1 }),
      expect.objectContaining({ league: 'nba', zeroCandidateTeams: 0, thinTeams: 0 }),
    ]));
  });

  it('renders a markdown audit summary', () => {
    const report = buildPlayerPropCandidateAuditReport({
      auditedAt: '2026-04-01T16:00:00.000Z',
      events: [{
        league: 'nhl',
        eventId: 'evt-3',
        matchup: 'TOR @ MTL',
        startsAt: '2026-04-01T23:00:00.000Z',
        home: {
          teamName: 'Montreal Canadiens',
          teamShort: 'MTL',
          candidateCount: 0,
          twoWayCandidateCount: 0,
          oneWayCandidateCount: 0,
          gapFilledCandidateCount: 0,
          topStatTypes: [],
        },
        away: {
          teamName: 'Toronto Maple Leafs',
          teamShort: 'TOR',
          candidateCount: 0,
          twoWayCandidateCount: 0,
          oneWayCandidateCount: 0,
          gapFilledCandidateCount: 0,
          topStatTypes: [],
        },
      }],
    });

    const markdown = renderPlayerPropCandidateAuditMarkdown(report);
    expect(markdown).toContain('# Player Prop Candidate Audit');
    expect(markdown).toContain('Zero-candidate teams: 2');
    expect(markdown).toContain('Teams with no two-way candidates: 0');
    expect(markdown).toContain('TOR @ MTL (NHL)');
    expect(markdown).toContain('Issues: home_zero_candidates, away_zero_candidates, event_zero_candidates');
  });
});
