import express, { Request, Response } from "express";
import { getLogin } from "../auth";
import { parseToken } from "../utils";
import { getStats } from "../stats";
import { WeeklyStats } from "../../../types/gen/types.gen";

const router = express.Router();

router.get("/", async (req: Request<{}, any, undefined>, res: Response<WeeklyStats>) => {
    getLogin(parseToken(req));
    if (typeof req.query.startDate === 'string' && typeof req.query.endDate === 'string') {
        try {
            const data = await getStats(req.query.startDate, req.query.endDate);
            return res.status(200).json(data);
        } catch (e) {
            // necháme to zatím spadnout na 400
        }
    }
    res.sendStatus(400);
});

export default router;