<?php

namespace App\Http\Main\Engineers\Messages;

use App\Models\Message;
use App\Models\Team;
use App\Models\Conversation;
use App\Models\Notification;
use Illuminate\Support\Str;
use App\Models\User;
use Livewire\Component;

class SendMessage extends Component
{


    public $users;
    public $to;
    public $message;
    public $subject;
    public $curr_id;
    public $q;
    public $nameno;

    public $queryString = ['q'];


    public function mount()
    {



        $this->users = User::all()->except(auth()->user()->id);
    }
    public function updatedTo()
    {
        if ($this->to != null) {
            $users = User::where('phone', $this->to)->get();
            if ($users->count() > 0) {
                $this->nameno = $users->first()->first_name . ' ' . $users->first()->last_name;
            } else {
                $this->nameno = '<span class="text-danger">User does not exist</span>';
            }
        }
    }


    public function curr($id)
    {

        $this->curr_id = $id;
    }

    public function send()
    {
        if ($this->to != null) {
            $users = User::where('phone', $this->to)->get();
            if ($users->count() > 0) {
                $users = User::where('phone', $this->to)->get();
                $to = $users->first()->id;
                $former = Message::where('from', $to)->where('to', auth()->user()->id)->get();


                $me = new Message();
                $me->to = $to;
                $me->message = $this->message;
                $me->from = auth()->user()->id;
                if (count($former) != 0) {
                    $me->conversation_id = $former->first()->conversation_id;
                } else {
                    $former = Message::where('to', $to)->where('from', auth()->user()->id)->get();
                    if (count($former) != 0) {
                        $me->conversation_id = $former->first()->conversation_id;
                    } else {
                        $conversation =  new Conversation();
                        $conversation->save();
                        $me->conversation_id = $conversation->id;
                    }
                }
                $me->save();

                $arr = [
                    'from' => auth()->user()->id,
                    'user_id' => $to,
                    'seen' => false,
                    'message' => Str::words($me->message, 6, '...'),
                    'link' => '/messages',
                    'name' => 'Message from ' . auth()->user()->first_name . ' ' . auth()->user()->last_name
                ];


                Notification::new($arr);
                $this->emit('sent');
            }
        }
    }

    public function render()
    {



        $messages = Message::orderBy('id', 'desc')->orWhere('to', auth()->id())->orWhere('from', auth()->id());
        if ($this->q != null) {


            $userssearch =   User::orWhere('first_name', 'LIKE', '%' . $this->q . '%')->orWhere('last_name', 'LIKE', '%' . $this->q . '%')->get();
            foreach ($userssearch as $user) {
                $messages = $messages->orWhere('to', $user->id)->orWhere('from', $user->id);
            }
            $messages = $messages->where('message', 'LIKE', '%' . $this->q . '%');
        }
        $messages = $messages->get()->groupBy('conversation_id');
        $current = Message::where('conversation_id', $this->curr_id)->orderBy('id', 'asc')->get();
        if (auth()->user()->currentTeam->id != 1 && auth()->user()->currentTeam->id != 2) {
            return view('main.engineers.messages.send-message', [
                'current' => $current,
                'messages' => $messages
            ])->layout('layouts.app-frontdesk');
        } else {
            return view('main.engineers.messages.send-message', [
                'current' => $current,
                'messages' => $messages
            ])->layout('layouts.app-engineers');
        }
    }
}
