// api/queries.js

import AccountType, { CustomerType, SubmitLogin, User } from "../../components/Account";
import { SubmitOrder } from "../../components/Order";
import RatingSubmitType from "../../components/RatingSubmit";

export const fetchItemList = async () => {
    const response = await fetch('http://localhost:8085/article/all');
    if (!response.ok) {
        throw new Error('Fehler beim Laden der Items');
    }
    const data = await response.json();
    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(); 
    return data;
}

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

export const submitOrder = async (data: SubmitOrder) => {
    const response = await fetch('http://localhost:8085/order', {
        method: 'POST',
        headers: {
            'Content-Type': 'application/json',
        },
        body: JSON.stringify(data),
    });

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

    return await response.json();
}

export const submitAccount = async (data: AccountType) => {
    const response = await fetch('http://localhost:8085/account', {
        method: 'POST',
        headers: {
            'Content-Type': 'application/json',
        },
        body: JSON.stringify(data),
    });

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

    return await response.json();
}

export const submitCustomer = async (data: CustomerType) => {
    const response = await fetch('http://localhost:8085/customer', {
        method: 'POST',
        headers: {
            'Content-Type': 'application/json',
        },
        body: JSON.stringify(data),
    });

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

    return await response.json();
}

export const submitLogin = async (loginData: SubmitLogin) => {
    const response = await fetch("http://localhost:8085/session?email=" + loginData.email + "&password=" + loginData.password, {
        method: "POST",
        headers: {
            "Content-Type": "application/json",
        },
        body: JSON.stringify(loginData),
    });
    if (!response.ok) {
        throw new Error("Login failed");
    }
    return response.json();
};

export const fetchAccount = async (loginData: SubmitLogin) => {
    const response = await fetch("http://localhost:8085/account?email=" + loginData.email + "&password=" + loginData.password);
    if (!response.ok) {
        throw new Error("Login failed");
    }
    return response.json();
};

export const submitRegister = async (registerData: AccountType) => {
    const response = await fetch("http://localhost:8085/account", {
        method: "POST",
        headers: {
            "Content-Type": "application/json",
        },
        body: JSON.stringify(registerData),
    });
    if (!response.ok) {
        throw new Error("Login failed");
    }
    return response.json();
};

export const fetchCustomer = async (userId: number) => {
    const response = await fetch('http://localhost:8085/customer?id=' + userId);
    if (!response.ok) {
        throw new Error('Fehler beim Laden des Customers');
    }
    const data = await response.json();
    return data;
};

export const deleteAccount = async (userId: number) => {
    const response = await fetch('http://localhost:8085/account?id=' + userId, {
        method: 'DELETE',
    });
    if (!response.ok) {
        throw new Error('Fehler beim Löschen des Accounts');
    }
    return await response.json();
};

export const fetchOrders = async (customerId: number) => {
    const response = await fetch('http://localhost:8085/order/all?sutomerId=' + customerId);
    if (!response.ok) {
        throw new Error('Fehler beim Laden des Customers');
    }
    const data = await response.json();
    return data;
};

export const fetchAccounts = async (loginData: User) => {
    const response = await fetch("http://localhost:8085/account/all?email=" + loginData.email + "&session=" + loginData.session);
    if (!response.ok) {
        throw new Error("Login failed");
    }
    return response.json();
};