import { makeRequest } from "@frontend/kitui";
import { defaultQuizConfig } from "@model/quizSettings";
import { CopyQuizRequest, CopyQuizResponse } from "model/quiz/copy";
import { CreateQuizRequest } from "model/quiz/create";
import { DeleteQuizRequest, DeleteQuizResponse } from "model/quiz/delete";
import { EditQuizRequest, EditQuizResponse } from "model/quiz/edit";
import { GetQuizRequest, GetQuizResponse } from "model/quiz/get";
import { GetQuizListRequest, GetQuizListResponse } from "model/quiz/getList";
import { RawQuiz } from "model/quiz/quiz";


const baseUrl = process.env.NODE_ENV === "production" ? "/squiz" : "https://squiz.pena.digital/squiz";
const imagesUrl = process.env.NODE_ENV === "production" ? "/squizstorer" : "https://squiz.pena.digital/squizstorer";

function createQuiz(body?: Partial<CreateQuizRequest>) {
    return makeRequest<CreateQuizRequest, RawQuiz>({
        url: `${baseUrl}/quiz/create`,
        body: { ...defaultCreateQuizBody, ...body },
        method: "POST",
    });
}

async function getQuizList(body?: Partial<GetQuizListRequest>) {
    const response = await makeRequest<GetQuizListRequest, GetQuizListResponse>({
        url: `${baseUrl}/quiz/getList`,
        body: { ...defaultGetQuizListBody, ...body },
        method: "POST",
    });

    return response.items;
}

function getQuiz(body?: Partial<GetQuizRequest>) {
    return makeRequest<GetQuizRequest, GetQuizResponse>({
        url: `${baseUrl}/quiz/get`,
        body: { ...defaultGetQuizBody, ...body },
        method: "GET",
    });
}

async function editQuiz(body: EditQuizRequest, signal?: AbortSignal) {
    return makeRequest<EditQuizRequest, EditQuizResponse>({
        url: `${baseUrl}/quiz/edit`,
        body,
        method: "PATCH",
        signal,
    });
}

function copyQuiz(id: number) {
    return makeRequest<CopyQuizRequest, CopyQuizResponse>({
        url: `${baseUrl}/quiz/copy`,
        body: { id },
        method: "POST",
    });
}

function deleteQuiz(id: number) {
    return makeRequest<DeleteQuizRequest, DeleteQuizResponse>({
        url: `${baseUrl}/quiz/delete`,
        body: { id },
        method: "DELETE",
    });
}

function addQuizImages(quizId: number, image: Blob) {
    const formData = new FormData();

    formData.append("quiz", quizId.toString());
    formData.append("image", image);

    return makeRequest<FormData, { [key: string]: string; }>({
        url: `${imagesUrl}/quiz/putImages`,
        body: formData,
        method: "PUT",
    });
}

export const quizApi = {
    create: createQuiz,
    getList: getQuizList,
    get: getQuiz,
    edit: editQuiz,
    copy: copyQuiz,
    delete: deleteQuiz,
    addImages: addQuizImages,
};


const defaultCreateQuizBody: CreateQuizRequest = {
    "fingerprinting": true,
    "repeatable": true,
    "note_prevented": true,
    "mail_notifications": false,
    "unique_answers": true,
    "name": "",
    "description": "",
    "config": JSON.stringify(defaultQuizConfig),
    "status": "stop",
    "limit": 0,
    "due_to": 0,
    "time_of_passing": 0,
    "pausable": false,
    "super": false,
    "group_id": 0,
};

const defaultGetQuizBody: GetQuizRequest = {
    "quiz_id": "string",
    "limit": 0,
    "page": 0,
    "need_config": true,
};

const defaultGetQuizListBody: GetQuizListRequest = {
    "limit": 100,
    "offset": 0,
};
