import axios from 'axios';
import { UserResponse } from '../response/UserResponse';

// import { cookies } from 'next/headers'

export const GetCurrentUser = async (): Promise<UserResponse | undefined> => {
	console.log(process.env.NEXT_PUBLIC_API_URL + "/user/current")
	const res = await axios.get(process.env.NEXT_PUBLIC_API_URL + "/user/current", {
		maxRedirects: 0,
		withCredentials: true,
		validateStatus: (status) => {
			return status >= 200 && status < 400
		}});
	return res.data;
};

export const GetUserTable = async (page: number): Promise<UserResponse[]> => {
	const res = await axios.put(process.env.NEXT_PUBLIC_API_URL + `/users?page=${page.toString()}`, {
		name: "",
		jobPosition: ""
	}, {
		withCredentials: true
	});
	if (res.data.Location)
		window.location.href = res.data.Location
	return res.data || []
}

export const SetUserName = async (userName: string) => {
	const queryParams = new URLSearchParams({
		user_name: userName
	});
	await axios.put(process.env.NEXT_PUBLIC_API_URL + `/user/name?${queryParams.toString()}`)
}

export const PromoteUser = async (userId: number) => {
	const queryParams = new URLSearchParams({
		user_id: userId.toString()
	});
	await axios.put(process.env.NEXT_PUBLIC_API_URL + `/user/promote?${queryParams.toString()}`, {}, {withCredentials: true});
}

export const DemoteUser = async (userId: number) => {
	const queryParams = new URLSearchParams({
		user_id: userId.toString()
	})
	await axios.put(process.env.NEXT_PUBLIC_API_URL + `/user/demote?${queryParams.toString()}`, {}, {withCredentials: true})
}

export const DeactivateUser = async (userId: number) => {
	const queryParams = new URLSearchParams({
		user_id: userId.toString()
	})
	await axios.delete(process.env.NEXT_PUBLIC_API_URL + `/user/deactivate?${queryParams.toString()}`, {withCredentials: true})
}

export const ActivateUser = async (userId: number) => {
	const queryParams = new URLSearchParams({
		user_id: userId.toString()
	})
	await axios.put(process.env.NEXT_PUBLIC_API_URL + `/user/activate?${queryParams.toString()}`, {}, {withCredentials: true})
}

export const CreatePosition = async (positionName: string) => {
	const queryParams = new URLSearchParams({
		position_name: positionName
	})
	await axios.post(process.env.NEXT_PUBLIC_API_URL + `/position/create?` + queryParams.toString())
}

export const SetUserPosition = async (userId: number, positionName: string) => {
	const queryParams = new URLSearchParams({
		user_id: userId.toString(),
		position: positionName
	})
	await axios.put(process.env.NEXT_PUBLIC_API_URL + `/user/position?${queryParams.toString}`)
}
