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

import {
  pickCatalogCardForPayload,
  submissionPayloadMatchesSet,
} from '../../lib/card-import/reconcile-submissions.js';

describe('submissionPayloadMatchesSet', () => {
  it('matches by set code or set name for the imported game', () => {
    const imported = { game: 'pokemon', setCode: 'sv10', setName: 'Perfect Order' };

    expect(
      submissionPayloadMatchesSet(
        { name: 'Seel', setCode: 'sv10', game: 'Pokemon' },
        imported
      )
    ).toBe(true);

    expect(
      submissionPayloadMatchesSet(
        { name: 'Seel', set: 'Perfect Order', game: 'pokemon' },
        imported
      )
    ).toBe(true);

    expect(
      submissionPayloadMatchesSet(
        { name: 'Seel', setCode: 'sv9', game: 'Pokemon' },
        imported
      )
    ).toBe(false);

    expect(
      submissionPayloadMatchesSet({ name: 'Bolt', setCode: 'fin', game: 'MTG' }, {
        game: 'pokemon',
        setCode: 'sv10',
        setName: 'Perfect Order',
      })
    ).toBe(false);
  });
});

describe('pickCatalogCardForPayload', () => {
  const catalogRows = [
    { id: 1, card_number: '015/208' },
    { id: 2, card_number: '016/208' },
  ];

  it('matches a specific printing when the collector number aligns', () => {
    expect(
      pickCatalogCardForPayload(catalogRows, {
        name: 'Seel',
        cardNumber: '15/208',
      })
    ).toBe(1);
  });

  it('returns null when the collector number is ambiguous or missing among siblings', () => {
    expect(
      pickCatalogCardForPayload(catalogRows, {
        name: 'Seel',
        cardNumber: '99/208',
      })
    ).toBeNull();

    expect(
      pickCatalogCardForPayload(catalogRows, {
        name: 'Seel',
      })
    ).toBeNull();
  });

  it('matches name-only payloads when exactly one catalog row exists', () => {
    expect(
      pickCatalogCardForPayload([{ id: 42, card_number: '1/100' }], {
        name: 'Pikachu',
      })
    ).toBe(42);
  });
});
