import {
    ApiParameter,
    get,
    HasPagingParameters,
    post,
    ServiceResult,
} from "@insite/client-framework/Services/ApiService";
import { VmiCountCollectionModel, VmiCountModel } from "@insite/client-framework/Types/ApiModels";

const vmiBinCountsUrl = "/api/v1/vmiLocations/{vmiLocationId}/bins/{vmiBinId}/binCounts";
export interface GetVmiCountApiParameter extends ApiParameter {
    vmiLocationId?: string;
    vmiBinId?: string;
    expand?: "product"[];
}

export interface UpdateVmiCountApiParameter extends ApiParameter {
    vmiCount: VmiCountModel;
}

export interface AddVmiCountCollectionApiParameter extends ApiParameter {
    vmiLocationId: string;
    vmiCounts: VmiCountModel[];
}

export interface GetVmiCountsApiParameter extends ApiParameter, HasPagingParameters {
    vmiLocationId?: string;
    vmiBinId?: string;
    userName?: string;
    previousCountFromDate?: string;
    previousCountToDate?: string;
    getUniqueByUser?: boolean;
    sort?: string;
}

export interface AddVmiCountApiParameter extends ApiParameter {
    vmiLocationId?: string;
    vmiBinId?: string;
    binCount?: Omit<VmiCountModel, "id" | "createdOn" | "createdBy" | "uri" | "properties">;
}

export async function getVmiCounts(parameter: GetVmiCountsApiParameter) {
    if (!parameter.vmiLocationId) {
        throw Error("vmiLocationId is required");
    }

    if (!parameter.vmiBinId) {
        throw Error("vmiBinId is required");
    }

    const vmiLocationId = parameter.vmiLocationId;
    delete parameter.vmiLocationId;
    const vmiBinId = parameter.vmiBinId;
    delete parameter.vmiBinId;
    delete (parameter as any).onComplete;

    const vmiCounts = await get<VmiCountCollectionModel>(
        vmiBinCountsUrl.replace("{vmiLocationId}", vmiLocationId).replace("{vmiBinId}", vmiBinId),
        parameter,
    );

    return vmiCounts;
}

export async function createVmiCounts(
    parameter: AddVmiCountCollectionApiParameter,
): Promise<ServiceResult<VmiCountCollectionModel>> {
    try {
        const vmiLocationId = parameter.vmiLocationId;
        const vmiBinId = parameter.vmiCounts[0].vmiBinId;
        const vmiBinCollectionModel = await post<VmiCountModel[], VmiCountCollectionModel>(
            `${vmiBinCountsUrl.replace("{vmiLocationId}", vmiLocationId).replace("{vmiBinId}", vmiBinId)}/batch`,
            parameter.vmiCounts,
        );
        return {
            result: vmiBinCollectionModel,
            successful: true,
        };
    } catch (error) {
        if ("status" in error && error.status === 400 && error.errorJson && error.errorJson.message) {
            return {
                successful: false,
                errorMessage: error.errorJson.message,
            };
        }
        throw error;
    }
}

export async function postVmiCount(parameter: AddVmiCountApiParameter): Promise<ServiceResult<VmiCountModel>> {
    if (!parameter.vmiLocationId) {
        throw Error("vmiLocationId is required");
    }

    if (!parameter.vmiBinId) {
        throw Error("vmiBinId is required");
    }

    const vmiLocationId = parameter.vmiLocationId;
    const vmiBinId = parameter.vmiBinId;

    const vmiCountModel = await post<VmiCountModel>(
        vmiBinCountsUrl.replace("{vmiLocationId}", vmiLocationId).replace("{vmiBinId}", vmiBinId),
        parameter.binCount as VmiCountModel,
    );
    return {
        result: vmiCountModel,
        successful: true,
    };
}
