<?php

namespace Modules\Task\Http\Controllers\Api;

use App\Api\Controllers\Controller;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Jason\Api\Api;
use Modules\Task\Http\Resources\TaskBaseResource;
use Modules\Task\Http\Resources\TaskResource;
use Modules\Task\Models\Task;

class TaskController extends Controller
{

    public function index(Request $request): JsonResponse
    {
        $user        = Api::user();
        $category_id = $request->category_id ?? '';

        $tasks = Task::query()
            ->when($category_id, function ($q) use ($category_id) {
                $q->where('category_id', $category_id);
            })
            ->Shown()
            ->when($user, function ($q) use ($user) {
                $q->ofUser($user);
            })
            ->get();

        return $this->success(TaskBaseResource::collection($tasks));
    }

    public function show(Task $task)
    {
        return $this->success(new TaskResource($task));
    }

}
