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/:webSiteId", (req, res) => {
        const { webSiteId } = req.params;
        evaluatorService.getSequenceEvaluator(webSiteId).then((evaluator) => {
            return res.send(evaluator);
        }).catch((error) => {
            return res.status(ERROR_STATUS).send(error);
        });
    });

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

    app.post("/evaluator/update", (req, res) => {
        const { name, webSiteId, stepList } = req.body;
        evaluatorService.updateSequenceEvaluator(name, webSiteId, 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 { webSiteId, interactionList } = req.body;
        console.log("route body", interactionList);
        evaluatorService.evaluateSequence(webSiteId, interactionList).then((evaluation) => {
            console.log(evaluation);
            return res.status(SUCCESS_STATUS).send({evaluation});
        }).catch((error) => {
            console.error(error);
            return res.status(ERROR_STATUS).send(error);
        });
    });

}
