import { groupService } from '../groupService'; // Adjust path
import { api } from '../api'; // Actual axios instance from api.ts
import type { Group } from '../groupService'; // Adjust path

// Mock the api module (axios instance)
vi.mock('../api', () => ({
  api: {
    get: vi.fn(),
    // Add other methods like post, put, delete if they were used by groupService
  },
}));

const mockApi = api as vi.Mocked<typeof api>;

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

  describe('getUserGroups', () => {
    const mockGroups: Group[] = [
      {
        id: 1,
        name: 'Test Group 1',
        created_at: new Date().toISOString(),
        updated_at: new Date().toISOString(),
        owner_id: 1,
        members: [{ id: 1, email: 'test@example.com', role: 'owner' }],
      },
      {
        id: 2,
        name: 'Test Group 2',
        description: 'A group for testing',
        created_at: new Date().toISOString(),
        updated_at: new Date().toISOString(),
        owner_id: 2,
        members: [
          { id: 2, email: 'owner@example.com', role: 'owner' },
          { id: 3, email: 'member@example.com', role: 'member' },
        ],
      },
    ];

    it('should fetch user groups successfully', async () => {
      mockApi.get.mockResolvedValue({ data: mockGroups });

      const groups = await groupService.getUserGroups();

      expect(mockApi.get).toHaveBeenCalledWith('/api/v1/groups');
      expect(groups).toEqual(mockGroups);
    });

    it('should propagate error on failure and log it', async () => {
      const error = new Error('Failed to fetch groups');
      mockApi.get.mockRejectedValue(error);
      const consoleErrorSpy = vi.spyOn(console, 'error').mockImplementation(() => {});

      await expect(groupService.getUserGroups()).rejects.toThrow(error);

      expect(mockApi.get).toHaveBeenCalledWith('/api/v1/groups');
      expect(consoleErrorSpy).toHaveBeenCalledWith('Failed to fetch user groups:', error);

      consoleErrorSpy.mockRestore();
    });
  });

  // Tests for other functions mentioned in the subtask description (createGroup, getGroup, etc.)
  // would be added here if those functions existed in groupService.ts.
  // Since they are not present, no tests can be written for them.
  // For example, if createGroup were added:
  // describe('createGroup', () => {
  //   it('should create a group successfully', async () => {
  //     const newGroupData = { name: 'New Awesome Group', description: 'Description' };
  //     const createdGroupMock = { ...newGroupData, id: 3, created_at: '...', updated_at: '...', owner_id: 1, members: [] };
  //     mockApi.post.mockResolvedValue({ data: createdGroupMock });
  //     const group = await groupService.createGroup(newGroupData); // Assuming createGroup exists
  //     expect(mockApi.post).toHaveBeenCalledWith('/api/v1/groups', newGroupData); // Or whatever the endpoint is
  //     expect(group).toEqual(createdGroupMock);
  //   });
  // });
});
