import { IBox, UpdateBoxBodyType, IBoxDetail, BoxStatus } from '@/data/box/box.model';
import { CommonResponseType, PageAble } from '@/helpers/types';
import { request } from '@/services/request';
import axios from 'axios';
import { IRealBox, IRealBoxDetail, RealCreateBoxBodyType, UpdateRealBoxBodyType } from './real-box.model';

export const real_box_requests = {
    async getAll(params?: {
        page?: number;
        sort?: string;
        direction?: string;
        cargoId?: string;
        boxName?: string,
        partyId?: string | number;
        status?: BoxStatus;
    }) {
        return request.get<CommonResponseType<PageAble<IRealBox>>>('/boxes/list', { params });
    },
    async create({ ...body }: RealCreateBoxBodyType) {
        return request.post<CommonResponseType>('/boxes/create', body);
    },
    async update({ boxId, ...body }: UpdateRealBoxBodyType) {
        return request.put<CommonResponseType>('/boxes/change', body, {
            params: { boxId },
        });
    },
    async find(params: { boxId?: number | string }) {
        return request.get<any>(`/boxes/find/${params.boxId}`,);
    },
    async delete(params: { boxId: number | string }) {
        return request.delete<CommonResponseType>('/boxes/delete', { params });
    },
    async downloadExcel(params: { boxId: number | string }) {
        return request.get<Blob>(`/boxes/download/packets/${params.boxId}`, { responseType: "blob" });
    },
    async downloadQrCode(params: { boxId: number | string }) {
        return request.get(`/qr/${params.boxId}`, { responseType: 'blob' });
    },
    async translateWithGoogleApi(params: { text: string }): Promise<string> {
        const response = await axios.post('https://translation.googleapis.com/language/translate/v2', undefined, {
            params: {
                q: params.text,
                target: 'ru',
                source: 'zh',
                key: 'AIzaSyA5uAPZyjF_yo1hYOWWJ2uP7XgcmohZc8o',
            },
        });

        return response.data.data.translations?.[0]?.translatedText ?? '';
    },
    async translateWithMemoryApi(params: { text: string }): Promise<string> {
        const response = await axios.get<{
            responseData: {
                translatedText: string;
                match: number;
            };
        }>('https://api.mymemory.translated.net/get', {
            params: {
                q: params.text,
                langpair: 'zh|ru',
                // key: '7a4ac2e07cde1ff1e9de',
            },
        });

        return response.data.responseData.translatedText;
    },
};