<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Models\User;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Http;

class ManageUsersController extends Controller
{
    /**
     * Display a listing of the resource.
    */
    public function index(Request $request)
    {
        $users = User::orderBy('id' , 'DESC');
        $total_user = User::query()->count('id');
        $total_suspend = User::query()->where('suspend' , '0')->count('id');
        $total_google = User::query()->where('googlef2_status' , '1')->count('id');

        if($request->has('search') && $request->search == 'suspended'){
            $users = $users->where('suspend' , 0);
        }
        if($request->has('search') && $request->search == 'activeFA'){
            $users = $users->where('googlef2_status' , '1');
        }
        if($request->has('search') && $request->search != null){
            $users = $users->orwhere('lastname' , 'like' , '%'.$request->search.'%')
            ->orwhere('email' , 'like' , '%'.$request->search.'%');
        }
        if($request->has('pin') && $request->pin != null){
            $users = $users->where('support_pin', $request->pin);
        }

        $users = $users->paginate(10);

        return view('Admin.User.list' , compact('users' , 'total_user', 'total_suspend' , 'total_google'));
    }

    public function suspendUser(User $user){

        // SEO not working currently 
        // Http::get('https://seo.af/api/UserStatusChange/'.$user->email.'/'.$user->api_token);


        Http::get('https://sms.af/api/UserLeapStatus/'.$user->email.'/'.$user->api_token);
        $user->update([
            'suspend' => $user->suspend == 0 ? 1 : 0
        ]);
        return redirect()->back()->with('success_message', 'User has been suspended');

    }


    public function changeSMSStatus(User $user){
        Http::get('https://sms.aryanict.af/api/suspendUser?api_token='.$user->api_token);
        $user->update([
            'sms_status' => $user->sms_status == 0 ? 1 : 0,
        ]);
        return redirect()->back()->with('success_message' , 'SMS Sucessfully Suspended');
    }


    public function changePasswordView(User $user){
        return view('Admin.User.ChangePass' , compact('user'));
    }


    public function storePassword(Request $request, User $user){
        $this->validate($request , [
            'password' => 'required',
            'confirm' => 'required|same:password'
        ]);
        $user->update([
            'password' => Hash::make($request->password)
        ]);
        return redirect()->route('manage-users.index')->with('success_message', 'User password successfully changed');
    }


    public function change_google2fa(User $user){   
        if($user->googlef2_status == 1){
            $user->update([
                'googlef2_status' => '0'
            ]);
        }
        return redirect()->back()->with('success_message', 'User google 2FA has been successfully disabled');
    }

    public function deleteUser(User $user){
        $user->delete();
        return redirect()->back()->with('success_message', 'User suceesfully delete');
    }

}
