import { ItemLiquidable } from "@win2win/shared";
import { chain, keys, sumBy } from "lodash";
import { formatContactName } from "src/helpers/formatters";
import { computed, Ref } from "vue";
import { ItemLiquidado } from "../models";

export function useGroupLiquidacionItems(rawItems: Ref<(ItemLiquidable | ItemLiquidado)[]>) {

    const items = computed(() => (rawItems.value || [])
        .map((item) => ({
            id: item.captador.ID_USUARIO,
            nombre: formatContactName(item.captador.CONTACTO),
            codigo: item.captador.COD_USU_MVX,
            gerencia: item.gerencia ? {
                id: item.gerencia.ID_GERENCIA,
                nombre: item.gerencia.NOMBRE,
            } : {
                id: null,
                nombre: 'Sin gerencia'
            },
            delegacion: item.delegacion ? {
                id: item.delegacion.ID_DELEGACION,
                nombre: item.delegacion.NOMBRE,
                codigo: item.delegacion.COD_DEL_MVX,
            } : {
                id: null,
                nombre: 'Sin delegación',
                codigo: '~'
            },
            comision: item.comision_calculada,
            total: keys(item.comision_calculada).reduce((acc, key) => acc + item.comision_calculada[key], 0),
            confirmacion_captador: item['confirmacion_captador'],
            confirmacion_gerencia: item['confirmacion_gerencia']
        }))
    );

    const delegaciones = computed(() => {
        const promotores = chain(items.value)
            .groupBy('id')
            .mapValues((items) => {
                return {
                    ...items[0],
                    total: sumBy(items, (v) => v.comision.PROMOTOR),
                    count: items.length,
                    items: items.map((item) => ({
                        total: item.comision.PROMOTOR,
                        count: 1,
                        comision: item.comision
                    })),
                    state: items.every(item => item.confirmacion_captador) ? 1 : 0,
                };
            })
            .value();

        const gerencias = chain(promotores)
            .groupBy((v) => v.gerencia.id)
            .mapValues((_promotores) => {
                const id = _promotores[0].gerencia.id;
                const actividad = sumBy(items.value.filter(item => item.gerencia.id === id), (item) => item.comision.GERENTE);
                const ventas = sumBy(_promotores, 'total');
                return {
                    delegacion: _promotores[0].delegacion,
                    nombre: _promotores[0].gerencia.nombre,
                    actividad,
                    ventas,
                    total: actividad + ventas,
                    items: _promotores,
                    count: _promotores.length,
                    state: _promotores[0].confirmacion_gerencia ? 1 : 0
                };
            })
            .value();

        const _delegaciones = chain(gerencias)
            .groupBy((v) => v.delegacion.id)
            .mapValues((_gerencias) => {
                const id = _gerencias[0].delegacion.id;
                const actividad = sumBy(items.value.filter(item => item.delegacion.id === id), (item) => item.comision.ORGANIZADOR); const gerencias = sumBy(_gerencias, 'total');
                const delegacion = _gerencias[0].delegacion;
                return {
                    ...delegacion,
                    actividad,
                    total: actividad + gerencias,
                    items: _gerencias,
                    count: sumBy(_gerencias, 'count'),
                };
            })
            .value();

        return _delegaciones;
    });

    return {
        items,
        delegaciones
    }

}
