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

const addNewRoom = async (req, res) => {
    try{
        const  {room,units,cost_per_nigth,guest,bed,amenities,products} = req.body;

        const result = await pool.query(`SELECT new_room($1,$2,$3,$4,$5,$6::jsonb,$7::jsonb) AS room_id`,
                                        [room,units,cost_per_nigth,guest,bed,JSON.stringify(amenities),JSON.stringify(products)]);
        const newRoom = result.rows[0].room_id; 
        res.json({
        message: "Se agrego correctamente la habitacion",
        new_room: newRoom
    });
    }catch(error) {
    console.error(error);
    res.status(500).json({ message: 'No se pudo agregar nueva habitacion' });
    }


};
const reportRoom = async(req,res) =>{
    try{
    const result = await pool.query('SELECT * FROM reportrooms()');

            const rooms = result.rows.map(ro => ({
            id_room: ro.id_room,
            name_room: ro.name_room,
            guests: ro.guests,
            bed_type: ro.bed_type,
            quantity: ro.quantity
         })); 
              res.json({
              message: "Se obtuvo el reporte de habitaciones",
              new_product: rooms
                 });
        }catch(error){
            console.error(error);
            res.status(500).json({ message: 'No pudo recuperar la tabla de reporte.'});
        }
};
const addNewProperty = async (req, res) => {
    try{
        const  {new_name,addres_pro,main_area_pro,rfc_pro,phone,areas,services} = req.body;

        const result = await pool.query(`SELECT new_property($1,$2,$3,$4,$5,$6::jsonb,$7::jsonb) AS property_id`,
                                        [new_name,addres_pro,main_area_pro,rfc_pro,phone,JSON.stringify(areas),JSON.stringify(services)]);
        const newRoom = result.rows[0].property_id; 
        res.json({
        message: "Se agrego correctamente la propiedad",
        new_property: newRoom
    });
    }catch(error) {
    console.error(error);
    res.status(500).json({ message: 'No se pudo agregar nueva propiedad' });
    }


};
const reportProperties = async (req, res) => {
    try{
     const result = await pool.query('SELECT * FROM reportproperties()');
     const property = result.rows.map(ro => ({
            id_prop: ro.id_prop,
            name_property: ro.name_property,
            areas: ro.areas
         }));
          res.json({
              message: "Se obtuvo el reporte de propiedades",
              reportProperties: property
                 });
    }catch(error) {
    console.error(error);
    res.status(500).json({ message: 'No se pudo consultar las propiedades' });
    }
};

const getunits = async (req, res) => {
    try{
        const result = await pool.query('SELECT * FROM getunits()');
            const units = result.rows.map(u => ({
            id_unit: u.id_unit,
            name_unit: u.name_unit}));
        res.json({
              message: "Se obtuvieron las unidades de medida",
              units: units
                 });


    }catch(error){
    console.error(error);
    res.status(500).json({ message: 'No se pudo recuperar las unidades' });
    }

};

const getapproveby = async (req, res) => {
    try{
        const result = await pool.query('SELECT * FROM getapproveby()');
            const approveby = result.rows.map(ap => ({
            id_user: ap.id_user,
            user_name: ap.user_name}));
        res.json({
              message: "Se obtuvieron las aprobadores",
              approve: approveby
                 });


    }catch(error){
    console.error(error);
    res.status(500).json({ message: 'No se pudo recuperar los aprobadores' });
    }

};

const getcurrency = async (req, res) => {
    try{
        const result = await pool.query('SELECT * FROM getcurrency()');
            const currency = result.rows.map(u => ({
            id_currency: u.id_currency,
            name_currency: u.name_currency}));
        res.json({
              message: "Se obtuvieron las monedas",
              currency: currency
                 });

    }catch(error){
    console.error(error);
    res.status(500).json({ message: 'No se pudo recuperar las monedas' });
    }

};
const getrecurrence = async (req, res) => {
    try{
        const result = await pool.query('SELECT * FROM getrecurrence()');
            const recurrence = result.rows.map(u => ({
            id_recurrence: u.id_recurrence,
            name_recurrence: u.name_recurrence}));
        res.json({
              message: "Se obtuvieron las recurrencias",
              currency: recurrence
                 });

    }catch(error){
    console.error(error);
    res.status(500).json({ message: 'No se pudo recuperar la recurrencia' });
    }

};

const getrequestby = async (req, res) => {
    try{
        const result = await pool.query('SELECT * FROM getrequestby()');
            const units = result.rows.map(u => ({
            id_user: u.id_user,
            user_name: u.user_name}));
        res.json({
              message: "Se obtuvieron los solicitantes",
              request: units
                 });


    }catch(error){
    console.error(error);
    res.status(500).json({ message: 'No se pudo recuperar los solicitantes' });
    }

};

const getcategoryexpense = async (req, res) => {
    try{
        const result = await pool.query('SELECT * FROM getcategoryexpense()');
            const categoryex = result.rows.map(u => ({
            id_category: u.id_category,
            name_category: u.name_category}));
        res.json({
              message: "Se obtuvieron las categorias del gasto",
              categoryex: categoryex
                 });


    }catch(error){
    console.error(error);
    res.status(500).json({ message: 'No se pudo las categorias del gasto' });
    }

};
module.exports = {addNewRoom,reportRoom,addNewProperty,reportProperties,getunits,
                  getcategoryexpense,getrequestby,getcurrency,getapproveby,getrecurrence};