// api/queries.js

import RatingSubmitType from "../../components/RatingSubmit";

export const fetchItemList = async () => {
    const response = await fetch('http://localhost:8085/article/all');
    console.log("API Response:", response);
    if (!response.ok) {
        throw new Error('Fehler beim Laden der Items');
    }
    const data = await response.json();
    console.log("Fetched Items:", data);
    return data;
};

export const submitRating = async (ratingData: RatingSubmitType) => {
    const response = await fetch('http://localhost:8085/review?uuid=' + ratingData.articleId + '&rating=' + ratingData.rating * 2, {
        method: 'POST',
        headers: {
            'Content-Type': 'text/plain',
        },
        body: ratingData.content,
    });

    if (!response.ok) {
        throw new Error('Fehler beim Senden der Bewertung');
    }

    const data = await response.json();
    console.log("Rating Submitted:", data);
    return data;
}

export const fetchRatingList = async (itemId: string) => {
    const response = await fetch('http://localhost:8085/review/all?uuid=' + itemId);
    console.log("API Response:", response);
    if (!response.ok) {
        throw new Error('Fehler beim Laden der Items');
    }
    const data = await response.json();
    console.log("Fetched Items:", data);
    return data;
};