<?php

namespace App\Http\Controllers;

use App\Http\Requests\UserProfile\ChangePasswordRequest;
use App\Http\Requests\UserProfile\ProfileRequest;
use App\Models\User;
use Carbon\Carbon;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Hash;
use App\Models\ResentActivity;
use App\Services\UserServices;
use Monarobase\CountryList\CountryListFacade;


class UserController extends Controller
{
 
    protected UserServices $userServices;

    public function __construct(UserServices $userServices)
    {
        $this->userServices =  $userServices;
    }


    public function UserProfile(){
        $countries = CountryListFacade::getList('en');
        return view('Dashboard.User.profile' , compact('countries'));
    }

    public function UserProfileUpdate(ProfileRequest $request){
            $this->userServices->update($request->handleRequest());
            $this->updateOrCreateResentActivity();
            return redirect()->back()->with('success_message' , 'Your profile has been successfully updated!');
    }

    public function ChangePassword(){
        return view('Dashboard.User.ChangePassword');
    }

    public function ChangePasswordSave(ChangePasswordRequest $request){

         auth()->user()->update(['password' => Hash::make($request->password)]);
         $this->updateOrCreateResentActivity();
         return redirect()->back()->with('success_message' , 'Your myLEAP account password has been changed successfully!');
        
    }
    
    public function CheckSupportPin(Request $request){
        if($request->has('pin') and $request->pin != null){
            $user = User::where('support_pin' , $request->pin)->first();
            if($user != null){
                 return response()->json(['name'=>$user->name.' '.$user->lastname,'email'=>$user->email,'phone'=>$user->phone_number]);
            }else{
                 return false;
            }
        }else{
            return false;
        }
        
        
    }

    protected function updateOrCreateResentActivity(){
        ResentActivity::updateOrCreate(['user_id'=>auth()->user()->id],
        [
            'profile_update_date' => Carbon::now()->format('Y-m-d')
        ]);
    }
    
    
    
    
    
}
