<?php

namespace App\Http\Controllers\App;

use Illuminate\Routing\Controller as BaseController;
use Illuminate\Http\Request;
use Hash;
use Session;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Redirect;
use App\Models\User;
use App\Models\UserData;
use App\Models\UserDocuments;
use Illuminate\Support\Facades\Validator;

class UserController extends BaseController
{
    public function loginForm(Request $request){
        
        
    }
    public function signup(Request $request){
        $validate = Validator::make($request->all(), [
            'email' => 'required|email',
            'first_name' => 'required',
            'last_name' => 'required',
            'phone_number' => 'required',
            'user_type' => 'required',
            'gender' => 'required',
        ]);
        
        if($validate->fails()){
            return response()->json([
                'status' => false,
                'message' => 'Validation errors in your request',
                'errors' => $validate->errors()
            ], 400);
        }
        $postData = $request->all();
        $userPhone = User::with(['UserData'])->whereRelation('user_data', 'phone_number', '=', $postData['phone_number'])->first();
        if($userPhone){
            return response()->json([
                'status' => false,
                'message' => 'Validation errors in your request',
                'errors' => 'Phone number already exist'
            ], 400);
        }
        $userEmail = User::with(['UserData'])->where('email', '=', $postData['email'])->first();
        if($userEmail){
            return response()->json([
                'status' => false,
                'message' => 'Validation errors in your request',
                'errors' => 'Email already exist'
            ], 400);
        }
        $user = [
            'name' => $postData['first_name'] .' ' . $postData['last_name'],
            'email' => $postData['email'],
            'user_type' => 'Subscribers',
            'password' => '123456'
        ];
        
        $user = User::create($user);
        
        $userData = [
            'user_id' => $user['id'],
            'first_name' => $postData['first_name'],
            'last_name' => $postData['last_name'],
            'phone_number' => $postData['phone_number'],
            'wallet_amount' => $postData['wallet_amount']
        ];
        $userData = UserData::create($userData);
    }
    
    /**
     * Customer login
     */
    public function login(Request $request){
        try {
            $response =[];
            $validate = Validator::make($request->all(), [
                'email' => 'required|email',
                'password' => 'required'
            ]);
            
            if($validate->fails()){
                return response()->json([
                    'status' => false,
                    'message' => 'Validation errors in your request',
                    'errors' => $validate->errors()
                ], 400);
            }
            $credentials = $request->only('email', 'password');
            if (!Auth::attempt($credentials)) {
                return response()->json([
                    'status' => false,
                    'message' => 'Email & Password does not match.',
                ], 401);
            }else{
                $user = User::where('email', $request->email)->first();
                //print_r($user); exit;
                return response()->json([
                    'status' => true,
                    'message' => 'User Logged In Successfully',
                    'token' => $user->createToken("Gozap API Token")->plainTextToken
                ], 200);
            }
        } catch (\Throwable $e) {
            return response()->json([
                'status' => false,
                'message' => $e->getMessage()
            ], 500);
        }
    }
    public function customer_kyc(){

    }
}
