<?php

namespace App\Http\Controllers\Api;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Models\AccessLog;

class AccessLogController extends Controller
{

    public function store(Request $request)
    {

        $request->validate([
            'user_id' => 'required|integer',
            'face_result' => 'required|string',
            'voice_result' => 'required|string'
        ]);

        $face = $request->face_result;
        $voice = $request->voice_result;

        // LOGIKA METODE BIOMETRIC

        if ($face == 'success' && $voice == 'success') {

            $method = "Face + Voice";
            $status = "success";

        }
        elseif ($face == 'success' && $voice == 'failed') {

            $method = "Voice";
            $status = "failed";

        }
        elseif ($face == 'failed' && $voice == 'success') {

            $method = "Face";
            $status = "failed";

        }
        else {

            $method = "Face + Voice";
            $status = "failed";

        }

        AccessLog::create([

            'user_id' => $request->user_id,
            'method' => $method,
            'status' => strtolower($status),
            'device' => 'front-door',
            'access_time' => now()

        ]);

        return response()->json([
            "message" => "log saved"
        ]);

    }

}