<?php

namespace App\Modules\OTPVerification\Repositories;

use App\Modules\OTPVerification\Models\OTPVerification;
use App\Modules\SMS\Repositories\SMSRepository;

class OTPVerificationRepository
{
    /**
     * Send a sms verification
     */
    public static function sendSMSVerification(string|int $phone_number): ?OTPVerification
    {
        /* for apple testing */
        $phone_code = ($phone_number == '61126667') ? 77777 : rand(10000, 99999);

        $verification = OTPVerification::where(['username' => $phone_number])->first();
        $verification
            ? $verification->update(['code' => $phone_code])
            : OTPVerification::create(['username' => $phone_number, 'code' => $phone_code]);

        SMSRepository::sendSMS($phone_number, 'Tassyklaýyş belgi: '.$phone_code);

        return $verification;
    }
}
