import express from "express";
import { getSuitStyles, getList, getListItemsOnAnySuit, getByID, getFinancials } from "~/services/suit/index";
import svcItems from "~/services/suit/items";
import svcCore from "~/services/core";
import { fullSuitPhotoUpload } from "~/services/photos";

// eslint-disable-next-line new-cap
const router = express.Router();

type iReqItems = {
	query: {
		itemType: string;
	};
	params: {
		fullsuitID: string;
	};
	body?: {
		itemType: string;
		itemID: string;
	};
};

type iReqDelete = {
	query: {
		itemID: string;
	};
	params: {
		fullsuitID: string;
	};
};

router.get("/", async function (req, res) {
	res.json(await getList());
});

router.get("/itemsAssignedToAnySuit", async function (req, res) {
	res.json(await getListItemsOnAnySuit());
});

router.get("/styles", async function (req, res) {
	res.json(await getSuitStyles());
});

router.get("/:fullsuitID", async function (req, res) {
	res.json(await getByID(req.params.fullsuitID));
});

router.patch("/:fullsuitID", async function (req, res) {
	res.json(
		await svcCore.patch({
			table: "fullsuit",
			whereClause: `fullsuitID=${parseInt(req.params.fullsuitID, 10)}`,
			patchBody: req.body,
		}),
	);
});

router.get("/:fullsuitID/financials", async function (req, res) {
	res.json(await getFinancials(req.params.fullsuitID));
});

router.get("/:fullsuitID/items", async function (req: iReqItems, res) {
	res.json(await svcItems.getItems(req.query.itemType.toUpperCase(), req.params.fullsuitID));
});

router.post("/:fullsuitID/items", async function (req: iReqItems, res) {
	res.json(await svcItems.addItem(req.params.fullsuitID, req.body.itemType, req.body.itemID));
});

router.delete("/:fullsuitID/items", async function (req: iReqDelete, res) {
	res.json(
		await svcItems.deleteItem({
			itemID: req.query.itemID,
			fullsuitID: req.params.fullsuitID,
		}),
	);
});

router.patch("/:fullsuitID/photo", async function (req, res) {
	res.json(
		await fullSuitPhotoUpload(req),
		// await svcCore.patch({
		// 	table: "fullsuit",
		// 	whereClause: `fullsuitID=${parseInt(req.params.fullsuitID, 10)}`,
		// 	patchBody: [{
		// 		col: 'photo',
		// 		type: 'varchar',
		// 		value: req,
		// 	}],
		// }),
	);
});

export default router;
