<?php

declare(strict_types=1);

namespace App\Http\Controllers;

use App\Http\Resources\PostResource;
use App\Models\Post;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Gate;

class PostController extends Controller
{
    protected $model;

    public function index(Request $request)
    {
        $query = Post::query()->forApi();

        if ($request->has('postTitle') && ! empty($request->postTitle)) {
            $query->where('title', 'like', '%' . $request->postTitle . '%');
        }

        if ($request->has('categoryUrl') && ! empty($request->categoryUrl)) {
            $query->whereHas('categories', function ($q) use ($request): void {
                $q->where('url', $request->categoryUrl);
            });
        }

        if ($request->has('tags') && ! empty($request->tags)) {
            $tags = json_decode($request->tags);
            $query->whereHas('tags', function ($q) use ($tags): void {
                $q->whereIn('slug', $tags);
            });
        }

        if ($request->has('sortType') && ! empty($request->sortType)) {
            switch ($request->sortType) {
                case 'latest':
                    $query->orderBy('created_at', 'desc');
                    break;
                case 'oldest':
                    $query->orderBy('created_at', 'asc');
                    break;
                case 'popular':
                    $query->reorder()->orderByDesc('views');
                    break;
            }
        }

        $query->where('status', 13);

        return $request->noPaginate ? PostResource::collection($query->get()) : PostResource::collection($query->paginate());
    }

    public function store(Request $request)
    {
        $item = new Post;
        $data = $request->all();
        Gate::authorize('create', Post::class);

        $data['created_by'] = Auth::user()->id;
        $post = $item->create($data);

        if (isset($request->categories)) {
            $categories = json_decode($request->categories, true);
            $post->categories()->syncWithPivotValues($categories, ['model_type' => 'Post::class']);
        }

        return new PostResource($post);
    }

    public function show(Post $post)
    {
        $post->load(['author', 'categories', 'tags']);
        $post->increment('views');
        $post->refresh();

        return new PostResource($post);
    }

    public function update(Request $request, Post $post)
    {
        Gate::authorize('update', $post);
        $data = $request->all();
        $data['updated_by'] = Auth::user()->id;
        $post->update($data);

        if (isset($request->categories)) {
            $categories = json_decode($request->categories, true);
            $post->categories()->syncWithPivotValues($categories, ['model_type' => 'Post::class']);
        }

        return new PostResource($post);
    }

    public function destroy(Post $post)
    {
        Gate::authorize('delete', $post);

        return $post->delete();
    }
}
