<?php

namespace App\Http\Controllers\Auth;

use App\Http\Controllers\Controller;
use App\Repositories\Member\Auth\MemberAuthRepository;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Log;

class RegisterController extends Controller
{
    public function index()
    {
        return view('account.signup');
    }

    public function register(Request $request, MemberAuthRepository $memberAuthRepository)
    {
        $validated = $request->validate([
            'name' => 'required|string',
            'referral' => 'nullable|string',
            'phone' => 'string',
            'email' => 'nullable|email',
            'gender' => 'nullable|in:LAKI-LAKI,PEREMPUAN',
            'date_of_birth' => 'nullable|date'
        ]);

       try {
            $customer = $memberAuthRepository->register($validated);

            $check = $request->all();
            $check["user_id"] = $customer->user_id;

            $auth = $memberAuthRepository->getAuth($check);

            return redirect('/')->with('success', 'Registration successful!');
        } catch (\Exception $e) {
            Log::info($e);
            return redirect()->back()->with('error', $e->getMessage());
        }
    }
}
