<?php

namespace App\Http\Controllers\Api;

use App\Http\Controllers\Controller;
use App\Http\Resources\ExportResource;
use App\Models\Export;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;

class ExportController 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',
            'category' => 'exists:categories,id',
            'type' => 'in:internal,external'
        ], $this->localeValidationRule()));
        if($validator->fails()) {
            return response()->json($validator->errors());
        }
        
        $exports = Export::query()
            ->where('group_id', request('group'))
            ->where('locale', request('locale'))
            // ->when(request('title'), fn ($q) => $q->whereIn('id', $searchIds))
            ->when(request('category'), fn ($q) => $q->where('category_id', request('category')))
            ->when(request('unit'), fn ($q) => $q->where('unit', request('unit')))
            ->when(request('currency'), fn ($q) => $q->where('currency', request('currency')))
            ->when(request('payment'), fn ($q) => $q->where('payment', request('payment')))
            ->when(request('send'), fn ($q) => $q->where('send', request('send')))
            ->when(request('type'), fn ($q) => $q->where('type', request('type')));

        return response()->json([
            'meta_text' => settings('text')[request('locale')],
            'exports' => ExportResource::collection($exports->simplePaginate(request('per_page') ?? 7)),
        ], 200);
    }
}
