const express = require('express');
const {
	createPost,
	getAllPosts,
	getPost,
	updatePost,
	deletePost,
	createPostComment,
	updatePostComment,
	deletePostComment,
	getPostComments,
	getPostLikes,
	handleCommentLike,
	getCommentLikes,
	handlePostLike,
	sharePost,
	getProfilePosts,
	handlePostTag,
	taggedPhotos,
	getProfilePostById,
} = require('../../controllers/members/posts');
const { memberAuth, validation } = require('../../middlewares');
const { body } = require('express-validator');

const router = express.Router();

router.get('/profile', memberAuth, getProfilePosts);
router.get('/profile/:id', memberAuth, getProfilePostById);

router.patch(
	'/tags',
	memberAuth,
	[body('postId').notEmpty(), body('status').isIn(['Accepted', 'Rejected'])],
	handlePostTag,
);
router.get('/tags/photos', memberAuth, taggedPhotos);

router.post(
	'/share',
	memberAuth,
	[
		body('postId').notEmpty().withMessage('postId is required'),
		body('content').optional(),
		body('shareContent').optional(),
		body('shareTags').optional().isArray({ min: 1 }).withMessage('shareTags must be an array of member IDs'),
		validation,
	],
	sharePost,
);

const createPostValidation = [body('content').notEmpty().withMessage('content is required'), validation];

router.post(
	'/comments/likes',
	memberAuth,
	[body('commentId').notEmpty(), body('like').isBoolean().withMessage('like (boolean) is required'), validation],
	handleCommentLike,
);

router.get('/comments/likes/:commentId', memberAuth, getCommentLikes);

router.post('/comments/:postId', memberAuth, [body('comment').notEmpty(), validation], createPostComment);
router.get('/comments/:postId', memberAuth, getPostComments);
router.patch('/comments/:id', memberAuth, updatePostComment);
router.delete('/comments/:id', memberAuth, deletePostComment);

router.post(
	'/likes',
	memberAuth,
	[body('postId').notEmpty(), body('like').isBoolean().withMessage('like (boolean) is required'), validation],
	handlePostLike,
);
router.get('/likes/:postId', memberAuth, getPostLikes);

router.post('/', memberAuth, createPostValidation, createPost);
router.get('/', memberAuth, getAllPosts);
router.get('/:id', memberAuth, getPost);
router.patch('/:id', memberAuth, updatePost);
router.delete('/:id', memberAuth, deletePost);

module.exports = router;
