<?php

namespace App\Http\Controllers\Api;

use App\Http\Controllers\Controller;
use App\Http\Resources\ImportResource;
use App\Models\Import;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;

class ImportController extends ApiBaseController
{
    public function index(Request $request)
    {
        $validator = Validator::make($request->all(), array_merge([
            'per_page' => 'numeric|gt:0',
            'page' => 'numeric|gt:0',
            'group' => 'required|exists:groups,id'
        ], $this->localeValidationRule()));
        
        if ($validator->fails()) {
            return response()->json($validator->errors());
        }

        $imports = Import::query()
            ->where('group_id', request('group'))
            ->where('locale', request('locale'))
            // ->when(request('title'), fn ($q) => $q->whereIn('id', $searchIds))
            ->when(request('country'), fn ($q) => $q->where('country', request('country')))
            ->when(request('unit'), fn ($q) => $q->where('unit', request('unit')))
            ->when(request('currency'), fn ($q) => $q->where('currency', request('currency')));
            // ->when(request('registered_at'), fn ($q) => $q->whereDate('registered_at', request('registered_at')));

        return response()->json([
            'imports' => ImportResource::collection($imports->simplePaginate(request('per_page') ?? 7)),
        ], 200);
    }
}
