<?php

namespace App\Http\Controllers\API;

use App\Http\Controllers\Controller;
use App\Http\Resources\UsefulInfoItemResource;
use App\Http\Resources\UsefulInfoResource;
use App\Models\UsefulInfo;

class UsefulInfoController extends Controller
{
    public function index()
    {
        $usefulInfos = UsefulInfo::orderBy('position')->get();

        return UsefulInfoResource::collection($usefulInfos);
    }

    public function items($id)
    {
        $usefulInfo = UsefulInfo::find($id);

        // chech if useful info not found
        if (!$usefulInfo) {
            return response()->json([
                'message' => 'Useful info not found'
            ], 404);
        }

        return UsefulInfoItemResource::collection($usefulInfo->items);
    }

    public function itemShow($id, $itemId)
    {
        $usefulInfo = UsefulInfo::find($id);

        return new UsefulInfoItemResource($usefulInfo->items->find($itemId));
    }
}
