<?php

namespace App\Http\Controllers\api;

use App\Http\Controllers\Controller;
use App\Http\Requests\BookRequest;
use App\Models\Book;
use App\Models\Law;
use App\Models\Order;
use App\Traits\BaseApiResponse;

class BookController extends Controller
{
    use BaseApiResponse;

    public function index(BookRequest $request)
    {
        $validated = $request->validated();

        $perPage = $validatedData['per_page'] ?? 15;
        $page = $validatedData['page'] ?? 1;

        $books = Book::where('volum_id', $validated['volum_id'])->paginate($perPage, ['*'], 'page', $page);

        $books->getCollection()->transform(function ($section) {
            $section['is_locked'] = auth()->user()->isSubscriber() !== true ? true : Law::where('is_locked',$section['law_id'])->first()?->is_locked ?? false;
            
            unset($section['law_id']);
            unset($section['volum_id']);

            return $section;
        });

        return $this->success($books->items(), 'Success');
    }
}
