<?php

namespace App\Http\Controllers;

use App\Log;
use App\Services\LogService;
use Exception;
use Illuminate\Contracts\Foundation\Application;
use Illuminate\Contracts\Pagination\LengthAwarePaginator;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
use Illuminate\Routing\Redirector;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Gate;
use Illuminate\Support\Facades\Validator;
use function GuzzleHttp\Psr7\str;

class LogController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @param Request $request
     * @return Application|LengthAwarePaginator|\Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function index(Request $request)
    {
        if (!Gate::allows('日志-查询')) {
            return redirect(url('/'));
        }
        //没有查询条件，默认展示最近50条
        if (!$request->has('created_at_start') &&
            !$request->has('created_at_end') &&
            !$request->has('class') &&
            !$request->has('method') &&
            !$request->has('description') &&
            !$request->has("is_exception")
        ) {
            $logs = Log::query()
                ->with('user:id,name')
                ->orderBy('id', 'desc')
                ->paginate(50);
            return view('maintenance.log.index', ['logs' => $logs]);
        }
        //不传开始时间提示错误信息并返回
        if (!$request->has('created_at_start')) {
            session()->flash('warning', '请选择开始时间');
            return view('maintenance.log.index', ['logs' => null]);
        }
        $query = Log::query()->with('user:id,name');
        if ($request->has('class')) {
            $query->where('class', 'like', $request['class'] . '%');
        }
        if ($request->has('method')) {
            $query->where('method', 'like', $request['method'] . '%');
        }
        if ($request->has('description')) {
            $str = $request->description;
            $str = trim($str, '\\');
            $str = str_replace('\\', '\\\\', $str);
            $query->where('description', 'like', '%' . $str . '%');
        }
        if ($request->has('created_at_start')) {
            $query->where('created_at', '>=', $request->created_at_start." 00:00:00");
        }
        if ($request->has('created_at_end')) {
            $query->where('created_at', '<=', $request->created_at_end." 23:59:59");
        }
        if ($request->has("is_exception")){
            $query->where(function(Builder $query){
                $query->where("method","like","ERROR%")->orWhere("method","like","EXCEPTION%");
            });
        }
        $query->orderByDesc('id');
        $logs = $query->with('user:id,name')->paginate($request->paginate??50);
        return view('maintenance.log.index', ['logs' => $logs]);
    }

    /**
     * Display the specified resource.
     *
     * @param Log $log
     * @return Application|\Illuminate\Contracts\View\Factory|Response|\Illuminate\View\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');
    }
}
