<?php

namespace App\Livewire;

use App\Models\Comment;
use Livewire\Component;
use Livewire\WithPagination;

class UserComments extends Component
{
    use WithPagination;

    public $model;

    public $commentSearch;

    public $order = 'created_at_desc';

    public function render()
    {
        $orderby = 'created_at';
        $orderdirection = 'desc';

        switch ($this->order) {
            case 'created_at_desc':
                $orderby = 'created_at';
                $orderdirection = 'desc';
                break;
            case 'created_at_asc':
                $orderby = 'created_at';
                $orderdirection = 'asc';
                break;
            default:
                $orderby = 'created_at';
                $orderdirection = 'desc';
        }

        $comments = Comment::where('user_id', $this->model->id)
            ->when($this->commentSearch != '', fn ($query) => $query->where('body', 'like', '%'.$this->commentSearch.'%'))
            ->orderBy($orderby, $orderdirection)
            ->paginate(6);

        return view('livewire.user-comments', [
            'comments' => $comments,
        ]);
    }
}
