import { NextFunction, Response } from "express";
import { CustomRequest } from "../types/custom.types";
import Category from "../models/category.model";
import Channel from "../models/channel.model";
import Source from "../models/source.model";
import Authority from "../models/authority.model";
import { STATE, VISIBILITY } from "../types/common.types";

export default class OverviewController {
    static async overview(request: CustomRequest, response: Response, next: NextFunction) {
        try {
            const categories = await Category.findAll({ attributes: ['status'] });
            const channels = await Channel.findAll({ attributes: ['status'] });
            const sources = await Source.findAll({ attributes: ['status'] });
            const authorities = await Authority.findAll({ attributes: ['status'] });
            response.status(200).json({
                rest : categories,
                categories : {
                    visible : categories.filter(e=>e.status=== VISIBILITY.VISIBLE).length,
                    hidden : categories.filter(e=>e.status=== VISIBILITY.HIDDEN).length,
                },
                channels : {
                    visible : channels.filter(e=>e.status=== VISIBILITY.VISIBLE).length,
                    hidden : channels.filter(e=>e.status=== VISIBILITY.HIDDEN).length,
                },
                sources : {
                    visible : sources.filter(e=>e.status === VISIBILITY.VISIBLE).length,
                    hidden : sources.filter(e=>e.status === VISIBILITY.HIDDEN).length,
                },
                authorities : {
                    active : authorities.filter(e=>e.status=== STATE.ACTIVE).length,
                    inactive : authorities.filter(e=>e.status=== STATE.INACTIVE).length,
                }
            })
        } catch (error) {
            next(error);
        }
    }
}