import { beforeEach, describe, expect, it } from 'vitest';
import {
  enqueuePhotoAction,
  loadPhotoQueue,
  removePhotoAction,
  replacePhotoQueue,
  type PhotoModerationAction,
} from './photoModerationQueue';

describe('photoModerationQueue', () => {
  beforeEach(() => {
    window.localStorage.clear();
  });

  it('enqueues and loads actions', () => {
    const queue = enqueuePhotoAction({ eventSlug: 'demo-event', photoId: 12, action: 'approve' });

    expect(queue).toHaveLength(1);
    const loaded = loadPhotoQueue();
    expect(loaded).toHaveLength(1);
    expect(loaded[0]?.eventSlug).toBe('demo-event');
    expect(loaded[0]?.photoId).toBe(12);
  });

  it('removes actions by id', () => {
    const queue = enqueuePhotoAction({ eventSlug: 'demo-event', photoId: 12, action: 'approve' });
    const next = removePhotoAction(queue, queue[0]!.id);

    expect(next).toHaveLength(0);
    expect(loadPhotoQueue()).toHaveLength(0);
  });

  it('replaces the queue', () => {
    enqueuePhotoAction({ eventSlug: 'demo-event', photoId: 12, action: 'approve' });
    const next: PhotoModerationAction[] = [
      {
        id: 'fixed',
        eventSlug: 'another',
        photoId: 99,
        action: 'hide',
        createdAt: new Date().toISOString(),
      },
    ];
    replacePhotoQueue(next);

    expect(loadPhotoQueue()).toHaveLength(1);
    expect(loadPhotoQueue()[0]?.id).toBe('fixed');
  });
});
