<?php

namespace App\Apps\Versionmgr\Controllers;

use App\Apps\Versionmgr\Models\SystemVersion;
use App\Http\Controllers\Controller;
use App\Http\Responses\ApiResponse;
use Illuminate\Http\Request;

class HistoryController extends Controller
{
    public function index()
    {
        return view('versionmgr::history.index');
    }

    public function list(Request $request)
    {
        $query = SystemVersion::with('operator')
            ->orderBy('id', 'desc');

        if ($request->filled('version')) {
            $query->where('version', $request->version);
        }

        if ($request->filled('upgrade_result')) {
            $query->where('upgrade_result', $request->upgrade_result);
        }

        $perPage = $request->input('per_page', 15);
        $paginator = $query->paginate($perPage);

        return response()->json(ApiResponse::paginate($paginator));
    }
}
