import { describe, expect, it } from 'vitest';
import {
  DEFAULT_SCANNER_DESTINATION,
  destinationActionKey,
  mergeScannedCardEntry,
} from '../../lib/scanner-session.js';

describe('destinationActionKey', () => {
  it('maps owned destination to owned action key', () => {
    expect(destinationActionKey(DEFAULT_SCANNER_DESTINATION)).toBe('owned');
  });

  it('returns pending when destination is missing', () => {
    expect(destinationActionKey(null)).toBe('pending');
  });
});

describe('mergeScannedCardEntry', () => {
  const scanDefaults = { condition: 'NM', isFoil: false };

  it('creates a new queue entry with scan defaults', () => {
    const { cardEntry, scannedCards, merged } = mergeScannedCardEntry([], {
      name: 'Pikachu',
      set: 'Base',
      databaseId: 1,
    }, scanDefaults);

    expect(merged).toBe(false);
    expect(scannedCards).toHaveLength(1);
    expect(cardEntry).toMatchObject({
      name: 'Pikachu',
      set: 'Base',
      quantity: 1,
      condition: 'NM',
      isFoil: false,
      processed: false,
    });
    expect(cardEntry.id).toBeTruthy();
  });

  it('increments quantity for an unprocessed duplicate name+set', () => {
    const existing = {
      id: 99,
      name: 'Bolt',
      set: 'Alpha',
      quantity: 2,
      processed: false,
    };

    const { cardEntry, scannedCards, merged } = mergeScannedCardEntry(
      [existing],
      { name: 'Bolt', set: 'Alpha', databaseId: 1, scanImageUrl: 'https://blob/new.jpg' },
      scanDefaults
    );

    expect(merged).toBe(true);
    expect(scannedCards).toHaveLength(1);
    expect(cardEntry.quantity).toBe(3);
    expect(cardEntry.scanImageUrl).toBe('https://blob/new.jpg');
  });
});
