//운영 정보 관리 - 로그 api 연결

import { Axios, responseFileDownload } from '../utils';

// 비즈니스 로그 조회
export const BusinessLogList = async (token, params) => {
	try {
		const res = await Axios.post(`/api/v1/log/generic/list`, params, {
			headers: { Authorization: `Bearer ${token}` },
			timeout: 600000
		});

		return res.data;
	} catch (e) {
		if (e instanceof Error) {
			throw new Error('BusinessLogList Error', e);
		}
	}
};

export const BusinessLogExport = async (token, params) => {
	try {
		await Axios.post(`/api/v1/log/generic/excel-export`, params, {
			headers: { Authorization: `Bearer ${token}` },
			responseType: 'blob'
		}).then(response => {
			responseFileDownload(response, {
				defaultFileName: 'businessLog'
			});
		});
	} catch (e) {
		if (e instanceof Error) {
			throw new Error('BusinessLogExport Error', e);
		}
	}
};

export const getExcelProgress = async (token, taskId) => {
	try {
		const response = await Axios.get(`/api/v1/log/progress/${taskId}`, {
			headers: {
				Authorization: `Bearer ${token}`,
				'Content-Type': 'application/json',
			},
		});

		if (!response.data) {
			throw new Error(`HTTP error! status: ${response.status}`);
		}

		return response;
	} catch (error) {
		console.error('Progress API error:', error);
		throw error;
	}
};

export const getCurrencyList = async (token, startDate, endDate, order, size, currentPage) => {
	try {
		const response = await Axios.get(`/api/v1/log/currency/list?start_dt=${startDate}&end_dt=${endDate}
		&orderby=${order}&page_no=${currentPage}&page_size=${size}`, {
			headers: {
				Authorization: `Bearer ${token}`,
				'Content-Type': 'application/json',
			},
		});

		return response.data;
	} catch (error) {
		console.error('getCurrencyList API error:', error);
		throw error;
	}
};

export const GameCurrencyLogExport = async (token, params, fileName) => {
	try {
		await Axios.post(`/api/v1/log/currency/excel-export`, params, {
			headers: { Authorization: `Bearer ${token}` },
			responseType: 'blob',
			timeout: 300000
		}).then(response => {

			responseFileDownload(response, {
				defaultFileName: fileName
			});
		});
	} catch (e) {
		if (e instanceof Error) {
			throw new Error('GameCurrencyLogExport Error', e);
		}
	}
};

export const getCurrencyDetailList = async (token, searchType, searchData, tranId, logAction, currencyType, amountDeltaType, startDate, endDate, order, size, currentPage) => {
	try {
		const response = await Axios.get(`/api/v1/log/currency/detail/list?search_type=${searchType}&search_data=${searchData}&tran_id=${tranId}
			&log_action=${logAction}&currency_type=${currencyType}&amount_delta_type=${amountDeltaType}&start_dt=${startDate}&end_dt=${endDate}
			&orderby=${order}&page_no=${currentPage}&page_size=${size}`, {
			headers: {
				Authorization: `Bearer ${token}`,
				'Content-Type': 'application/json',
			},
		});

		return response.data;
	} catch (error) {
		console.error('getCurrencyDetailList API error:', error);
		throw error;
	}
};

export const GameCurrencyDetailLogExport = async (token, params, fileName) => {
	try {
		console.log(params);
		await Axios.post(`/api/v1/log/currency/detail/excel-export`, params, {
			headers: { Authorization: `Bearer ${token}` },
			responseType: 'blob',
			timeout: 300000
		}).then(response => {

			responseFileDownload(response, {
				defaultFileName: fileName
			});
		});
	} catch (e) {
		if (e instanceof Error) {
			throw new Error('GameCurrencyDetailLogExport Error', e);
		}
	}
};