<?php

namespace App\Http\Controllers\API;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Facades\File;
use Illuminate\Support\Str;
use Carbon\Carbon;
use getID3;
use DB;

use Kreait\Firebase\Factory;
use Kreait\Firebase\Messaging\CloudMessage;

class ChatliveradiotvController extends Controller
{
    
    public function chat_send_message_radio(Request $request)
    {
        $userid=$request->get('id_user');
        $message=$request->get('message');
        
        DB::table('ebm_chat_messages_radio_live')->insert(
            [
                'id_user' => $userid,
                'message' => $message,
                'created_at' => DB::raw('NOW()'), 
                'updated_at' => DB::raw('NOW()')
            ]
        );
        
        return response()->json([
            'success' => 'true'
        ]);
    }
    
    public function chat_messages_radio()
    {
        $allradioliveData = [];
        
        $allradiolive= DB::select('select ebm_chat_messages_radio_live.* from ebm_chat_messages_radio_live where DATE(created_at)=? order by id desc',[date('Y-m-d')]);
        foreach ($allradiolive as $rowsallradiolive)
        {
            $radioliveId=$rowsallradiolive->id;
            
            $allradioliveData[$radioliveId]=[
                'id' =>$rowsallradiolive->id,  
                'userId' => $rowsallradiolive->id_user,
                'message' => $rowsallradiolive->message,
                'created_at' => $rowsallradiolive->created_at,
                'avatar' =>[],
            ];
            
            $avatarforums= DB::select('select * from ebm_users where id_user=?',[$rowsallradiolive->id_user]); 
            foreach ($avatarforums as $rowavatarforums)
            {
                $allradioliveData[$radioliveId]['avatar'][] = [
                    'urlavatar' => $rowavatarforums->avatar,
                    'username' => $rowavatarforums->nom.' '.$rowavatarforums->prenom,
                    // Ajoutez d'autres attributs d'image ici
                ];
            }
        }
        
        $resultradioliveData = [
            'status' => 'true',
            'alldataradiolive' => array_values($allradioliveData),  
        ];
        return response()->json($resultradioliveData);
    }
    
    
    
}
