<?php

        include('../../config/config.php');
        date_default_timezone_set('Africa/Lagos');
        
        
      include ('../messaging/whatsapp/index.php');
            
            
            
               // Set CORS headers to allow requests from any origin (*)
        header("Access-Control-Allow-Origin: *");
        
        // Allow specific HTTP methods
        header("Access-Control-Allow-Methods: GET, POST, PUT, DELETE");
        
        // Allow specific HTTP headers
        header("Access-Control-Allow-Headers: Content-Type, Authorization");
        
        // Allow cookies to be included in the request (if needed)
        header("Access-Control-Allow-Credentials:true");
        
        // Retrieve the raw POST data
        
        
        $userID =  mysqli_real_escape_string($link, trim(isset($_POST['UserID']) ? $_POST['UserID'] : ''));
        $UserType =  mysqli_real_escape_string($link, trim(isset($_POST['UserType']) ? $_POST['UserType'] : ''));
        
       
        
      
        $currentDateTime = date('Y-m-d H:i:s');
        
        
        
          $sql = ("SELECT * FROM `Users` WHERE UserID='$userID'");
          $result = mysqli_query($link, $sql);
          $row = mysqli_fetch_assoc($result);
          $row_cnt = mysqli_num_rows($result);
    
          if ($row_cnt > 0) 
          {
              
                   
                   $UserType_new = trim($row['UserType']);
                   
                   
                   if($UserType_new == 'pastor')
                   {
                       
                       
                       
                            $pros_get_tribe = mysqli_query($link,"SELECT t.`TribeID`, t.`TribeLeaderID`, t.`TribeTitle`, t.`TribeDes`, t.`TribeImage`, t.`DeleteStatus`, COUNT(u.`UserID`) AS num_users
                                        FROM `tribe` t
                                        LEFT JOIN `Users` u ON t.`TribeID` = u.`TribeAssigenedID` AND u.`DeleteStatus` = 0
                                        WHERE t.`DeleteStatus` = 0
                                        GROUP BY t.`TribeID`
                                        ORDER BY num_users DESC
                                        LIMIT 5;

                                ");
                       
                   }else
                   {
                       
                       
                          $pros_get_tribe = mysqli_query($link,"SELECT t.`TribeID`, t.`TribeLeaderID`, t.`TribeTitle`, t.`TribeDes`, t.`TribeImage`, t.`DeleteStatus`, COUNT(u.`UserID`) AS num_users
                                        FROM `tribe` t
                                        LEFT JOIN `Users` u ON t.`TribeID` = u.`TribeAssigenedID` AND u.`DeleteStatus` = 0
                                        WHERE t.`DeleteStatus` = 0  AND t.`TribeLeaderID`='$userID'
                                        GROUP BY t.`TribeID`
                                        ORDER BY num_users DESC
                                        LIMIT 5;

                                ");
                       
                   }   
                       
                           
                                                        
                                                             
                                
                                  
                                  $pros_get_tribe_cnt = mysqli_num_rows($pros_get_tribe);
                                  
                                  if($pros_get_tribe_cnt > 0)
                                  {
                                      
                                      
                                      
                                      $tibescontent = [];
                                      
                                       while($pros_get_tribe_cnt_row = mysqli_fetch_assoc($pros_get_tribe))
                                       {
                                           
                                           
                                           
                                              $TribeID =  $pros_get_tribe_cnt_row['TribeID'];
                                              $TribeLeaderID =  $pros_get_tribe_cnt_row['TribeLeaderID'];
                                              $TribeTitle =  $pros_get_tribe_cnt_row['TribeTitle'];
                                              $TribeDes =  $pros_get_tribe_cnt_row['TribeDes'];
                                              $TribeImage =  $pros_get_tribe_cnt_row['TribeImage'];
                                              $num_users =  $pros_get_tribe_cnt_row['num_users'];
                                              
                                             $pros_get_leaders = mysqli_query($link, "SELECT * FROM `Users` WHERE  `UserID`='$TribeLeaderID'");
                                             $pros_get_leaders_cnt = mysqli_num_rows($pros_get_leaders);
                                             $pros_get_leaders_cnt_row = mysqli_fetch_assoc($pros_get_leaders);
                                             
                                            
                                             if($pros_get_leaders_cnt > 0)
                                             {
                                                  
                                                 
                                                     $UserFirstName = $pros_get_leaders_cnt_row['UserFirstName'];
                                                     $UserLastName = $pros_get_leaders_cnt_row['UserLastName'];
                                                     
                                                     $Full_Name = $UserFirstName.' '.$UserLastName;

                                             }else
                                             {
                                                      $Full_Name = '';
                                                 
                                             }
                                             
                                              
                                              $tibescontent[] = array(
                                                'TribeID' => $TribeID,
                                                'Tribe_leader_id' => $TribeLeaderID,
                                                'tribe_leader_full_name'=> $Full_Name,
                                                'tribe_title'=> $TribeTitle,
                                                'tribe_des'=>$TribeDes,
                                                'tribe_image'=>$TribeImage,
                                                'num_members'=>$num_users
                                                                                        
                                            );
                                           
                                           
                                           
                                       }
                                       
                                       
                                       
                                               $response = array(
                                                "requestSuccessful" => true, // Assuming the request was successful
                                                "responseMessage" => "success",
                                                "responseDescription" => "Tribe record found",
                                                 "responseBody" => $tibescontent
                                                
                                              );
                                                           
                                     
                                      
                                  }else
                                  {
                                      
                                      
                                      
                                      
                                       $response = array(
                                            "requestSuccessful" => true, // Assuming the request was successful
                                            "responseMessage" => "failed",
                                            "responseDescription" => "Tribes record not found.",
                                             "responseBody" => array(
                                                'TribeID' => 'NULL',
                                                'Tribe_leader_id' => 'NULL',
                                                'tribe_leader_full_name'=> 'NULL',
                                                'tribe_title'=> 'NULL',
                                                'tribe_des'=> 'NULL',
                                                'tribe_image'=>'NULL',
                                                'num_members'=>'NULL'
                                                                                                
                                            )
                                            
                                          );
                                      
                                      
                                      
                                            
                                      
                                  }
                                  
                                  
                             
                   
                   
              
                     
          }
          else
          {
              
             
                   $response = array(
                        "requestSuccessful" => true, // Assuming the request was successful
                        "responseMessage" => "failed",
                        "responseDescription" => "User not found",
                         "responseBody" => array(
                                'TribeID' => 'NULL',
                                'Tribe_leader_id' => 'NULL',
                                'tribe_leader_full_name'=> 'NULL',
                                'tribe_title'=> 'NULL',
                                'tribe_des'=> 'NULL',
                                'tribe_image'=>'NULL',
                                'num_members'=>'NULL'
                                                                                
                            )
                        
                      );
              
          }
      
      
         $json_response = json_encode($response);
    
        // // Output the JSON response
        echo $json_response;
       

?>