<?php

namespace App\Http\Controllers;

use App\Siswa;
use Illuminate\Support\Facades\DB;
use Illuminate\Http\Request;

class AccessLogController extends Controller
{
    /**
     * Handle the incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function __invoke(Request $request)
    {
        /*
         * Count unique logs for the given source
         *
         */
        $logs = DB::table('access_logs')
            ->select(DB::raw('count(*) as num'))
            ->where('src', $request->src)
            ->groupBy('siswa_id')
            ->get()
            ->count();

        /*
         * Count the total number of siswa
         *
         */
        $resources = Siswa::count();

        return response()->json([
            'accessed' => $logs,
            'total' => $resources,
        ]);
    }
}
