<?php

namespace App\Models\Mensajeria;

use App\Models\BaseModel;
use App\Models\Customers\Customer;

class ParticipanteModel extends BaseModel
{
    protected $table = 'chat_participantes';
    protected $primaryKey = 'id';
    protected $returnType = 'App\Entities\Mensajeria\ParticipanteEntity';
    protected $useTimestamps = true;
    protected $useSoftDeletes = true;
    protected $allowedFields = [
        'conversacion_id',
        'usuario_id',
        'cliente_id',
        'email',
        'last_read',
    ];

    public function getPeopleInChat($conversacionId){

        $builder = $this->db
            ->table($this->table . " t1")
            ->where("t1.conversacion_id", $conversacionId);

        return $builder;
    }

    public function getChatsByUser($userId){

        $builder = $this->db
            ->table($this->table . " t1")
            ->select("t1.conversacion_id AS id, t2.asunto AS asunto")
            ->where("t1.usuario_id", $userId)
            ->join("chat_conversaciones t2", "t2.id = conversacion_id", "left")
            ->orderBy('t1.created_at', 'DESC')
            ->get()
            ->getResultObject();

        return $builder;
    }


    public function getCustomer()
    {
        //$customerModel = new Customer();
        //return $customerModel->find($this->attributes['customer_id']);
    }
}
