<?php

namespace App\Repositories;

use Illuminate\Http\Request;

use App\Models\Message;

/**
 * Class MessageRepository
 *
 * @package App\Repositories
 */
class MessageRepository
{
    /**
     * @var \App\Repositories\MessageRepository|null
     */
    protected static $instance = null;

    /**
     * MessageRepository constructor.
     *
     * @param \App\Models\Message|null $message
     */
    public function __construct(Message $message = null)
    {
        $this->message = $message;
    }

    /**
     * Instance
     *
     * @param \App\Models\Message $message
     * @return \App\Repositories\MessageRepository
     */
    public static function instance(Message $message = null)
    {
        if (is_null(static::$instance)) {
            static::$instance = new MessageRepository($message);
        }

        return static::$instance;
    }

    /**
     * Get a groups messages
     *
     * @param \Illuminate\Http\Request $request
     * @return \Illuminate\Contracts\Pagination\Paginator
     */
    public function getGroupMessages(Request $request)
    {
        $search = Message::query();
        $search->with(['user', 'group']);

        $search->where('groupoid', $request->get('groupid'));
        $search->orderBy('created_at', 'DESC');

        return $search->simplePaginate(25);
    }
}