import { responseMessage, failureMessage, executeQuery } from "~/lib/db";
import cache from "~/functions/cache";
import helper from "~/functions/helper";
import { convertToDate } from "~/functions/dates";

const itemType = "library";
const cacheKeyGetBookList = "getLibraryBooks";

export async function getBooks() {
	const qry = await executeQuery({
		query: `
            SELECT
                *
            FROM
                library
			ORDER BY
				groupID,
				title
	`,
	});
	const data = helper.emptyOrRows(qry);

	return data;
}

export async function getGroups() {
	const qry = await executeQuery({
		query: `
            SELECT
                *
            FROM
                library_groups
			ORDER BY
				groupSortOrder
	`,
	});
	const data = helper.emptyOrRows(qry);

	return data;
}

export async function getOneBook(libraryBookID: string) {
	const qry = await executeQuery({
		query: `
            SELECT
                *
            FROM
                library
			WHERE
				libraryBookID=${libraryBookID}
	`,
	});
	const data = helper.emptyOrRows(qry);

	if (data.error || data.length === 0) {
		return failureMessage("getOne", itemType);
	} else {
		return data[0];
	}
}

export async function addBook(body) {
	const loanedDate = convertToDate(body.loanedDate);
	// body.loanedDate === "" ? null : `\'${body.loanedDate.substr(0, 10)}\'`;

	const query = `
		INSERT INTO library (
			groupID,
			asin,
			title,
			author,
			qty,
			loanedTo,
			loanedDate
		) VALUES (
			${body.groupID},
			'${body.asin}',
			'${body.title}',
			'${body.author}',
			${body.qty},
			'${body.loanedTo}',
			${loanedDate}
		)
	`;

	// console.log("query:::::", query);

	const result = await executeQuery({ query });

	if (result.error) {
		throw failureMessage("create", itemType);
	}

	// get a new cache
	cache.del(cacheKeyGetBookList);

	return responseMessage("create", itemType, result);
}

export async function updateBook(libraryBookID: string, body) {
	const loanedDate = convertToDate(body.loanedDate);

	const query = `
			UPDATE
				library
			SET
				groupID=${body.groupID},
				asin='${body.asin}',
				title='${body.title.replace("'", "\\'")}',
				author='${body.author}',
				qty=${body.qty},
				loanedTo='${body.loanedTo}',
				loanedDate=${loanedDate}
			WHERE
				libraryBookID=${libraryBookID}
		`;

	const result = await executeQuery({ query });

	if (result.error) {
		throw result.error;
	} else {
		// remove the cache
		cache.del(cacheKeyGetBookList);

		return responseMessage("patch", itemType, result);
	}
}

export async function removeBook(libraryBookID: string) {
	const result = await executeQuery({
		query: `DELETE FROM library WHERE libraryBookID=${libraryBookID}`,
	});

	if (result.error) {
		throw failureMessage("delete", itemType);
	}

	// remove the cache
	cache.del(cacheKeyGetBookList);

	return responseMessage("delete", itemType, result);
}
