<?php

namespace App\Services\Conversation;

use App\Http\Messages\ExeptionMessages;
use App\Repositories\Conversation\ConversationRepositoryInterface;
use Exception;
use Illuminate\Support\Facades\DB;
use App\Repositories\Customer\CustomerRepositoryInterface;

class ConversationService
{
    protected $conversationRepository;

    public function __construct(ConversationRepositoryInterface $conversationRepositoryInterface )
    {
        $this->conversationRepository = $conversationRepositoryInterface;
    }
    public function createConversation($data){
        $conversation = $this->conversationRepository->findByPhone($data['phone']);
        if ($conversation) {
            return $conversation;
        }
        return $this->conversationRepository->create($data)->refresh();
    }
    public function getConversationByPhone($phone){
        return $this->conversationRepository->findByPhone($phone);
    }
    public function checkCustomerRegister($conversation_id){
        return $this->conversationRepository->activatedCustomer($conversation_id);
    }
    public function disableConversacionCustomer($conversation_id){
        return $this->conversationRepository->disabledConversation($conversation_id);
    }
    public function editLastMessage($conversation_id){
        return $this->conversationRepository->updateLastMessage($conversation_id);
    }

    public function listConversationPaginate($requestPaginate){
        $query = $this->conversationRepository->query();
        if ($requestPaginate->filled('search')) {
            $query->where('phone', 'LIKE', '%' . $requestPaginate->query('search') . '%');
        }
        if ($requestPaginate->filled('ia_status')) {
            $query->where('ia_status', $requestPaginate->query('ia_status'));
        }
        $itemsPerPage = $requestPaginate->input('itemsPerPage', 10);
        $page = $requestPaginate->input('page', 1);
        return $query->orderBy('last_message', 'asc')->paginate($itemsPerPage, ['*'], 'page', $page);
    }
    public function updateStatusIA($id,$status){
        return $this->conversationRepository->updateStatusIA($id,$status);
    }

    public function updateConversationThread($phone, $threadId) {
        return  $this->conversationRepository->updateThreadPhone($phone, $threadId);
    }
}
