import axios from 'axios'

import { AddItemToOrderQuery } from '../queries/AddItemToOrderQuery'
import { CreateItemQuery } from '../queries/CreateItemQuery'
import { GetCurrentPriceQuery } from '../queries/GetCurrentPriceQuery'
import { GetOrderItemsQuery } from '../queries/GetOrderItemsQuery'
import { SetItemPriceQuery } from '../queries/SetItemPriceQuery'
import { DeleteOrderItemRequest } from '../request/DeleteOrderItemRequest'
import { SetItemMadeRequest } from '../request/SetItemMadeRequest'
import { SetItemQuantityRequest } from '../request/SetItemQuantityRequest'
import { ItemHistoryResponse } from '../response/ItemHistoryResponse'
import { ItemPriceResponse } from '../response/ItemPriceResponse'
import { OrderFilledResponse } from '../response/OrderFilledResponse'
import { OrderItemPriceResponse } from '../response/OrderItemPriceResponse'

export const CreateItem = async (query: CreateItemQuery): Promise<ItemPriceResponse> => {
	const queryParams = new URLSearchParams({
		item_name: query.item_name,
		in_season: query.in_season,
		item_price: query.item_price
	})

	const res = await axios.post(process.env.NEXT_PUBLIC_API_URL + `/item/create?${queryParams.toString()}`, {}, {withCredentials: true})

	return res.data
}

export const SetItemPrice = async (query: SetItemPriceQuery): Promise<ItemPriceResponse> => {
	const queryParams = new URLSearchParams({
		item_id: query.item_id,
		item_price: query.item_price
	})

	const res = await axios.put(process.env.NEXT_PUBLIC_API_URL + `/item/price?${queryParams.toString()}`, {}, {withCredentials: true})
	
	return res.data as ItemPriceResponse

}

export const GetCurrentPrice = async (query: GetCurrentPriceQuery): Promise<ItemPriceResponse> => {
	const queryParams = new URLSearchParams({
		item_id: query.item_id
	})

	const res = await axios.get(process.env.API_URL + `/item/price?${queryParams.toString()}`)
		if (res.data.Location) {
		window.location.href = res.data.Location
	}
	return res.data
}

export const AddItemToOrder = async (query: AddItemToOrderQuery): Promise<OrderItemPriceResponse> => {
	const queryParams = new URLSearchParams({
		item_id: query.item_id,
		order_id: query.order_id,
		quantity: query.quantity
	})
	
	console.log(process.env.NEXT_PUBLIC_API_URL + `/order/item?${queryParams.toString()}`)
	const res = await axios.put(process.env.NEXT_PUBLIC_API_URL + `/order/item?${queryParams.toString()}`, {}, {withCredentials: true})
	if (res.data?.Location) {
		window.location.href = res.data.Location
	}

	return res.data
}

export const GetOrderItems = async (query: GetOrderItemsQuery): Promise<OrderItemPriceResponse[]> => {
	const queryParams = new URLSearchParams({
		order_id: query.order_id
	})

 	const res = await axios.get(process.env.NEXT_PUBLIC_API_URL + `/order/items?${queryParams.toString()}`, {withCredentials: true})
	if (res.data?.Location) {
		window.location.href = res.data.Location
	}

	return res.data || []
}

export const SetItemMade = async (request: SetItemMadeRequest): Promise<OrderFilledResponse> => {
	const res = await axios.put(process.env.NEXT_PUBLIC_API_URL + '/item/made', request, {withCredentials: true})
	if (res.data?.Location) {
		window.location.href = res.data.Location
	}

	return res.data
}

export const SetItemQuantity = async (request: SetItemQuantityRequest): Promise<OrderFilledResponse> => {
	const res = await axios.put(process.env.NEXT_PUBLIC_API_URL + `/item/quantity`, request, {withCredentials: true})
	if (res.data.Location) {
		window.location.href = res.data.Location
	}

	return res.data
}

export const DeleteOrderItem = async (request: DeleteOrderItemRequest) => {
	const res = await axios.delete(process.env.API_URL + `/order/item?order_id=${request.order_id}&item_id=${request.item_id}`)
	if (res.data.Location) {
		window.location.href = res.data.Location
	}
	
}

export const DeleteItem = async (itemId: number) => {
	const queryParams = new URLSearchParams({
		item_id: itemId.toString()
	})

	const res = await axios.delete(process.env.API_URL + `/item?${queryParams.toString()}`)
	if (res.data.Location) {
		window.location.href = res.data.Location
	}

}

export const GetItems = async (): Promise<ItemPriceResponse[]> => {
	const res = await axios.get(process.env.NEXT_PUBLIC_API_URL + `/items`, {withCredentials: true})

	if (res.data.Location) {
		window.location.href = res.data.Location
	}
	return res.data
}

export const GetItemHistory = async (itemId: number): Promise<ItemHistoryResponse[]> => {
	const res = await axios.get(process.env.NEXT_PUBLIC_API_URL + `/item/history?item_id=${itemId.toString()}`, {withCredentials: true})

	return res.data
}