import request from '@/utils/request.js'

// ─── Stores ────────────────────────────────────────────────────────────────

export const storeApi = {
  getList: (params) => request.get('/system/stores', { params }),
  getDetail: (id) => request.get(`/system/stores/${id}`),
  create: (data) => request.post('/system/stores', data),
  update: (id, data) => request.put(`/system/stores/${id}`, data),
  delete: (id) => request.delete(`/system/stores/${id}`)
}

// ─── Departments ─────────────────────────────────────────────────────────────
// 后端 except(['show'])，无 getDetail/getTree 路由

export const departmentApi = {
  getList: (params) => request.get('/system/departments', { params }),
  create: (data) => request.post('/system/departments', data),
  update: (id, data) => request.put(`/system/departments/${id}`, data),
  delete: (id) => request.delete(`/system/departments/${id}`)
}

// ─── Positions ───────────────────────────────────────────────────────────────
// 后端 except(['show'])，无 getDetail 路由

export const positionApi = {
  getList: (params) => request.get('/system/positions', { params }),
  create: (data) => request.post('/system/positions', data),
  update: (id, data) => request.put(`/system/positions/${id}`, data),
  delete: (id) => request.delete(`/system/positions/${id}`)
}

// ─── Users ───────────────────────────────────────────────────────────────────

export const userApi = {
  getList: (params) => request.get('/system/users', { params }),
  getDetail: (id) => request.get(`/system/users/${id}`),
  create: (data) => request.post('/system/users', data),
  update: (id, data) => request.put(`/system/users/${id}`, data),
  delete: (id) => request.delete(`/system/users/${id}`),
  resetPassword: (id, data) => request.put(`/system/users/${id}/password`, data),
  updateStatus: (id, status) => request.put(`/system/users/${id}/status`, { status }),
  approve: (id) => request.put(`/system/users/${id}/approve`),
  reject: (id) => request.put(`/system/users/${id}/reject`)
}

// ─── Roles ───────────────────────────────────────────────────────────────────

export const roleApi = {
  getList: (params) => request.get('/system/roles', { params }),
  getDetail: (id) => request.get(`/system/roles/${id}`),
  create: (data) => request.post('/system/roles', data),
  update: (id, data) => request.put(`/system/roles/${id}`, data),
  delete: (id) => request.delete(`/system/roles/${id}`),
  assignPermissions: (id, permissionIds) =>
    request.put(`/system/roles/${id}/permissions`, { permission_ids: permissionIds })
  // getPermissions: 后端无此子路由，通过 getDetail 的 role.permissions 关联获取
}

// ─── Menus ───────────────────────────────────────────────────────────────────
// 后端 except(['show'])，无 getDetail/getTree 路由

export const menuApi = {
  getList: (params) => request.get('/system/menus', { params }),
  create: (data) => request.post('/system/menus', data),
  update: (id, data) => request.put(`/system/menus/${id}`, data),
  delete: (id) => request.delete(`/system/menus/${id}`)
}

// ─── Permissions ─────────────────────────────────────────────────────────────
// 后端 except(['show'])，无 getDetail/getTree 路由

export const permissionApi = {
  getList: (params) => request.get('/system/permissions', { params }),
  create: (data) => request.post('/system/permissions', data),
  update: (id, data) => request.put(`/system/permissions/${id}`, data),
  delete: (id) => request.delete(`/system/permissions/${id}`)
}

// ─── Dictionaries ─────────────────────────────────────────────────────────────
// 后端路由：/system/dictionaries (CRUD except show/update)
// 字典项：GET|POST /dictionaries/{id}/items
// 字典项更新/删除：PUT|DELETE /dictionary-items/{item}

export const dictApi = {
  // Dictionary types
  getTypeList: (params) => request.get('/system/dictionaries', { params }),
  createType: (data) => request.post('/system/dictionaries', data),
  deleteType: (id) => request.delete(`/system/dictionaries/${id}`),

  // Dictionary items（挂在 dictionary 下）
  getItemList: (dictId, params) =>
    request.get(`/system/dictionaries/${dictId}/items`, { params }),
  createItem: (dictId, data) =>
    request.post(`/system/dictionaries/${dictId}/items`, data),
  updateItem: (itemId, data) =>
    request.put(`/system/dictionary-items/${itemId}`, data),
  deleteItem: (itemId) =>
    request.delete(`/system/dictionary-items/${itemId}`)
}

// ─── Operation Logs ──────────────────────────────────────────────────────────
// 后端仅有 index 路由，无 show

export const logApi = {
  getList: (params) => request.get('/system/operation-logs', { params })
}

// ─── Registration Packages ────────────────────────────────────────────────────

export const registrationPackageApi = {
  getList: (params) => request.get('/system/registration-packages', { params }),
  getDetail: (id) => request.get(`/system/registration-packages/${id}`),
  create: (data) => request.post('/system/registration-packages', data),
  update: (id, data) => request.put(`/system/registration-packages/${id}`, data),
  delete: (id) => request.delete(`/system/registration-packages/${id}`)
}
