<?php

namespace App\Modules\BookingExtraPassenger\Domain;

use App\Modules\BookingExtraPassenger\Domain\IdBookingExtraPassenger;
use App\Modules\Shared\Domain\Storage\Criteria\StorageCriteria;
use Illuminate\Support\Facades\DB;

class ListPartnersBookingExtraPassenger implements StorageCriteria
{

    public function __construct(
        private IdBookingExtraPassenger $idBookingExtraPassenger,
    ) {}

    
    public function run()
    {
        return $this->analize($this->query());
    }

    protected function query()
    {
        $selectRoom = 'rp.RoomPassenger_Status, rp.Id_BookingExtraPassenger, rp.Id_Room';
        $selectPassenger = 'rp.Id_RoomPassenger, p.Id_Passenger, p.Passenger_Name, p.Passenger_LastName, p.Passenger_Email, p.Passenger_Phone, 
                            p.Passenger_PhoneCode, p.Passenger_Gender, p.Passenger_NoDocument, p.Passenger_Order, 
                            p.Passenger_Status, p.Id_TypeDocument, p.Id_UserCountry';    
        $selectTypeRoomProvider = 'r.Id_TypeRoom,r.Id_Provider, tr.TypeRoom_Name, tr.TypeRoom_Abrv, tr.TypeRoom_Capacity, tr.TypeRoom_Status,p.Provider_BusinessName, p.Provider_TradeName, p.Provider_NoDocument, p.Provider_Phone';

        $where = 'rp.RoomPassenger_Status <> 0';
        
        $result1 = DB::select(
            "SELECT $selectRoom
            FROM t_room_passenger as rp    
            WHERE $where
            AND rp.Id_BookingExtraPassenger = ?",
            [
                $this->idBookingExtraPassenger->value()
            ]
        );

     

        $finalResult = []; 
        if (count($result1) > 0) {
            foreach ($result1 as $roomPassenger) {
               
                $idRoom = $roomPassenger->Id_Room;

                $result2 = DB::select(
                    "SELECT $selectPassenger
                    FROM t_room_passenger as rp JOIN t_passenger as p 
                    ON p.Id_Passenger = rp.Id_Passenger
                    WHERE $where
                    AND rp.Id_Room = ?",
                    [
                        $idRoom
                    ]
                );   
                
                
                $result3 = DB::select(
                    "SELECT $selectTypeRoomProvider
                    FROM t_room as r JOIN t_type_room as tr 
                    ON r.Id_TypeRoom = tr.Id_TypeRoom
                    LEFT JOIN t_provider as p 
                    ON r.Id_Provider > 0 AND p.Id_Provider = r.Id_Provider
                    WHERE r.Id_Room = ?",
                    [
                        $idRoom
                    ]
                );



                $finalResult[] = [          
              
                    'RoomPassenger_Status' => $roomPassenger->RoomPassenger_Status,
                    'Id_BookingExtraPassenger' => $roomPassenger->Id_BookingExtraPassenger,
                    'Id_Room' => $roomPassenger->Id_Room,

                    'Id_TypeRoom' => $result3[0]->Id_TypeRoom,
                    'Id_Provider' => $result3[0]->Id_Provider,
                    'TypeRoom_Name' => $result3[0]->TypeRoom_Name,
                    'TypeRoom_Abrv' => $result3[0]->TypeRoom_Abrv,
                    'TypeRoom_Capacity' => $result3[0]->TypeRoom_Capacity,
                    'TypeRoom_Status' => $result3[0]->TypeRoom_Status,
                    'Provider_BusinessName' => $result3[0]->Provider_BusinessName,
                    'Provider_TradeName' => $result3[0]->Provider_TradeName,
                    'Provider_NoDocument' => $result3[0]->Provider_NoDocument,
                    'Provider_Phone' => $result3[0]->Provider_Phone,                      

                    'passengers' => (array)$result2 

               
                ];
            }
        }

        return $finalResult;
    }

    
    private function whereIn($index, $ids)
    {
        return $index . ' IN (' . $this->getQuestionMarks($ids) . ')';
    }
    private function getQuestionMarks($ids)
    {
        return implode(',', array_fill(0, count($ids), '?'));
    }

    protected function analize($data)
    {
        return $data;
    }
}
