<?php

namespace App\Lib;

use App\Models\User;
use App\Notifications\ForgotPasswordNotification;
use App\Notifications\VerificationNotification;
use Illuminate\Support\Facades\Http;

class Sms
{
    protected string $authKey = '';
    public function __construct()
    {
        $this->authKey =  '431780AD1nBSSB4M675ef5b3P1';
    }

    public function mobileSmsVerification(User $user)
    {
        $templateId = '6717a74cd6fc05756b25e213';
        $phoneNo = $user->mobile;

        try {
            Http::withHeaders([
                'accept' => 'application/json',
                'authkey' => $this->authKey,
                'content-type' => 'application/json',
            ])->post('https://control.msg91.com/api/v5/flow', [
                'template_id' => $templateId,
                'short_url' => '0', // Change to '1' or '0' as needed
                'realTimeResponse' => '1', // Optional, adjust as needed
                'recipients' => [
                    [
                        'mobiles' => $phoneNo,
                        'verificationCode' => $user->ver_code,
                    ],
                ],
            ])->throw();
        } catch (\Throwable $th) {
            report($th);
        }

        $user->notify(new VerificationNotification());
    }
    public function forgetSmsToken(User $user, $token)
    {
        $templateId = '6717a74cd6fc05756b25e213';
        $phoneNo = $user->mobile;

        try {
            Http::withHeaders([
                'accept' => 'application/json',
                'authkey' => $this->authKey,
                'content-type' => 'application/json',
            ])->post('https://control.msg91.com/api/v5/flow', [
                'template_id' => $templateId,
                'short_url' => '0', // Change to '1' or '0' as needed
                'realTimeResponse' => '1', // Optional, adjust as needed
                'recipients' => [
                    [
                        'mobiles' => $phoneNo,
                        'verificationCode' => $token,
                    ],
                ],
            ])->throw();
        } catch (\Throwable $th) {
            report($th);
        }

        $user->notify(new ForgotPasswordNotification());
    }
}
