import { Request, Response } from 'express';
//import validator from 'joi'; // DOCS: https://joi.dev/api
import validator from '../../../../handlers/validation.js';
import { Prisma } from '@prisma/client';

// MARK: GET transaction
const schema_get = validator
	.object({
		sort: validator
			.string()
			.valid(...Object.keys(Prisma.TransactionsScalarFieldEnum))
			.default('id'),

		order: validator.string().valid('asc', 'desc').default('asc'),
		take: validator.number().min(1).max(512),
		skip: validator.number().min(0),

		id: validator.number().positive().precision(0),
		user_id: validator.number().positive().precision(0),
		paid: validator.boolean().note('true-> Only paid / false-> Only unpaid / undefined-> both')
	})
	.nand('id', 'user_id'); // Allow id or user_id. not both.

// MARK: CREATE transaction
const schema_post = validator.object({
	products: validator.array().items(validator.number().positive().precision(0)).required(),
	user_id: validator.number().positive().precision(0).required(),
	paid: validator.boolean().default(false)
});

// MARK: UPDATE transaction
const schema_patch = validator.object({
	id: validator.number().positive().precision(0).required(),
	user_id: validator.number().positive().precision(0).required(),
	paid: validator.boolean().default(false)
});

// MARK: DELETE transaction
const schema_del = validator.object({
	id: validator.number().positive().precision(0).required()
});

// Describe all schemas
const schema_get_desc = schema_get.describe();
const schema_post_desc = schema_post.describe();
const schema_patch_desc = schema_patch.describe();
const schema_del_desc = schema_del.describe();

// GET route
export default async function get(req: Request, res: Response) {
	res.status(200).json({
		GET: schema_get_desc,
		POST: schema_post_desc,
		PATCH: schema_patch_desc,
		DELETE: schema_del_desc
	});
}

export { schema_get, schema_post, schema_patch, schema_del };
