/* eslint-disable import/no-unresolved */
/* eslint-disable import/order */


import { sageService } from '@/service/sageService';
import { Client, ClientCreateResponse, ClientRequest, ClientUpdateRequest } from '@/types/clientType';
import { createAsyncThunk } from '@reduxjs/toolkit';


/**
 * get all clients
 */
export const getClients = createAsyncThunk(
  'client/getClients',
  async (): Promise<Client[]> => {
    try {
      return await sageService.getClients();
    } catch (err) {
      throw err;
    }
  }
);


/**
 * get client by id
 */
export const getClient = createAsyncThunk(
  'client/getClient',
  async (id: string): Promise<Client> => {
    try {
      return await sageService.getClient(id);
    } catch (err) {
      throw err;
    }
  }
);



/**
 * add client
 */
export const addClient = createAsyncThunk(
  'client/addClient',
  async (data: ClientRequest): Promise<ClientCreateResponse> => {
    try {
      return await sageService.addClient(data);
    } catch (err) {
      throw err;
    }
  }
);




/**
 * update client
 */
export const updateClient = createAsyncThunk(
  'client/updateClient',
  async (
    { numero, data }: { numero: string; data: ClientUpdateRequest }
  ): Promise<Client> => {
    try {
      return await sageService.updateClient(numero, data);
    } catch (err) {
      throw err;
    }
  }
);



/**
 * update client status
 */
export const updateStatus = createAsyncThunk(
  'client/updateStatus',
  async (
    { numero, est_actif }: { numero: string; est_actif: boolean }
  ): Promise<Client> => {
    try {
      return await sageService.updateStatusClient(numero, est_actif);
    } catch (err) {
      throw err;
    }
  }
);