'use strict';

async function statistic (ctx) {
    try {
        const models = ctx.fs.dc.models;
        const { userId } = ctx.fs.api;
        const { projectType } = ctx.query;

        let findOption = {
            where: {
                reportType: 'conserve',
            },
            attributes: ['id', 'road', 'time', 'projectType', 'projectType', 'roadSectionStart', 'roadSectionEnd', 'scenePic', 'conserveBeforePic', 'conserveUnderwayPic', 'conserveAfterPic'],
            include: [{
                model: models.User,
                attributes: ['name']
            }],
            order: [['time', 'DESC']]
        }

        if (projectType) {
            findOption.where.projectType = projectType;
        }

        const reportRes = await await models.Report.findAll(findOption)
        const reportCount = await models.Report.count({
            attributes: ['projectType'],
            group: ['projectType'],
            where: {
                reportType: 'conserve',
            },
        })

        ctx.status = 200
        ctx.body = {
            processed: reportRes.length,
            reportCount: reportCount,
            reportList: reportRes,
        }
    } catch (error) {
        ctx.fs.logger.error(`path: ${ctx.path}, error: ${error}`);
        ctx.status = 400;
        ctx.body = {
            message: typeof error == 'string' ? error : undefined
        }
    }
}

module.exports = {
    statistic,
};