// api/queries.js

import AccountType, { AdminAccountOperation, CustomerType, SubmitLogin, User } from "../../components/Account";
import OrderType, { OrderPatch } from "../../components/Order";
import RatingSubmitType from "../../components/RatingSubmit";
import { ItemWithFSImage } from "../../components/Item";

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 fetchItemListWithImage = async () => {
    const response = await fetch('http://localhost:8085/article/all/image');
    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: OrderType) => {
    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 (user: User) => {
    const response = await fetch('http://localhost:8085/account?email=' + user.email + '&password=' + user.password, {
        method: 'DELETE',
    });
    if (!response.ok) {
        throw new Error('Fehler beim Löschen des Accounts');
    }
    return await response.json();
};

export const deleteAccountAdmin = async (user: AdminAccountOperation) => {
    const response = await fetch('http://localhost:8085/account/admin?email=' + user.email + '&session=' + user.session + '&accountId=' + user.accountId, {
        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?customerId=' + 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();
};

export const fetchItems = async () => { //TODO: remove and use above
    const response = await fetch("http://localhost:8085/article/all");
    if (!response.ok) {
        throw new Error("fetching items failed");
    }
    return response.json();
};

export const fetchStatisticsVolume = async (loginData: User) => {
    const response = await fetch("http://localhost:8085/statistics/volume?email=" + loginData.email + "&session=" + loginData.session);
    if (!response.ok) {
        throw new Error("fetching satistics Volume failed");
    }
    return response.json();
};

export const fetchStatisticsRevenue = async (loginData: User) => {
    const response = await fetch("http://localhost:8085/statistics/revenue?email=" + loginData.email + "&session=" + loginData.session);
    if (!response.ok) {
        throw new Error("fetching satistics Revenue failed");
    }
    return response.json();
};

export const editAccount = async (customer: CustomerType) => {
    const response = await fetch('http://localhost:8085/customer?id=' + customer.id, {
        method: 'PUT',
        headers: {
            "Content-Type": "application/json",
        },
        body: JSON.stringify(customer),
    });
    if (!response.ok) {
        throw new Error('Fehler beim Löschen des Accounts');
    }
    return await response.json();
};

export const orderPatch = async (order: OrderPatch) => {
    const response = await fetch("http://localhost:8085/order?id=" + order.id + "&status=" + order.status, {
        method: "PATCH",
    }
    );
    if (!response.ok) {
        throw new Error("Order patch failed");
    }
    return response.json();
};

export const fetchOrderStatus = async (loginData: User) => {
    const response = await fetch("http://localhost:8085/statistics/orderstatus?email=" + loginData.email + "&session=" + loginData.session);
    if (!response.ok) {
        throw new Error("fetching order status failed");
    }
    return response.json();
};

export const fetchStockPercent = async (loginData: User) => {
    const response = await fetch("http://localhost:8085/statistics/stockpercent?email=" + loginData.email + "&session=" + loginData.session);
    if (!response.ok) {
        throw new Error("fetching stock% failed");
    }
    return response.json();
};


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

export const updateCustomer = async (customer: CustomerType) => {
    const response = await fetch('http://localhost:8085/customer?id=' + customer.id, {
        method: 'PUT',
        headers: {
            "Content-Type": "application/json",
        },
        body: JSON.stringify(customer),
    });
    if (!response.ok) {
        throw new Error('Fehler beim Ändern des Customers');
    }
    return await response.json();
}

export const fetchFarmingStationItemList = async (): Promise<ItemWithFSImage[]> => {
    const response = await fetch('http://localhost:8085/farm/articles');
    if (!response.ok)
        throw new Error('Failed to fetch items');
    return response.json();
}
