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

const forumController = {
    async createForum(req, res) {
        const schema = Joi.object({
            name: Joi.string().min(1).max(255).required(),
            permissions: Joi.array().items(Joi.string().min(1).max(255)).required()
        });
        const { error, value } = schema.validate(req.body);
        if (error) {
            return res.status(400).json({ error: error.details[0].message });
        }
        try {
            const { userid: userId } = req.headers;
            const forum = await forumService.createForum(userId, value.name, value.permissions);
            res.status(201).json(forum);
        } catch (error) {
            console.error('Error in createForum:', error);
            res.status(400).json({ error: error.message });
        }
    },

    async deleteForum(req, res) {
        try {
            const { userid: userId } = req.headers;
            const { forumId } = req.params;
            await forumService.deleteForum(userId, forumId);
            res.status(200).json({ message: 'Forum deleted successfully' });
        } catch (error) {
            console.error('Error in deleteForum:', error);
            res.status(400).json({ error: error.message });
        }
    },

    async getAllForums(req, res) {
        try {
            const { userid: userId } = req.headers;
            const forums = await forumService.getAllForums(userId);
            res.status(200).json(forums);
        } catch (error) {
            console.error('Error in getAllForums:', error);
            res.status(400).json({ error: error.message });
        }
    },

    async getForum(req, res) {
        try {
            const { userid: userId } = req.headers;
            const { forumId, page } = req.params;
            const forum = await forumService.getForum(userId, forumId, page);
            res.status(200).json(forum);
        } catch (error) {
            console.error('Error in getForum:', error);
            res.status(400).json({ error: error.message });
        }
    },

    async createTopic(req, res) {
        const schema = Joi.object({
            forumId: Joi.number().integer().required(),
            title: Joi.string().min(1).max(255).required(),
            content: Joi.string().min(1).max(5000).required()
        });
        const { error, value } = schema.validate(req.body);
        if (error) {
            return res.status(400).json({ error: error.details[0].message });
        }
        try {
            const { userid: userId } = req.headers;
            const result = await forumService.createTopic(userId, value.forumId, value.title, value.content);
            res.status(201).json(result);
        } catch (error) {
            console.error('Error in createTopic:', error);
            res.status(400).json({ error: error.message });
        }
    },

    async getTopic(req, res) {
        try {
            const { userid: userId } = req.headers;
            const { id: topicId } = req.params;
            const topic = await forumService.getTopic(userId, topicId);
            res.status(200).json(topic);
        } catch (error) {
            console.error('Error in getTopic:', error);
            res.status(400).json({ error: error.message });
        }
    },

    async addMessage(req, res) {
        const schema = Joi.object({
            content: Joi.string().min(1).max(5000).required()
        });
        const { error, value } = schema.validate(req.body);
        if (error) {
            return res.status(400).json({ error: error.details[0].message });
        }
        try {
            const { userid: userId } = req.headers;
            const { id: topicId } = req.params;
            const result = await forumService.addMessage(userId, topicId, value.content);
            res.status(201).json(result);
        } catch (error) {
            console.error('Error in addMessage:', error);
            res.status(400).json({ error: error.message });
        }
    }
};

export default forumController;
