<?php

namespace App\Http\Controllers\Admin\Report;

use App\Http\Controllers\Controller;
use App\Models\Report\ReportExport;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;

class ReportExportController extends Controller
{
    public function index(Request $request): JsonResponse
    {
        $query = ReportExport::query()->with('operator');
        $query->when($request->type, fn($q, $v) => $q->where('type', $v));
        $query->when($request->filled('status'), fn($q) => $q->where('status', $request->status));

        return $this->paginate($query->latest()->paginate($request->input('per_page', 15)));
    }

    public function store(Request $request): JsonResponse
    {
        $validated = $request->validate([
            'name' => 'required|string|max:255',
            'type' => 'required|string|max:50',
            'params' => 'nullable|array',
        ]);
        $validated['operator_id'] = auth()->id();
        $validated['status'] = 0;

        $export = ReportExport::create($validated);
        return $this->success($export);
    }
}
