<?php

namespace App\Facades;
use Illuminate\Support\Facades\Session;
use App\Models\Video_Call_Entry;
use Illuminate\Http\Request;
use App\Models\Organisation;
use App\Models\PeerToPeerVideoCall;
use App\Models\Education;
use App\Models\Individual;
use App\Models\Loby_Waiting_Clients;
use App\Models\MeetingLinks;
use App\Models\StudentClassInfo;
use App\Models\Video_Call_Entry_Scheduling;
use App\Models\Video_Call_Entry_Classroom;
use App\Models\Lobby_Page_Detail;

class GetVideoCallInfo
{

    public static function getReceiverVideoCallInformationPeerToPeerVideoCall($channel_id,$type)
    {
         $session_emailId = Session::get('emailId');
        // $caller_data = PeerToPeerVideoCall::where('Receiver_Email', '=', $session_emailId)->where('Is_Call_Accepted', '=', 1)->orderBy('created_at', 'desc') // Replace 'created_at' with your timestamp column
        //     ->first();
            $caller_data = PeerToPeerVideoCall::where('Channel_Id', '=', $channel_id)->first();
        if ($caller_data) {

            //   foreach ($caller_data as $roww) {
            $caller_profile_pic = $caller_data->Caller_Profile_Pic;
            $calling_profile_pic = $caller_data->Receiver_Profile_Pic;
            $caller_name = $caller_data->Caller_Full_Name;
            $calling_name = $caller_data->Receiver_Full_Name;
            $channel_ID = $caller_data->Channel_Id;
            $caller_email = $caller_data->Caller_Email;
            $receiver_email = $caller_data->Receiver_Email;

            if($type=='caller'){
            $acountType = 'other';
            $organisation = Organisation::where('email_id', $session_emailId)->get();
            if ($organisation) {
                foreach ($organisation as $row) {

                        $caller_name = $row["entreprise_name"];


                    $acountType = 'org';

                }

            }
            $organisation = Organisation::where('email_id', $receiver_email)->get();
            if ($organisation) {
                foreach ($organisation as $row) {

                        $calling_name = $row["entreprise_name"];


                    $acountType = 'org';

                }

            }

        }else{
            $acountType = 'other';
            $organisation = Organisation::where('email_id', $session_emailId)->get();
            if ($organisation) {
                foreach ($organisation as $row) {

                        $calling_name = $row["entreprise_name"];


                    $acountType = 'org';

                }

            }$organisation = Organisation::where('email_id', $caller_email)->get();
            if ($organisation) {
                foreach ($organisation as $row) {

                        $caller_name = $row["entreprise_name"];


                    $acountType = 'org';

                }

            }
        }


            // Video_Call_Entry::where('channel_id', $channel_ID)->delete();
            $caller_name = ucfirst($caller_name);
            $calling_name = ucfirst($calling_name);
            $videoCallInfo = [
                'receiver' => 'true',
                'channelID' => $channel_ID,
                'acountType' => $acountType,
                'calling_person_name' => $calling_name,
                'caller_name' => $caller_name,
                'caller_picture' =>$caller_profile_pic ,
                'calling_profile_pic' => $calling_profile_pic,
                'caller_person_email' => $caller_email,
                'calling_person_email' => $receiver_email,

            ];
        }
        return $videoCallInfo;
    }




    public static function getReceiverVideoCallInformationNormalMeetingVideoCall($channel_id)
    {
        $session_emailId = Session::get('emailId');
        $receiver_data = MeetingLinks::where('participant_email', '=', $session_emailId)->where('generated_link', '=',$channel_id)->orderBy('created_at', 'desc') // Replace 'created_at' with your timestamp column
            ->first();
        if ($receiver_data) {
            //   foreach ($caller_data as $roww) {
            $caller_profile_pic = $receiver_data->caller_profile_pic;
            $calling_profile_pic = $receiver_data->participant_profile_pic;
            $caller_name = $receiver_data->caller_name;
            $calling_name = $receiver_data->participant_name;
            $caller_person_email = $receiver_data->email_id;
            $user_id=$receiver_data->user_id ;
            $acountType = 'other';
            $organisation = Organisation::where('email_id', $session_emailId)->get();
            if ($organisation) {
                foreach ($organisation as $row) {
                    $caller_name = $row["entreprise_name"];
                    $acountType = 'org';

                }
            }

            $is_text_ticker="no";
            $text_ticker_content=NULL;
            $is_text_ticker_video_call="no";
            $text_ticker_content_video_call=NULL;
            $is_chat_pannel='yes';
            $lobby_profile_pic="no";
            $lobby_background_pic="no";
            $can_see_waiting_people="no";
            $lobby_page_info = Lobby_Page_Detail::where('channel_id', '=', $channel_id)->first();
               if($lobby_page_info){
                $is_text_ticker= $lobby_page_info->is_text_ticker;
                $text_ticker_content=$lobby_page_info->text_ticker_content;
                $is_chat_pannel=$lobby_page_info->is_chat_pannel;

                $lobby_profile_pic="lobbyroomdata/".$caller_person_email.'/'. $lobby_page_info->lobby_profile_pic;
                $lobby_background_pic="lobbyroomdata/".$caller_person_email.'/'. $lobby_page_info->lobby_background_pic;
                $is_text_ticker_video_call= $lobby_page_info->is_ticker_for_video_page;
                $text_ticker_content_video_call= $lobby_page_info->ticker_text_for_video_page ;

                $can_see_waiting_people= $lobby_page_info->can_see_waiting_people ;
            }
            // Loby_Waiting_Clients::create([
            //     'user_email' => $session_emailId,
            //     'channel_id' =>$channel_id,
            // ]);
            // Video_Call_Entry::where('channel_id', $channel_ID)->delete();
            $caller_name = ucfirst($caller_name);
            $calling_name = ucfirst($calling_name);
            $videoCallInfo = [
                'receiver' => 'true',
                'channelID' => $channel_id,
                'acountType' => $acountType,
                'calling_person_name' => $caller_name,
                'caller_name' => $calling_name,
                'caller_picture' => $calling_profile_pic,
                'calling_profile_pic' => $caller_profile_pic,
                'calling_person_email' => $caller_person_email,
                'caller_person_email' => $caller_person_email,
                'user_id' =>  $user_id,
                'is_text_ticker' =>  $is_text_ticker,
                'text_ticker_content' =>  $text_ticker_content,
                'is_chat_pannel' =>  $is_chat_pannel,
                'lobby_profile_pic'=>$lobby_profile_pic,
                'lobby_background_pic'=>$lobby_background_pic,
                'is_text_ticker_video_call'=>  $is_text_ticker_video_call,
                'text_ticker_content_video_call'=>  $text_ticker_content_video_call,
                'can_see_waiting_people' =>$can_see_waiting_people,

            ];
        }
        return $videoCallInfo;
    }

