import api from './client'

/**
 * UIWS 이식 모듈 API 클라이언트 (GUARDiA Mall).
 * 기존 Mall axios 인스턴스(client.ts: baseURL='' same-origin, JWT 인터셉터/토큰키 분리) 재사용.
 * Mall client 는 baseURL 가 비어 있으므로 UIWS 경로는 full path(/api/...)로 호출한다.
 * 응답 봉투: { success, message, data }. 호출부는 res.data.data 로 페이로드 접근.
 *
 * 인증: 관리자 영역(/admin)에서 호출되므로 client 인터셉터가 mall_admin_token 을 자동 첨부.
 */

// ── 2FA (운영 로그인 2차 검증)
export const verify2fa = (verifyToken: string, code: string) =>
  api.post('/api/mall/auth/verify', { verifyToken, code })

// ── 로그인 보조 3종 (UIWS auth 패턴 이식, 관리자/운영 계정 대상, 무인증 접근)
//    응답 봉투 { success, message, data } — 호출부는 res.data.data 로 페이로드 접근.
export const authSignup = (body: { username: string; password: string; displayName: string; email: string }) =>
  api.post('/api/mall/auth/signup', body)
export const authFindId = (body: { displayName: string; email: string }) =>
  api.post('/api/mall/auth/find-id', body)
export const authResetPassword = (body: { username: string; email: string }) =>
  api.post('/api/mall/auth/reset-password', body)

// ── 쪽지(message)
export const sendMessage = (body: object) => api.post('/api/messages', body)
export const listSent = (params: Record<string, unknown>) => api.get('/api/messages/sent', { params })
export const sentDetail = (id: number) => api.get(`/api/messages/sent/${id}`)
export const deleteSent = (ids: number[]) => api.delete('/api/messages/sent', { data: { ids } })
export const listReceived = (params: Record<string, unknown>) => api.get('/api/messages/received', { params })
export const receivedDetail = (id: number) => api.get(`/api/messages/received/${id}`)
export const deleteReceived = (ids: number[]) => api.delete('/api/messages/received', { data: { ids } })
export const unreadCount = () => api.get('/api/messages/unread-count')

// ── 일정(schedule)
export const scheduleCalendar = (params: Record<string, unknown>) => api.get('/api/schedules', { params })
export const scheduleAll = (params: Record<string, unknown>) => api.get('/api/schedules/all', { params })
export const scheduleSearch = (keyword: string) => api.get('/api/schedules/search', { params: { keyword } })
export const createSchedule = (body: object) => api.post('/api/schedules', body)
export const scheduleDetail = (id: number) => api.get(`/api/schedules/${id}`)
export const updateSchedule = (id: number, body: object) => api.put(`/api/schedules/${id}`, body)
export const deleteSchedule = (id: number) => api.delete(`/api/schedules/${id}`)

// ── 일지(diary)
export const diaryList = (params: Record<string, unknown>) => api.get('/api/diaries', { params })
export const createDiary = (body: object) => api.post('/api/diaries', body)
export const diaryDetail = (id: number) => api.get(`/api/diaries/${id}`)
export const updateDiary = (id: number, body: object) => api.put(`/api/diaries/${id}`, body)
export const deleteDiary = (id: number) => api.delete(`/api/diaries/${id}`)

// ── 첨부(attachment)
export const uploadAttachment = (refType: string, refId: number, file: File) => {
  const fd = new FormData()
  fd.append('refType', refType)
  fd.append('refId', String(refId))
  fd.append('file', file)
  return api.post('/api/attachments', fd, { headers: { 'Content-Type': 'multipart/form-data' } })
}
export const deleteAttachment = (id: number) => api.delete(`/api/attachments/${id}`)
export const attachmentDownloadUrl = (id: number) => `/api/attachments/${id}/download`

// ── 업무일지(worklog)
export const worklogList = (params: Record<string, unknown>) => api.get('/api/worklogs', { params })
export const worklogCalendar = (params: Record<string, unknown>) => api.get('/api/worklogs/calendar', { params })
export const worklogSearch = (params: Record<string, unknown>) => api.get('/api/worklogs/search', { params })
export const worklogProgress = (params: Record<string, unknown>) => api.get('/api/worklogs/dashboard/progress', { params })
export const createWorklog = (body: object) => api.post('/api/worklogs', body)
export const worklogDetail = (id: number) => api.get(`/api/worklogs/${id}`)
export const updateWorklog = (id: number, body: object) => api.put(`/api/worklogs/${id}`, body)
export const deleteWorklog = (id: number) => api.delete(`/api/worklogs/${id}`)
export const addWorklogComment = (id: number, cmtContent: string) =>
  api.post(`/api/worklogs/${id}/comments`, { cmtContent })
export const confirmWorklogComment = (cmtId: number) => api.post(`/api/worklogs/comments/${cmtId}/confirm`)

// ── 통계(stats)
export const personalWorkStats = (params: Record<string, unknown>) => api.get('/api/stats/personal-work', { params })
export const companyWorkStats = (params: Record<string, unknown>) => api.get('/api/stats/company-work', { params })
