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

class ContactController {
    constructor() {
        this.addContactMessage = this.addContactMessage.bind(this);
    }

    async addContactMessage(req, res) {
        const schema = Joi.object({
            email: Joi.string().email().required(),
            name: Joi.string().min(1).max(255).required(),
            message: Joi.string().min(1).max(2000).required(),
            acceptDataSave: Joi.boolean().required()
        });
        const { error, value } = schema.validate(req.body);
        if (error) {
            return res.status(400).json({ error: error.details[0].message });
        }
        try {
            await ContactService.addContactMessage(value.email, value.name, value.message, value.acceptDataSave);
            res.status(200).json({ status: 'ok' });
        } catch (error) {
            res.status(409).json({ error: error.message });
        }
    }
}

export default ContactController;
