<?php

namespace App\Http\Controllers\api;

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

class GateController extends Controller
{
    use BaseApiResponse;

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

        $gates = Gate::where('book_id', $validated['book_id'])
            ->paginate($validated['per_page'], ['*'], 'page', $validated['page']);

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

            unset($gate['law_id']);

            return $gate;
        });



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