<?php

namespace App\Http\Controllers\Api;

use App\Http\Controllers\Controller;
use App\Models\PreQuestion;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;

class PreQuestionApiController extends Controller
{
    public function index(Request $request): JsonResponse
    {
        $query = PreQuestion::query()->orderBy('id', 'asc');

        if ($request->filled('import_id')) {
            $query->where('import_id', (int) $request->input('import_id'));
        }

        $perPage = (int) $request->input('per_page', 200);
        $perPage = max(1, min($perPage, 1000));

        $paginator = $query->paginate($perPage);

        return response()->json([
            'success' => true,
            'data' => $paginator->items(),
            'meta' => [
                'current_page' => $paginator->currentPage(),
                'per_page' => $paginator->perPage(),
                'total' => $paginator->total(),
                'last_page' => $paginator->lastPage(),
            ],
        ]);
    }
}

