import { api } from './api' import type { Chore, ChoreCreate, ChoreUpdate, ChoreType, ChoreAssignment, ChoreAssignmentCreate, ChoreAssignmentUpdate, ChoreHistory } from '../types/chore' import { groupService } from './groupService' import { apiClient, API_ENDPOINTS } from '@/services/api' import type { Group } from '@/types/group' export const choreService = { async getAllChores(): Promise { try { const response = await api.get('/chores/all') return response.data } catch (error) { console.error('Failed to get all chores via optimized endpoint, falling back to individual calls:', error) return this.getAllChoresFallback() } }, async getAllChoresFallback(): Promise { let allChores: Chore[] = [] try { const personalChores = await this.getPersonalChores() allChores = allChores.concat(personalChores) const userGroups: Group[] = await groupService.getUserGroups() for (const group of userGroups) { try { const groupChores = await this.getChores(group.id) allChores = allChores.concat(groupChores) } catch (groupError) { console.error(`Failed to get chores for group ${group.id} (${group.name}):`, groupError) } } } catch (error) { console.error('Failed to get all chores:', error) throw error } return allChores }, async getChores(groupId: number): Promise { const response = await api.get(`/api/v1/chores/groups/${groupId}/chores`) return response.data }, async createChore(chore: ChoreCreate): Promise { if (chore.type === 'personal') { const response = await api.post('/api/v1/chores/personal', chore) return response.data } else if (chore.type === 'group' && chore.group_id) { const response = await api.post(`/api/v1/chores/groups/${chore.group_id}/chores`, chore) return response.data } else { throw new Error('Invalid chore type or missing group_id for group chore') } }, async updateChore(choreId: number, chore: ChoreUpdate): Promise { if (chore.type === 'personal') { const response = await api.put(`/api/v1/chores/personal/${choreId}`, chore) return response.data } else if (chore.type === 'group' && chore.group_id) { const response = await api.put( `/api/v1/chores/groups/${chore.group_id}/chores/${choreId}`, chore, ) return response.data } else { throw new Error('Invalid chore type or missing group_id for group chore update') } }, async deleteChore(choreId: number, choreType: ChoreType, groupId?: number): Promise { if (choreType === 'personal') { await api.delete(`/api/v1/chores/personal/${choreId}`) } else if (choreType === 'group' && groupId) { await api.delete(`/api/v1/chores/groups/${groupId}/chores/${choreId}`) } else { throw new Error('Invalid chore type or missing group_id for group chore deletion') } }, async getPersonalChores(): Promise { const response = await api.get('/api/v1/chores/personal') return response.data }, async createAssignment(assignment: ChoreAssignmentCreate): Promise { const response = await api.post('/api/v1/chores/assignments', assignment) return response.data }, async getMyAssignments(includeCompleted: boolean = false): Promise { const response = await api.get(`/api/v1/chores/assignments/my?include_completed=${includeCompleted}`) return response.data }, async getChoreAssignments(choreId: number): Promise { const response = await api.get(`/api/v1/chores/chores/${choreId}/assignments`) return response.data }, async updateAssignment(assignmentId: number, update: ChoreAssignmentUpdate): Promise { const response = await apiClient.put(`/api/v1/chores/assignments/${assignmentId}`, update) return response.data }, async deleteAssignment(assignmentId: number): Promise { await api.delete(`/api/v1/chores/assignments/${assignmentId}`) }, async completeAssignment(assignmentId: number): Promise { const response = await api.patch(`/api/v1/chores/assignments/${assignmentId}/complete`) return response.data }, async _original_updateGroupChore( groupId: number, choreId: number, chore: ChoreUpdate, ): Promise { const response = await api.put(`/api/v1/chores/groups/${groupId}/chores/${choreId}`, chore) return response.data }, async _original_deleteGroupChore(groupId: number, choreId: number): Promise { await api.delete(`/api/v1/chores/groups/${groupId}/chores/${choreId}`) }, async _updatePersonalChore(choreId: number, chore: ChoreUpdate): Promise { const response = await api.put(`/api/v1/chores/personal/${choreId}`, chore) return response.data }, async _deletePersonalChore(choreId: number): Promise { await api.delete(`/api/v1/chores/personal/${choreId}`) }, async getChoreHistory(choreId: number): Promise { const response = await apiClient.get(API_ENDPOINTS.CHORES.HISTORY(choreId)) return response.data }, }