/**
 * Tests for commentService
 */

import {getCommentsByEvenement, createComment, deleteComment, updateComment} from './commentService';
import http from '@/http';

describe('commentService', () => {
  beforeEach(() => {
    jest.clearAllMocks();
  });

  describe('getCommentsByEvenement', () => {
    it('shouldCallApiWithEvenementId', async () => {
      const mockComments = [
        { id: 1, texte: 'Great event!', date: '2024-01-01', heure: '14:00', auteur: { id: 1, pseudo: 'user1' } },
      ];
      (http.get as jest.Mock).mockResolvedValueOnce({ data: mockComments });

      const comments = await getCommentsByEvenement(42);

      expect(http.get).toHaveBeenCalledWith('/commentaires-evenement?evenementId=42');
      expect(comments).toEqual(mockComments);
    });
  });

  describe('createComment', () => {
    it('shouldCallApiToCreateComment', async () => {
      const mockComment = { id: 1, texte: 'Nice!', date: '2024-01-01', heure: '14:00', auteur: { id: 1, pseudo: 'user1' } };
      (http.post as jest.Mock).mockResolvedValueOnce({ data: mockComment });

      const comment = await createComment(42, 'Nice!');

      expect(http.post).toHaveBeenCalledWith('/commentaires-evenement', {
        evenementId: 42,
        texte: 'Nice!',
      });
      expect(comment).toEqual(mockComment);
    });

    it('shouldThrowErrorWhenTexteIsEmpty', async () => {
      await expect(createComment(42, '')).rejects.toThrow('Comment text is required');
    });
  });

  describe('deleteComment', () => {
    it('shouldCallApiToDeleteComment', async () => {
      (http.delete as jest.Mock).mockResolvedValueOnce({ data: null });

      await deleteComment(1);

      expect(http.delete).toHaveBeenCalledWith('/commentaires-evenement/1');
    });
  });

  it('shouldPatchOwnComment', async () => {
    const updated = {id: 3, texte: 'Super, on y retourne !', date: '2026-09-17', heure: '10:00', auteur: {id: 42, pseudo: 'breizh56'}};
    (http.patch as jest.Mock).mockResolvedValue({status: 200, data: updated});

    expect(await updateComment(3, 'Super, on y retourne !')).toEqual(updated);
    expect(http.patch).toHaveBeenCalledWith('/commentaires-evenement/3', {texte: 'Super, on y retourne !'});
  });
});
