import { HttpStatus } from '@utils';
import { catchAsync, deserializeIfJSON } from '@utils';
import { logsService } from '@services';
import { Response } from "express";

import { pick } from '@utils';
import { ExtRequest } from '@types';

const getLogs = catchAsync(async (req: ExtRequest, res: Response) => {
    const filter = typeof req.query.filter === 'string'
        ? deserializeIfJSON(req.query.filter) || {}
        : {};

    const options = pick(req.query, ['sortBy', 'limit', 'page']);
    const result = await logsService.queryLogs(filter, options);
    res.send(result);
});

const distinct = catchAsync(async (req: ExtRequest, res: Response) => {
    const { field } = pick(req.query, ['field']);
    const result = await logsService.distinct(String(field));
    res.send(result);
});

const createLog = catchAsync(async (req: ExtRequest, res: Response) => {
    const user = await logsService.createLogs(req.body);
    res.status(HttpStatus.CREATED).send(user);
});

const createMany = catchAsync(async (req: ExtRequest, res: Response) => {
    const result = await logsService.createManyLogs(req.body);
    res.status(HttpStatus.CREATED).send(result);
});

export {
    getLogs,
    distinct,
    createLog,
    createMany,
};
