import { apiClient, API_ENDPOINTS } from '@/services/api';
import type { Group, GroupCreate, GroupUpdate, GroupPublic } from '@/types/group';
import type { ChoreHistory } from '@/types/chore';
import type { UserPublic } from '@/types/user';

export const groupService = {
  async getUserGroups(): Promise<GroupPublic[]> {
    const response = await apiClient.get(API_ENDPOINTS.GROUPS.BASE);
    return response.data;
  },

  async generateSchedule(groupId: string, data: { start_date: string; end_date: string; member_ids: number[] }): Promise<void> {
    await apiClient.post(API_ENDPOINTS.GROUPS.GENERATE_SCHEDULE(groupId), data);
  },

  async getGroupChoreHistory(groupId: string): Promise<ChoreHistory[]> {
    const response = await apiClient.get(API_ENDPOINTS.GROUPS.CHORE_HISTORY(groupId));
    return response.data;
  },

  async getGroupDetails(groupId: number): Promise<Group> {
    const response = await apiClient.get(API_ENDPOINTS.GROUPS.BASE + '/' + groupId);
    return response.data;
  },

  async createGroup(groupData: GroupCreate): Promise<Group> {
    const response = await apiClient.post(API_ENDPOINTS.GROUPS.BASE, groupData);
    return response.data;
  },

  async updateGroup(id: number, groupData: GroupUpdate): Promise<Group> {
    const response = await apiClient.put(API_ENDPOINTS.GROUPS.BY_ID(String(id)), groupData);
    return response.data;
  },

  async getGroupMembers(groupId: number): Promise<UserPublic[]> {
    const response = await apiClient.get(API_ENDPOINTS.GROUPS.MEMBERS(String(groupId)));
    return response.data;
  },
};
