import ObjectiveService from "../application/SequenceEvaluationApplication";

const ERROR_STATUS: number = 500;
const SUCCESS_STATUS: number = 200;

export default function attachRoutes(app, evaluatorService: ObjectiveService): void {

    app.get("/evaluator/ping", (req, res) => {
        res.send("alive");
    });

    app.get("/evaluator/getEvaluator/:sessionId", (req, res) => {
        const { sessionId } = req.params;
        evaluatorService.getSequenceEvaluator(sessionId).then((evaluator) => {
            return res.send(evaluator);
        }).catch((error) => {
            return res.status(ERROR_STATUS).send(error);
        });
    });

    app.post("/evaluator/create", (req, res) => {
        const { name, sessionId, stepList } = req.body;
        evaluatorService.createSequenceEvaluator(name, sessionId, stepList).then(() => {
            return res.sendStatus(SUCCESS_STATUS);
        }).catch((error) => {
            return res.status(ERROR_STATUS).send(error);
        });
    });

    app.post("/evaluator/update", (req, res) => {
        const { name, sessionId, stepList } = req.body;
        evaluatorService.updateSequenceEvaluator(name, sessionId, stepList).then(() => {
            return res.sendStatus(SUCCESS_STATUS);
        }).catch((error) => {
            console.error(error);
            return res.status(ERROR_STATUS).send(error);
        });
    });

    app.post("/evaluator/evaluateSequence", (req, res) => {
        const { sessionId, interactionList } = req.body;
        evaluatorService.evaluateSequence(sessionId, interactionList).then((evaluation) => {
            return res.status(SUCCESS_STATUS).send({evaluation});
        }).catch((error) => {
            console.error(error);
            return res.status(ERROR_STATUS).send(error);
        });
    });

}
