<?php

namespace App\Http\Controllers\api;

use App\Http\Controllers\Controller;
use App\Http\Requests\ChapterRequest;
use App\Models\Law;
use App\Models\Order;
use App\Models\Part;
use App\Traits\BaseApiResponse;
use Illuminate\Http\Request;

class PartController extends Controller
{
    use BaseApiResponse;

    public function index(ChapterRequest $request)
    {   
        $validated = $request->validated();
        
        $perPage = $request->input('per_page', 15);
        $page = $request->input('page', 1);

        $parts = Part::where('book_id' , $request->input('book_id'))->paginate($perPage, ['*'], 'page', $page);

        $parts->getCollection()->transform(function ($part) {
            unset($part['book_id']);
            $gate['is_locked'] = auth()->user()->isSubscriber()  !== false ? false : Law::where('is_locked', $part['law_id'])->first()?->is_locked;

            unset($gate['law_id']);

            return $gate;
        });

        
        return $this->success($parts->items());
    }
}
