<?php

namespace App\Http\Controllers\Chat;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;

use App\Models\Company;
use App\Models\ChatGroup;
use App\Models\Message;
use Illuminate\Support\Facades\Validator;

class ChatController extends Controller
{
    
    public function getChat(Request $request){
        $chat_id = $request->chat_id;
        
        return ChatGroup::find($chat_id);
    }
    
    
     public function getMessages(Request $request){
        $chat_id = $request->chat_id;
        
        return Message::where('chat_id',$chat_id)->get();
    }
    
    
    /**
     * Start a new chat by creating a chat group.
     *
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function startChat(Request $request)
    {
        $validator = Validator::make($request->all(), [
            'company_id' => 'required|integer|exists:companies,id',
            //'user_id' => 'required|integer|exists:users,id',
            'customer_id' => 'required|string|max:255',
            'name' => 'required|string|max:255',
            'email' => 'required|email|max:255',
            'subject' => 'required|string|max:1000',
            //'status' => 'required|string|max:255'
        ]);

        if ($validator->fails()) {
            return response()->json(['errors' => $validator->errors()], 422);
        }
        
        $company_id = $request->company_id;
        
        $user_id = $this->select_user($company_id);
        
        if($user_id){
           
        $data = [
            'company_id' => $company_id,
            'user_id' => $user_id,
            'customer_id' => $request->customer_id,
            'name' => $request->name,
            'email' => $request->email,
            'subject' => $request->subject,
            'status' => 'open',
            
            ];

        $chatGroup = ChatGroup::create($data);

        return response()->json(['chat' => $chatGroup], 200);
        
        }else{
            return response()->json(['message' => 'user not found'], 400);
        }
    }
    
    
    
    public function sendMessage(Request $request)
    {
        $validator = Validator::make($request->all(), [
            'chat_id' => 'required|integer|exists:chat_group,id',
            'from' => 'required|string|max:255',
            //'to' => 'required|string|max:255',
            'message' => 'required|string',
            'type' => 'required|string|max:255'
        ]);

        if ($validator->fails()) {
            return response()->json(['errors' => $validator->errors()], 422);
        }
        
         $data = [
            'chat_id' => $request->chat_id,
           
            'from' => $request->from,
            'to' => $request->from == 'user'?'company':'user',
            'message' => $request->message,
            'type' => $request->type,
          
            
            ];
      

        $message = Message::create($data);

        return response()->json(['message' => 'Message sent successfully', 'data' => $message], 200);
    }
    
    public function select_user($company_id){
        $companyUsers = get_company_users($company_id);
        
        $selected = false;
        
        foreach($companyUsers as $user){
            $access = json_decode($user->access);
            
            if(in_array('chat',$access)){
                $selected = $user->user_id;
            }
        }
        
        return $selected;
        
    }
    
      public function getChatGroupsByCompany(Request $request)
    {
        $companyId = getSelectedCompany();
    
        $chatGroups = ChatGroup::where('company_id', $companyId)->get();

        return response()->json($chatGroups);
    }
}
