<?php

namespace App\Http\Controllers\API\V1;

use App\Http\Controllers\Controller;
use App\Http\Resources\HashtagCollection;
use App\Models\Hashtag;
use App\Models\Post;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;

class APIHashtagController extends Controller
{
    private $limit = 24;

    public function index()
    {
        $hashtags = Hashtag::all();
        return new HashtagCollection($hashtags);
    }

    public function show($id)
    {
        $hashtag = Hashtag::findOrFail($id);
        return $hashtag;
    }

    public function getPosts(Request $request, $offset)
    {
        $hashtag = $request->hashtag;
        $posts = Post::with(['user', 'province', 'place', 'images'])
                    ->where('description', 'like', '%' . $hashtag . '%')
                    ->where('user_id', '!=', Auth::id())
                    ->orderBy('id', 'DESC')
                    ->paginate($this->limit, ['*'], 'page', $offset)->items();
        return $posts;
    }
}
