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

import { sageService } from '@/service/sageService';
import { DevisListItem, DevisRequest, DevisResponse, DevisStatusRequest, DevisStatusResponse, DevisUpdateRequest } from '@/types/devisType';
import type { StatusRequest, StatusResponse, TransformationResponse } from '@/types/sageTypes';
import { createAsyncThunk } from '@reduxjs/toolkit';


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


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


/**
 * verified status devis
 */
export const selectDevisAsync = createAsyncThunk<
  DevisListItem, 
  DevisListItem, 
  { state: any } 
>(
  "devis/selectDevisAsync",
  async (devis, { dispatch }) => {
    if (!devis.isVerified && !devis.a_deja_ete_transforme) {
      const updatedDevis = await dispatch(getDevisById(devis.numero)).unwrap() as any
      return updatedDevis.data;
    }
    return devis;
  }
);



/**
 * create devis
 */
export const createDevis = createAsyncThunk(
  'devis/createDevis',
  async (data: DevisRequest): Promise<DevisResponse> => {
    try {
      return await sageService.createDevis(data);
    } catch (err) {
      throw err;
    }
  }
);



/**
 * update devis
 */
export const updateDevis = createAsyncThunk(
  'devis/updateDevis',
  async (
    { numero, data }: { numero: string; data: DevisUpdateRequest }
  ) => {
    try {
      return await sageService.updateDevis(numero, data);
    } catch (err) {
      throw err;
    }
  }
);



/**
 * changed statut devis
 */
export const changerStatutDevis = createAsyncThunk(
  'devis/changerStatutDevis',
  async (data: StatusRequest):  Promise<StatusResponse> => {
    try {
      return await sageService.changerStatut(0, data);
    } catch (err) {
      throw err;
    }
  }
);





/**
 * change devis to commande
 */
export const devisToCommande = createAsyncThunk(
  'devis/devisToCommande',
  async (numero: string):  Promise<TransformationResponse> => {
    try {
      return await sageService.devisToCommande(numero);
    } catch (err) {
      throw err;
    }
  }
);




/**
 * change devis to facture
 */
export const devisToFacture = createAsyncThunk(
  'devis/devisToFacture',
  async (numero: string):  Promise<TransformationResponse> => {
    try {
      return await sageService.devisToFacture(numero);
    } catch (err) {
      throw err;
    }
  }
);



/**
 * devis to pdf
 */
export const devisToPDF = createAsyncThunk(
  'devis/devisToPDF',
  async (numero: string) => {
    try {
     return await sageService.devisToPDF(numero);
    } catch (err) {
      throw err;
    }
  }
);

