import friendshipService from '../services/friendshipService.js';
import Joi from 'joi';

const friendshipController = {
    async endFriendship(req, res) {
        const schema = Joi.object({
            friendUserId: Joi.string().required()
        });
        const { error, value } = schema.validate(req.body);
        if (error) {
            return res.status(400).json({ error: error.details[0].message });
        }
        try {
            const { userid: hashedUserId } = req.headers;
            await friendshipService.endFriendship(hashedUserId, value.friendUserId);
            res.status(200).json({ message: 'Friendship ended successfully' });
        } catch (error) {
            console.error('Error in endFriendship:', error);
            res.status(400).json({ error: error.message });
        }
    },

    async acceptFriendship(req, res) {
        const schema = Joi.object({
            friendUserId: Joi.string().required()
        });
        const { error, value } = schema.validate(req.body);
        if (error) {
            return res.status(400).json({ error: error.details[0].message });
        }
        try {
            const { userid: hashedUserId } = req.headers;
            await friendshipService.acceptFriendship(hashedUserId, value.friendUserId);
            res.status(200).json({ message: 'Friendship accepted successfully' });
        } catch (error) {
            console.error('Error in acceptFriendship:', error);
            res.status(400).json({ error: error.message });
        }
    },

    async rejectFriendship(req, res) {
        const schema = Joi.object({
            friendUserId: Joi.string().required()
        });
        const { error, value } = schema.validate(req.body);
        if (error) {
            return res.status(400).json({ error: error.details[0].message });
        }
        try {
            const { userid: hashedUserId } = req.headers;
            await friendshipService.rejectFriendship(hashedUserId, value.friendUserId);
            res.status(200).json({ message: 'Friendship rejected successfully' });
        } catch (error) {
            console.error('Error in rejectFriendship:', error);
            res.status(400).json({ error: error.message });
        }
    },

    async withdrawRequest(req, res) {
        const schema = Joi.object({
            friendUserId: Joi.string().required()
        });
        const { error, value } = schema.validate(req.body);
        if (error) {
            return res.status(400).json({ error: error.details[0].message });
        }
        try {
            const { userid: hashedUserId } = req.headers;
            await friendshipService.withdrawRequest(hashedUserId, value.friendUserId);
            res.status(200).json({ message: 'Friendship request withdrawn successfully' });
        } catch (error) {
            console.error('Error in withdrawRequest:', error);
            res.status(400).json({ error: error.message });
        }
    },

    async getFriendships(req, res) {
        try {
            const { userid: hashedUserId } = req.headers;
            const { acceptedOnly } = req.query;
            console.log('Friendships:', acceptedOnly);
            const friendships = await friendshipService.getFriendships(hashedUserId, acceptedOnly === 'true');
            res.status(200).json(friendships);
        } catch (error) {
            console.error('Error in getFriendships:', error);
            res.status(400).json({ error: error.message });
        }
    },
};

export default friendshipController;
