<?php

namespace App\Http\Controllers\V2\Dashboard;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Facades\Otp\Otp;

class OtpController extends Controller
{
    public function getOtpToken(Request $request)
    {
        $request->validate([
            'phone_number' => 'required|digits:11|starts_with:09'
        ], [
            'starts_with' => ':attribute باید با :values شروع شود.'
        ], [
            'phone_number' => 'شماره تلفن'
        ]);

        Otp::getOtpToken($request, $request->ip(), $request->phone_number, "کد تایید تلفن همراه سامانه جامع راهداری (RMS) : \n");
        return response()->json([
            'message' => 'The code was sent.'
        ]);
    }

    public function verifyOtp(Request $request)
    {
        $request->validate([
            'phone_number' => 'required|digits:11',
            'verification_code' => 'required|digits:6'
        ]);

        $verified = Otp::verifyOtpToken($request->phone_number, $request->verification_code);
        if (!$verified) {
            return response()->json([
                'message' => 'کد تایید اشتباه است.'
            ], 400);
        }

        return response()->json([
            'token' => '',
            'message' => 'Code has been verified.'
        ]);
    }
}
