<?php

namespace Themes\kbgolf\pro\app\Http\Controllers\Auth;

use App\Http\Traits\FetchesData\BasicTrait;
use App\Services\CallApiService;
use App\Services\Msg\MailTemplateService;
use App\ThirdPartyApi\Interfaces\SmsInterface;

class MemberUnlockController
{
    private $callApiService;
    private $smsService;
    private $mailTemplateService;

    use BasicTrait;

    public function __construct(
        CallApiService $callApiService,
        SmsInterface $smsService,
        MailTemplateService $mailTemplateService)
    {
        $this->callApiService = $callApiService;
        $this->smsService = $smsService;
        $this->mailTemplateService = $mailTemplateService;
    }

    public function index()
    {
        $email = session('email', '');
//        $email = session('email');
//        if (empty($email)) {
//            notify()->error('잘 못 된 접근 입니다', 'Error', 'bottomRight');
//            return redirect()->back();
//        }

        return view('views.auth.member-unlock', compact('email'));
    }

    public function store()
    {
        if (request('cert_number') != session('smsCert.number')) {
            notify()->error(_e('Action failed'), 'Error', 'bottomRight');
            return redirect()->back()->withInput();
        }

        $response =  $this->callApiService->callApi([
            'url' => 'member-mobile-auth-passed',
            'data' => [
                'Email' => request('email'),
                'SingleUse' => request('single_use') === '1'
            ],
        ]);

        if ($this->callApiService->verifyApiError($response)) {
            notify()->error($response['body'], 'Error', 'bottomRight');
            return redirect()->back();
        }

        $this->mailTemplateService->send('msg.dabory.pro.ko_KR.email.auth.new-device-login-1',
            [
                'C11' => $response['DeviceDesc'],
                'C12' => $response['DeviceIp'],
                'C13' => route('member-device-block.store', ['code' => $response['ResetCode'], 'device_id' => $response['DeviceId']])
            ],
            request('email'), sprintf('[%s] 새로운 기기/장소에서 로그인(인증) 시도됨.', config('app.name')));

        notify()->success(_e('Action completed'), 'Success', 'bottomRight');
        return redirect()->route('member-login');
    }

    public function sendCert()
    {
        $member = $this->pick('member', [ 'Email' => request('email') ])['Data'];
        $memberExt = $this->pick('member-ext', [ 'Id' => $member['Id'] ])['Data'];
        $mobileNo = $memberExt['MobileNo'];

        $smsCert = rand(100000, 999999);
        $reqInfo = [
            'number' => $smsCert,
            'date' => date('YmdHis'),
            'mobile_no' => $mobileNo
        ];

        $title ='본인확인';
        $msg ="[Dabory] 본인확인 인증번호는 [{$smsCert}]입니다. 정확히 입력해주세요.";
        $receiver = $mobileNo;

        session()->put('smsCert', $reqInfo);

        $response = $this->smsService->sendMessage($title, $msg, $receiver);

        return response()->json($response);
    }
}
