<?php

namespace Themes\Pro\modunawa\app\Http\Controllers\Auth;

use App\Http\Controllers\Controller;
use App\Services\CallApiService;

class MemberWithdrawalController extends Controller
{
    private $callApiService;

    public function __construct(CallApiService $callApiService)
    {
        $this->callApiService = $callApiService;
    }

    public function index()
    {
        return view('views.auth.member-withdrawal');
    }

    public function store()
    {
        $response = $this->callApiService->callApi([
            'url' => 'member-login',
            'data' => [
                'Email' => session('member')['Email'],
                'Password' => request('password')
            ]
        ]);

        if ($this->callApiService->verifyApiError($response)) {
            notify()->error('현재 비밀번호를 입력해주세요', 'Error', 'bottomRight');
            return redirect()->back();
        }

        $memberId = session('member')['MemberId'];

        $memberSecuredAct = $this->callApiService->callApi([
            'url' => 'member-act',
            'data' => [
                'Page' => [
                    [
                        'Id' => $memberId,
                        'IsWithdrawn' => '1',
                    ]
                ]
            ],
        ]);

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

        notify()->success(_e('Action completed'), 'Success', 'bottomRight');

        return redirect()->route('member-logout');
    }
}
