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

class SettingsController {
    async filterSettings(req, res) {
        const { userid, type } = req.body;
        try {
            const responseFields = await settingsService.filterSettings(userid, type);
            res.status(200).json(responseFields);
        } catch (error) {
            console.error('Error filtering settings:', error);
            res.status(500).json({ error: 'An error occurred while filtering the settings' });
        }
    }

    async updateSetting(req, res) {
        const schema = Joi.object({
            userid: Joi.string().required(),
            settingId: Joi.number().integer().required(),
            value: Joi.any().required()
        });
        const { error, value } = schema.validate(req.body);
        if (error) {
            return res.status(400).json({ error: error.details[0].message });
        }
        try {
            await settingsService.updateSetting(value.userid, value.settingId, value.value);
            res.status(200).json({ message: 'Setting updated successfully' });
        } catch (error) {
            console.error('Error updating user setting:', error);
            res.status(500).json({ error: 'Internal server error' });
        }
    }

    async getTypeParamValueId(req, res) {
        const { paramValue } = req.body;
        try {
            const paramValueId = await settingsService.getTypeParamValueId(paramValue);
            res.status(200).json({ paramValueId });
        } catch (error) {
            console.error('Error retrieving parameter value ID:', error);
            res.status(404).json({ error: "notfound" });
        }
    }

    async getTypeParamValues(req, res) {
        const { type } = req.body;
        try {
            const paramValues = await settingsService.getTypeParamValues(type);
            res.status(200).json(paramValues);
        } catch (error) {
            console.error('Error retrieving parameter values:', error);
            res.status(500).json({ error: 'An error occurred while retrieving the parameter values' });
        }
    }

    async getTypeParamValue(req, res) {
        const { id } = req.params;
        try {
            const paramValue = await settingsService.getTypeParamValue(id);
            res.status(200).json({ paramValue });
        } catch (error) {
            console.error('Error retrieving parameter value:', error);
            res.status(404).json({ error: "notfound" });
        }
    }

    async getAccountSettings(req, res) {
        try {
            const { userId } = req.body;
            const accountSettings = await settingsService.getAccountSettings(userId);
            res.status(200).json(accountSettings);
        } catch (error) {
            console.error('Error retrieving account settings:', error);
            res.status(500).json({ error: 'Internal server error' });
        }
    }

    async setAccountSettings(req, res) {
        const schema = Joi.object({
            userId: Joi.string().required(),
            settings: Joi.object().required()
        });
        const { error, value } = schema.validate(req.body);
        if (error) {
            return res.status(400).json({ error: error.details[0].message });
        }
        try {
            await settingsService.setAccountSettings(value);
            res.status(200).json({ message: 'Account settings updated successfully' });
        } catch (error) {
            console.error('Error updating account settings:', error);
            res.status(500).json({ error: 'Internal server error' });
        }
    }

    async getPossibleInterests(req, res) {
        try {
            const { userid: userId } = req.headers;
            const interests = await settingsService.getPossibleInterests(userId);
            res.status(200).json(interests);
        } catch (error) {
            console.error('Error retrieving possible interests:', error);
            res.status(500).json({ error: 'An error occurred while retrieving the possible interests' });
        }
    }

    async getInterests(req, res) {
        try {
            const { userid: userId } = req.headers;
            const interests = await settingsService.getInterests(userId);
            res.status(200).json(interests);
        } catch (error) {
            console.error('Error retrieving interests:', error);
            res.status(500).json({ error: 'Internal server error' });
        }
    }

    async addInterest(req, res) {
        const schema = Joi.object({
            name: 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 interest = await settingsService.addInterest(userId, value.name);
            res.status(200).json({ interest });
        } catch (error) {
            console.error('Error adding interest:', error);
            res.status(500).json({ error: 'Internal server error' });
        }
    }

    async addUserInterest(req, res) {
        const schema = Joi.object({
            interestid: Joi.number().integer().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;
            await settingsService.addUserInterest(userId, value.interestid);
            res.status(200).json({ message: 'User interest added successfully' });
        } catch (error) {
            console.error('Error adding user interest:', error);
            res.status(500).json({ error: 'Internal server error' });
        }
    }

    async removeInterest(req, res) {
        try {
            const { userid: userId } = req.headers;
            const { id: interestId } = req.params;
            await settingsService.removeInterest(userId, interestId);
            res.status(200).json({ message: 'Interest removed successfully' });
        } catch (error) {
            console.error('Error removing interest:', error);
            res.status(500).json({ error: 'Internal server error' });
        }
    }

    async getVisibilities(req, res) {
        try {
            const visibilities = await settingsService.getVisibilities();
            res.status(200).json(visibilities);
        } catch (error) {
            console.error('Error retrieving visibilities:', error);
            res.status(500).json({ error: 'Internal server error' });
        }
    }

    async updateVisibility(req, res) {
        const { userParamTypeId, visibilityId } = req.body;
        const hashedUserId = req.headers.userid;
        try {
            await settingsService.updateVisibility(hashedUserId, userParamTypeId, visibilityId);
            res.status(200).json({ message: 'Visibility updated successfully' });
        } catch (error) {
            console.error('Error updating visibility:', error);
            res.status(500).json({ error: 'Internal server error' });
        }
    }
}

export default SettingsController;
