import {client, processResponse} from '../client';
import {apiUrls} from "app/src/api/api-urls";

const studentManager = {
  list, add, getInfo, update, deleteStudent, addWithAutoMajor
}

export default studentManager

async function list(page, pageSize) {
  const response = await client.get(apiUrls.student.list, {
    params: {
      page: page,
      pageSize: pageSize
    }
  })

  return processResponse(response)
}

async function getInfo(id) {
  const response = await client.get(apiUrls.student.info, {
    params: {id: id}
  })

  return processResponse(response)
}

async function add(data) {
  const postData = {
    "studentNumber": data.studentNumber,
    "studentName": data.name,
    "majorId": data.major
  }

  const response = await client.post(apiUrls.admin.student.add, postData)

  return processResponse(response)
}

async function addWithAutoMajor(data) {
  const postData = {
    "studentNumber": data.studentNumber,
    "studentName": data.studentName,
    "majorName": data.major
  }

  const response = await client.post(apiUrls.admin.student.addWithAutoMajor, postData)

  return processResponse(response)
}

async function update(id, studentName, username, initialPassword) {
  const request = {
    studentName: studentName === '' ? undefined : studentName,
    username: username === '' ? undefined : username,
    initialPassword: initialPassword === '' ? undefined : initialPassword
  }

  const response = await client.post(
    apiUrls.admin.student.update,
    request, {
      params: {id: id}
    })

  return processResponse(response)
}


async function deleteStudent(id) {
  const response = await client.post(apiUrls.admin.student.delete, undefined, {
    params: {id: id}
  })

  return processResponse(response)
}
