import { describe, it, expect, vi, beforeEach } from 'vitest';
import { groupService } from './groupService';

describe('groupService', () => {
  beforeEach(() => {
    vi.clearAllMocks();
  });

  describe('list', () => {
    it('should return list of groups', async () => {
      const result = await groupService.list();

      expect(result).toBeDefined();
      expect(result).toHaveProperty('groups');
      expect(Array.isArray(result.groups)).toBe(true);
      expect(result.groups.length).toBeGreaterThan(0);
      expect(result.groups[0]).toHaveProperty('id');
      expect(result.groups[0]).toHaveProperty('name');
    });
  });

  describe('get', () => {
    it('should return group details', async () => {
      const result = await groupService.get('g1');

      expect(result).toBeDefined();
      expect(result).toHaveProperty('group');
      expect(result.group).toHaveProperty('id');
      expect(result).toHaveProperty('membersList');
      expect(result).toHaveProperty('events');
    });
  });

  describe('create', () => {
    it('should create a new group', async () => {
      const newGroup = await groupService.create({
        name: 'Test Group',
        description: 'Test description',
      });

      expect(newGroup).toBeDefined();
      expect(newGroup).toHaveProperty('id');
      expect(newGroup.name).toBe('Test Group');
      expect(newGroup.userRole).toBe('admin');
    });
  });

  describe('join', () => {
    it('should join a group', async () => {
      const result = await groupService.join('g1');

      expect(result).toBeDefined();
      expect(result.success).toBe(true);
    });
  });

  describe('leave', () => {
    it('should leave a group', async () => {
      const result = await groupService.leave('g1');

      expect(result).toBeDefined();
      expect(result.success).toBe(true);
    });
  });
});
