const pool = require('../db/connection');

const totalrevenue = async (req, res) => 
{   
    try{
    const {start_date,end_date} = req.body
    const result = await pool.query('SELECT * FROM totalrevenue($1,$2) as status',[start_date,end_date]);
    const totalrenueve = result.rows[0].status; 
    /*const totalrenueve = result.rows.map(re => ({
      total: re.total
    }));*/

    
    res.json({
        data:totalrenueve
    });

    }catch(error)
    {
    console.error(error);
    res.status(500).json({ message: 'Error al obtener el dato' });   
    }
};

const cogs = async (req, res) => 
{
    try{
    const {start_date,end_date} = req.body;
    const result = await pool.query('SELECT * FROM cogs($1,$2) as status',[start_date,end_date]);
    const cogs = result.rows[0].status; 
    /*const cogs = result.rows.map(re => ({
      cogs_total: re.cogs_total,
    }));*/


    res.json({
        message: "Se obtuvieron los ingresos",
        data:cogs
    });

    }catch(error)
    {
    console.error(error);
    res.status(500).json({ message: error });
    }
};

const employeeshare = async (req, res) => 
{   
    try{
    const {start_date,end_date} = req.body
    const result = await pool.query('SELECT * FROM employeeshare($1,$2) as status ',[start_date,end_date]);
    const employeeshare = result.rows[0].status; 
    /*const employeeshare = result.rows.map(re => ({
      total_share: re.total_share,
    }));*/
    res.json({
        data:employeeshare
    });

    }catch(error)
    {
    console.error(error);
    res.status(500).json({ message: 'Error al obtener el dato' });   
    }
};

const tips = async (req, res) => 
{   
    try{
    const {start_date,end_date} = req.body
    const result = await pool.query('SELECT * FROM tips($1,$2) as status',[start_date,end_date]);
    const tips = result.rows[0].status; 
    /*const tips = result.rows.map(re => ({
      tips: re.tips
    }));*/

    
    res.json({
        message: "Se obtuvieron los ingresos",
        data:tips
    });

    }catch(error)
    {
    console.error(error);
    res.status(500).json({ message: 'Error al obtener el dato' });   
    }
};

const grossprofit = async (req, res) => 
{   
    try{
    const {start_date,end_date} = req.body;
    const result = await pool.query('SELECT * FROM grossprofit($1,$2) as status',[start_date,end_date]);
    const grossprofit = result.rows[0].status;
    /*const grossprofit = result.rows.map(re => ({
      grossprofit: re.grossprofit
    }));*/

    
    res.json({
        message: "Se obtuvieron los ingresos",
        data:grossprofit
    });

    }catch(error)
    {
    console.error(error);
    res.status(500).json({ message: 'Error al obtener el dato' });   
    }
};


const weightedCategoriesCost = async (req, res) => 
{   
    try{
    const {start_date,end_date} = req.body
    const result = await pool.query('SELECT * FROM weighted_categories_cost($1,$2)',[start_date,end_date]);
    const weightedCategoriesCost = result.rows.map(re => ({
      id_expense_cat : re.id_expense_cat ,
      category_name: re.category_name ,
      spanish_name : re.spanish_name ,
      total : re.total,
      participation : re.participation 
    }));
    res.json({
        data:weightedCategoriesCost
    });

    }catch(error)
    {
    console.error(error);
    res.status(500).json({ message: 'Error al obtener el dato' });   
    }
};

const ebitda = async (req, res) => 
{   
    try{
    const {start_date,end_date} = req.body;
    const result = await pool.query('SELECT * FROM ebitda($1,$2)', [start_date,end_date]);
    const ebitda = result.rows.map(re => ({
      expenses_total: re.expenses_total,
      ebitda : re.ebitda
    }));

    
    res.json({
        message: "Se obtuvieron los ingresos",
        data:ebitda
    });

    }catch(error)
    {
    console.error(error);
    res.status(500).json({ message: 'Error al obtener el dato' });   
    }
};

module.exports = {ebitda,weightedCategoriesCost,grossprofit,totalrevenue,cogs,employeeshare,tips};