import { configFor, callApi } from "./apiClientHelpers";
import { AssignmentsApi } from "omsorgcore-client-ts";

const API_BASE_URL = configFor(null).basePath;

export function listAssignments(accessToken, { orderId, employeeId, fromDate, toDate, page = 1, pageSize = 20 } = {}) {
  const api = new AssignmentsApi(configFor(accessToken));
  return callApi(api.apiAssignmentsGetRaw({ orderId, employeeId, fromDate, toDate, page, pageSize }));
}

export function getAssignment(accessToken, id) {
  const api = new AssignmentsApi(configFor(accessToken));
  return callApi(api.apiAssignmentsIdGetRaw({ id }));
}

export function createAssignment(accessToken, payload) {
  const api = new AssignmentsApi(configFor(accessToken));
  return callApi(api.apiAssignmentsPostRaw({
    createAssignmentRequest: {
      ...payload,
      startDate: payload.startDate ? new Date(payload.startDate) : payload.startDate,
      endDate: payload.endDate ? new Date(payload.endDate) : payload.endDate
    }
  }));
}

export function updateAssignment(accessToken, id, payload) {
  const api = new AssignmentsApi(configFor(accessToken));
  return callApi(api.apiAssignmentsIdPutRaw({ id, updateAssignmentRequest: payload }));
}

export function deleteAssignment(accessToken, id) {
  const api = new AssignmentsApi(configFor(accessToken));
  return callApi(api.apiAssignmentsIdDeleteRaw({ id }));
}

// Live-Vorschau der FR-EM-3-Konfliktprüfungen (Qualifikation/Abwesenheit/Arbeitszeit/Überschneidung/
// Vertrag), ohne etwas anzulegen - für den Anlegen-Dialog, sobald Mitarbeiter + Zeitraum feststehen.
export function checkAssignmentConflicts(accessToken, { orderId, employeeId, startDate, endDate, excludeAssignmentId }) {
  const api = new AssignmentsApi(configFor(accessToken));
  return callApi(api.apiAssignmentsCheckGetRaw({
    orderId,
    employeeId,
    startDate: startDate ? new Date(startDate) : startDate,
    endDate: endDate ? new Date(endDate) : endDate,
    excludeAssignmentId
  }));
}
