import axios from "axios";
import { Genre } from "./models/Types";
import { CatchError } from "./common";

// const controllerUrl = 'https://localhost:7057/Dictionaries/Genre/';
const controllerUrl = 'http://localhost:5198/Dictionaries/Genre/';

export async function GetGenreList() {
    try {
        // 👇️ const data: GetUsersResponse
        // const { data, status } = await axios.get<Array<Genre>>(`${controllerUrl}GenreList`,
        const { data, } = await axios.get<Array<Genre>>(`${controllerUrl}GenreList`,
        {
            headers: {
            Accept: 'text/plain',
            },
        },
        );

        // console.log(JSON.stringify(data, null, 4));

        // 👇️ "response status is: 200"
        // console.log('response status is: ', status);

        return data;
    }
    catch (error)
    {
        CatchError(error);
    }
}

export async function CreateGenre(value: string) {
    try {
        // 👇️ const data: GetUsersResponse
        // const { data, status } = await axios.post<Array<Genre>>(`${controllerUrl}CreateGenre?genreName=${value}`,
        await axios.post(`${controllerUrl}CreateGenre?genreName=${value}`,
            {
                headers: {
                // Accept: 'text/plain',
                },
                // body: {}
            },
        );

        // console.log(JSON.stringify(data, null, 4));

        // 👇️ "response status is: 200"
        // console.log('response status is: ', status);

        // return data;
    } 
    catch (error)
    {
        CatchError(error);
    }
}

export async function EditGenre(id : string, value: string) {
    try {
        // 👇️ const data: GetUsersResponse
        const { status } = await axios.post(`${controllerUrl}EditGenre?id=${id}&genreName=${value}`,
        {
            headers: {
            // Accept: 'text/plain',
            },
            // body: {}
        },
        );

        //console.log(JSON.stringify(data, null, 4));

        // 👇️ "response status is: 200"
        console.log('response status is: ', status);

        // return Promise.resolve();
        // Promise.resolve();
    }
    catch (error)
    {
        CatchError(error);
    }
}

export async function DeleteGenre(id : string) {
    try {
        // 👇️ const data: GetUsersResponse
        const { status } = await axios.post(`${controllerUrl}DeleteGenre?id=${id}`,
        {
            headers: {
            // Accept: 'text/plain',
            },
            // body: {}
        },
        );

        //console.log(JSON.stringify(data, null, 4));

        // 👇️ "response status is: 200"
        console.log('response status is: ', status);

        // return Promise.resolve();
        // Promise.resolve();
    }
    catch (error)
    {
        CatchError(error);
    }
}