import apiClient from "./apiClient";
import {Assembly} from "../types/assembly";
import {closeCancelAssemblyModal} from "../features/cancelAssemblyModal/cancelAssemblyModalSlice";

const router = '/assembly';
export type CreateAssemblyResponse = {
    ok: boolean,
    message: string,
    assemblyId: number,
    statusCode: AssemblyCreationStatusCode,
    userName?: string
}
const assemblyApi = {
    create: async (orderId: number): Promise<CreateAssemblyResponse> => {
        let response = await apiClient.post(`${router}/create`, {orderId});
        return response.data;
    },
    close: async (assemblyId: number): Promise<{ ok: boolean, message: string, reward: number }> => {
        let response = await apiClient.post(`${router}/close`, {assemblyId});
        return response.data;
    },
    cancel: async (): Promise<{ ok: boolean, message: string }> => {
        let response = await apiClient.post(`${router}/cancel`);
        return response.data;
    },
    getActive: async (): Promise<Assembly> => {
        let response = await apiClient.get(`${router}/getActive`);
        return response.data;
    },
    hasActive: async (): Promise<{ has: boolean }> => {
        let response = await apiClient.get(`${router}/hasActive`);
        return response.data;
    },
    updateState: async (assemblyId: number, state: number): Promise<{ ok: boolean }> => {
        let response = await apiClient.post(`${router}/updateState`, {assemblyId, state});
        return response.data;
    },
    confirm: async (assemblyId: number): Promise<{ ok: boolean, message: string }> => {
        let response = await apiClient.post(`${router}/confirm`, {assemblyId});
        return response.data;
    },
    cancelById: async (assemblyId: number): Promise<{ ok: boolean, message: string }> => {
        let response = await apiClient.post(`${router}/cancelById`, {assemblyId});
        return response.data;
    },
    attachCrpt: async (orderProductId: number, crpt: string): Promise<{ ok: boolean, message: string }> => {
        let response = await apiClient.post(`${router}/attachCrpt`, {orderProductId, crpt});
        return response.data;
    },
    needCrpt: async (orderProductId: number): Promise<{ needCrpt: boolean }> => {
        let response = await apiClient.get(`${router}/needCrpt`, {params: {orderProductId}});
        return response.data;
    }
}


export enum AssemblyCreationStatusCode {
    ASSEMBLY_ALREADY_EXISTS = "ASSEMBLY_ALREADY_EXISTS",
    USER_ALREADY_HAS_ACTIVE_ASSEMBLY = "USER_ALREADY_HAS_ACTIVE_ASSEMBLY",
    CREATED = "CREATED",
    INVALID_EXCEPTION = "INVALID_EXCEPTION"
}

export default assemblyApi;