<?php

class auth {
    public function akses_nomor_kirim_otp($nomor)
    {
        // Logic
        $kode = rand(1000,9999);
        $user = $this->userRepo->findDataPhone($nomor);
        $this->userRepo->updateOTP($nomor, $kode);
        $this->notifService->kirimNotifWa($user->phone_code.ltrim($user->phone_number, '0'), $this->formatPesanAksesWa($kode));
        $this->successSendOTP($nomor);

        return response()->json([
            'status'  => 'success',
            'message' => 'OTP sent successfully',
        ]);
    }

    public function akses_nomor_kirim_otp_sesi_waktu_mulai($nomor)
    {
        // Get current time
        $currentTime = Carbon::now()->timestamp;

        // Set session
        Session::put($nomor.'startTime', $currentTime);
        Session::put($nomor.'remainingTime', 60);

        return response()->json([
            'startTime'     => Session::get($nomor.'startTime'),
            'remainingTime' => Session::get($nomor.'remainingTime')
        ]);
    }

    public function akses_nomor_kirim_otp_sesi_waktu_batas($nomor)
    {
        // Get remaining time from session
        $startTime = Session::get($nomor.'startTime', 0);
        $remainingTime = Session::get($nomor.'remainingTime', 0);

        // Calculate remaining time
        $elapsedTime = time() - $startTime;
        $remainingTime = max(0, $remainingTime - $elapsedTime);

        return response()->json(['remainingTime' => $remainingTime]);
    }
}