const url = 'http://127.0.0.1:5180/api/';
async function ping(){
    const data = await fetch ('http://127.0.0.1:5180/api/ping');
    console.log(data);
}

async function setBaseDataHorseAPI(id, basedata) {
    try {
        console.log("Setting horse data");
        console.log("ID:", id);
        console.log("Basedata:", basedata);

        const apiUrl = url+`updateHorse/${id}/BasicData`;
        const response = await fetch(apiUrl, {
            method: 'POST',
            body: JSON.stringify(basedata),
            headers: {
                'Content-Type': 'application/json'
            }
        });
        console.log("API Response:", response);
    } catch (error) {
        console.error("Error setting horse data: " + error.message);
    }
}

async function getHorseLoadStateAPIAsync(id)
{
    let horseData;
    try {
    const apiUrl = url+`getHorse/${id}/LoadState`;
    await fetch(apiUrl)
        .then(response => response.json())
        .then(data => {
            horseData = data;
        });
    } catch (error) {
        console.log(error);
    }
    
    return horseData;
}

async function setHorsePedigreeAPIAsync(id, pedigreeData)
{
    const apiUrl = url+`updateHorse/${id}/Pedigree`;
    const response = await fetch(apiUrl, {
        method: 'POST',
        body: JSON.stringify({RelatedIds:pedigreeData}),
        headers: {
            'Content-Type': 'application/json'
        }
    });
    return response;
}

async function setHorseTrainingAPIAsync(id, training)
{
    const apiUrl = url+`updateHorse/${id}/Training`;
    const response = await fetch(apiUrl, {
        method: 'POST',
        body: JSON.stringify({ Training:training }),
        headers: {
            'Content-Type': 'application/json'
        }
    });
    return response;
}

async function setHorseGeneticsAPIAsync(id, genetics)
{
    const apiUrl = url+`updateHorse/${id}/Genetics`;
    console.log("Accessing API at:", apiUrl);
    const response = await fetch(apiUrl, {
        method: 'POST',
        body: JSON.stringify(genetics),
        headers: {
            'Content-Type': 'application/json'
        }
    });
    return response;
}

async function setHorseHealthAPIAsync(id, health)
{
    const apiUrl = url+`updateHorse/${id}/Health`;
    const response = await fetch(apiUrl, {
        method: 'POST',
        body: JSON.stringify({ Health: health }),
        headers: {
            'Content-Type': 'application/json'
        }
    });
    return response;
}

// Neue Funktion für Achievements
async function setHorseAchievementsAPIAsync(id, achievements)
{
    const apiUrl = url+`updateHorse/${id}/Achievements`;
    const response = await fetch(apiUrl, {
        method: 'POST',
        body: JSON.stringify(achievements),
        headers: {
            'Content-Type': 'application/json'
        }
    });
    return response;
}