    public static function getVideoCallInformation()
    {
        $session_emailId = Session::get('emailId');
        $caller_data = Video_Call_Entry::where('calling_email', '=', $session_emailId)->where('is_call_accepted', '=', 1)->orderBy('created_at', 'desc') // Replace 'created_at' with your timestamp column
            ->first();
        if ($caller_data) {
            //   foreach ($caller_data as $roww) {
            $caller_profile_pic = $caller_data->caller_email_profile_image;
            $calling_profile_pic = $caller_data->calling_email_profile_image;
            $caller_name = $caller_data->caller_full_name;
            $calling_name = $caller_data->calling_full_name;
            $channel_ID = $caller_data->channel_id;
            $caller_type = $caller_data->call_type;
            $caller_person_email = $caller_data->caller_email;
            // $caller_profile_pic = $roww["caller_email_profile_image"];
            //  $calling_profile_pic = $roww["calling_email_profile_image"];

            //  $caller_name = $roww["caller_full_name"];
            // $calling_name = $roww["calling_full_name"];
            //  $channel_ID = $roww["channel_id"];
            //   $caller_type = $roww["call_type"];
            //     $caller_person_email = $roww["caller_email"];
            //   }

            $acountType = 'other';
            $organisation = Organisation::where('email_id', $session_emailId)->get();

            if ($organisation) {
                foreach ($organisation as $row) {
                    $caller_name = $row["entreprise_name"];
                    $acountType = 'org';

                }

            }
            // Video_Call_Entry::where('channel_id', $channel_ID)->delete();
            $caller_name = ucfirst($caller_name);
            $calling_name = ucfirst($calling_name);
            $videoCallInfo = [
                'receiver' => 'true',
                'channelID' => $channel_ID,
                'acountType' => $acountType,
                'calling_person_name' => $caller_name,
                'caller_name' => $calling_name,
                'caller_picture' => $calling_profile_pic,
                'calling_profile_pic' => $caller_profile_pic,
                'calling_person_email' => $caller_person_email,
                'caller_person_email' => $caller_person_email,
            ];
        }
        return $videoCallInfo;
    }




    public static function getChannelPersonalInfoPeerToPeerVideoCall($channelName)
    {
        $returnArray = [];
        $caller_email = "";
        $caller_full_name = "";
        $email_id=Session::get('emailId');
        $caller_data = PeerToPeerVideoCall::where('Channel_Id', '=', $channelName)->get();
        if (count($caller_data) > 0) {
            foreach ($caller_data as $roww) {
                $calling_email = $roww["Receiver_Email"];
                $calling_name = $roww["Receiver_Full_Name"];

                $caller_email = $roww["Caller_Email"];
                $caller_full_name = $roww["Caller_Full_Name"];
                $id = $roww["user_id"];

                $personInfo = [
                    'id' => $id,
                    'calling_email' => $calling_email,
                    'calling_name' => $calling_name,
                ];
                $returnArray[] = $personInfo;
                $personInfo = [
                    'id' => 1,
                    'calling_email' => $caller_email,
                    'calling_name' => $caller_full_name,
                ];
                $returnArray[] = $personInfo;
            }
        }
        // Loby_Waiting_Clients::create([
        //     'user_email' => $email_id,
        //     'channel_id' =>$channelName,
        // ]);


        // $personInfo = [
        //     'id' => 1,
        //     'calling_email' => $caller_email,
        //     'calling_name' => $caller_full_name,
        // ];
       // $returnArray[] = $personInfo;
        return $returnArray;
    }













    public static function getChannelPersonalInfo($channelName)
    {
        $returnArray = [];
        $caller_email = "";
        $caller_full_name = "";
        $caller_data = Video_Call_Entry::where('channel_id', '=', $channelName)->get();
        if (count($caller_data) > 0) {
            foreach ($caller_data as $roww) {
                $calling_email = $roww["calling_email"];
                $calling_name = $roww["calling_full_name"];

                $caller_email = $roww["caller_email"];
                $caller_full_name = $roww["caller_full_name"];
                $id = $roww["id"];
                $personInfo = [
                    'id' => $id,
                    'calling_email' => $calling_email,
                    'calling_name' => $calling_name,
                ];
                $returnArray[] = $personInfo;
            }
        }

        $personInfo = [
            'id' => 1,
            'calling_email' => $caller_email,
            'calling_name' => $caller_full_name,
        ];
        $returnArray[] = $personInfo;
        return $returnArray;
    }
}
?>