<?php

namespace App\Http\Controllers;

use App\Filters\LogFilters;
use App\Log;
use App\Services\LogService;
use Illuminate\Contracts\Foundation\Application;
use Illuminate\Contracts\Pagination\LengthAwarePaginator;
use Illuminate\Contracts\View\Factory;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
use Illuminate\Support\Facades\Gate;
use Illuminate\View\View;

class LogController extends Controller
{
    /**
     * @param Request $request
     * @param LogFilters $filters
     * @return Application|LengthAwarePaginator|Factory|View
     */
    public function index(Request $request, LogFilters $filters)
    {
        if (!Gate::allows('日志-查询')) {
            return redirect(url('/'));
        }
        $logs = Log::query()->filter($filters)->orderByDesc('id')->with('user:id,name')->paginate($request->input('paginate')??50);
        return view('maintenance.log.index', compact('logs'));
    }

    /**
     * Display the specified resource.
     *
     * @param Log $log
     * @return Application|Factory|Response|View
     */
    public function show(Log $log)
    {
        $log->loadMissing('user:id,name');
        return view('maintenance.log.show', ['log' => $log]);
    }

    public function syncRedisLogs()
    {
        LogService::syncRedisLogs();
        return redirect('maintenance/log');
    }
}
