<?php

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

use App\Http\Controllers\Controller;
use App\Services\CallApiService;
use App\Services\DormantService;
use Illuminate\Support\Facades\Cookie;
use Illuminate\Support\Facades\Validator;

class LoginController extends Controller
{
    private $callApiService;
    private $dormantService;

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

    public function index()
    {
        $this->dormantService->setGateToken(session('GateToken'));
        $dormantDefault = $this->dormantService->getSetup();
        session()->put('GateToken', $this->dormantService->getGateToken('all'));

        [$oauth2InfoList, $develLoginInfo] = setupSsoClientOrLoginInfo();

//        dump($oauth2InfoList);

        return view('views.auth.member-login', compact('oauth2InfoList', 'develLoginInfo', 'dormantDefault'));
    }

    public function store()
    {
        $validator = validator::make(request()->all(), [
            'email' => ['required', 'email'],
            'password' => ['required'],
        ]);

        if ($validator->fails()) {
            return redirect()->back()
                ->withErrors($validator)
                ->withInput();
        }

        $data = [
            'Email' => request('email'),
            'Password' => request('password')
        ];
        $response = $this->callApiService->callApi([
            'url' => 'member-login',
            'data' => $data
        ]);

        if (isset($response['apiStatus'])) {
            switch ($response['apiStatus']) {
                case 228:
                    return redirect()->route('member-login', ['email' => request('email')])->with(['dormant_member' => true]);
                case 701:
                    return redirect()->route('member-verify-resend', ['email' => request('email')]);
                case 702:
                    notify()->error(__('Google 로그인으로 접속해주세요'), 'Error', 'bottomRight');
                    return redirect()->back()->withInput();
                case 703:
                    notify()->error(__('탈퇴 진행 중인 계정입니다'), 'Error', 'bottomRight');
                    return redirect()->back()->withInput();
                case 709:
                    return redirect()->route('find-member-pw-verify.index', ['email' => request('email')]);
                default:
                    $mgs = $response['body'] ?? _e('API request failed. Please Check');
                    notify()->error($mgs, 'Error', 'bottomRight');
                    return redirect()->back()->withInput();
            }
        }

        session()->put('locale', \Str::of($response['CountryCode'])->explode('_')->first());
        session()->put('member', array_merge($response, ['is_member' => true, 'Ip' => request()->ip()]));
        session()->forget('user');

        $redirectUrl = getLoginRedirectUrl(request('previous_url'));

        if (request('remember')) {
            $minutes = time() + 60 * 60 * 24 * 100;
            return redirect()->to($redirectUrl)->withCookie(cookie('dabory_member_info', serialize($data), $minutes));
        }

        return redirect()->to($redirectUrl)->withCookie(Cookie::forget('dabory_member_info'));
    }
}
