import api from './client'

/**
 * UIWS system(권한관리) 모듈 API 클라이언트 — guardia-esn 백엔드(com.zioinfo.esn.uiws.system) 실측 계약.
 * 기존 ESN axios 인스턴스(client.ts: baseURL='', esn_token JWT 인터셉터) 재사용.
 *
 * 응답 봉투: { success, message, data }. 호출부는 res.data.data 로 페이로드 접근.
 * 목록 엔드포인트 data 페이로드는 PageResponse = { content, total, page, size } (flat array 아님).
 */

export interface PageResponse<T> {
  content: T[]
  total: number
  page: number
  size: number
}

// 페이지네이션 헬퍼 — PageResponse 의 total/size 로 totalPages 계산(백엔드는 totalPages 미제공).
export const totalPages = (p: { total: number; size: number } | undefined | null): number =>
  p && p.size > 0 ? Math.ceil(p.total / p.size) : 0

// ── 2.1 권한(roles) — base /api/system/roles ────────────────────────────────
export const roleList = (keyword?: string, page = 0, size = 20) =>
  api.get('/api/system/roles', { params: { keyword: keyword || undefined, page, size } })
export const roleGet = (id: string) => api.get(`/api/system/roles/${encodeURIComponent(id)}`)
export const createRole = (body: object) => api.post('/api/system/roles', body)
export const updateRole = (id: string, body: object) => api.put(`/api/system/roles/${encodeURIComponent(id)}`, body)
export const deleteRoles = (ids: string[]) => api.delete('/api/system/roles', { data: { ids } })

// ── 2.5 메뉴생성(role-menus) — 권한별 메뉴매핑 ─────────────────────────────────
export const roleMenuRoles = (page = 0, size = 100) =>
  api.get('/api/system/role-menus', { params: { page, size } })
export const roleMenuMapping = (roleId: string) =>
  api.get(`/api/system/roles/${encodeURIComponent(roleId)}/menus`)
export const saveRoleMenuMapping = (roleId: string, menus: object[]) =>
  api.put(`/api/system/roles/${encodeURIComponent(roleId)}/menus`, { menus })

// ── 2.7 코드(codes) — 그룹+값 복합. 값은 그룹 단위로 전체 치환(per-code 엔드포인트 없음) ──
export const codeGroupList = (keyword?: string, page = 0, size = 20) =>
  api.get('/api/system/codes', { params: { keyword: keyword || undefined, page, size } })
export const codeGroupGet = (grpCd: string) =>
  api.get(`/api/system/codes/${encodeURIComponent(grpCd)}`)
export const codeGroupValues = (grpCd: string) =>
  api.get(`/api/system/codes/group/${encodeURIComponent(grpCd)}/values`)
export const createCodeGroup = (body: object) => api.post('/api/system/codes', body)
export const updateCodeGroup = (grpCd: string, body: object) =>
  api.put(`/api/system/codes/${encodeURIComponent(grpCd)}`, body)
export const deleteCodeGroup = (grpCd: string) =>
  api.delete(`/api/system/codes/${encodeURIComponent(grpCd)}`)

// ── 2.9 메뉴(menus) — base /api/system/menus ────────────────────────────────
export const menuList = (page = 0, size = 100) =>
  api.get('/api/system/menus', { params: { page, size } })
export const menuSearch = (keyword?: string) =>
  api.get('/api/system/menus/search', { params: { keyword: keyword || undefined } })
export const createMenu = (body: object) => api.post('/api/system/menus', body)
export const updateMenu = (id: string, body: object) =>
  api.put(`/api/system/menus/${encodeURIComponent(id)}`, body)
export const deleteMenus = (ids: string[]) => api.delete('/api/system/menus', { data: { ids } })

// ── 2.3 부서(depts) — base /api/system/depts ────────────────────────────────
export const deptList = (keyword?: string, page = 0, size = 100) =>
  api.get('/api/system/depts', { params: { keyword: keyword || undefined, page, size } })
export const deptTree = () => api.get('/api/system/depts/tree')
export const createDept = (body: object) => api.post('/api/system/depts', body)
export const updateDept = (id: string, body: object) =>
  api.put(`/api/system/depts/${encodeURIComponent(id)}`, body)
export const deleteDept = (id: string) =>
  api.delete(`/api/system/depts/${encodeURIComponent(id)}`)

// ── 2.4 거래처(companies) — base /api/system/companies ───────────────────────
export const companyList = (keyword?: string, page = 0, size = 20) =>
  api.get('/api/system/companies', { params: { keyword: keyword || undefined, page, size } })
export const createCompany = (body: object) => api.post('/api/system/companies', body)
export const updateCompany = (id: string, body: object) =>
  api.put(`/api/system/companies/${encodeURIComponent(id)}`, body)
export const deleteCompanies = (ids: string[]) => api.delete('/api/system/companies', { data: { ids } })
