import { api } from '@/core/services/api'
import type { EmploymentApiResponse, EmploymentPayload } from '@/modules/profile/types/employment.types'

export async function createUserEmployment(
  userId: string,
  payload: EmploymentPayload,
): Promise<EmploymentApiResponse> {
  const { data } = await api.post<EmploymentApiResponse>(`/v1/users/${userId}/employments`, payload)

  if (!data.success) {
    throw new Error(data.message ?? 'Gagal menambah pekerjaan.')
  }

  return data
}

export async function updateUserEmployment(
  userId: string,
  employmentId: string,
  payload: EmploymentPayload,
): Promise<EmploymentApiResponse> {
  const { data } = await api.put<EmploymentApiResponse>(
    `/v1/users/${userId}/employments/${employmentId}`,
    payload,
  )

  if (!data.success) {
    throw new Error(data.message ?? 'Gagal mengemas kini pekerjaan.')
  }

  return data
}

export async function deleteUserEmployment(
  userId: string,
  employmentId: string,
): Promise<EmploymentApiResponse> {
  const { data } = await api.delete<EmploymentApiResponse>(
    `/v1/users/${userId}/employments/${employmentId}`,
  )

  if (!data.success) {
    throw new Error(data.message ?? 'Gagal memadam pekerjaan.')
  }

  return data
}
