<?php

namespace App\Http\Controllers;

use App\Models\TypeUser;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Auth;

use Illuminate\Validation\ValidationException;


use Illuminate\Http\Request;
use Laravel\Fortify\Contracts\LoginResponse as LoginResponseContract;
use Laravel\Fortify\Contracts\LogoutResponse as LogoutResponseContract;
use Laravel\Fortify\Fortify;
use Laravel\Fortify\Http\Controllers\AuthenticatedSessionController as FortifyAuthenticatedSessionController;

use Illuminate\Auth\Events\Verified;

use App\Models\User;

class userController extends Controller
{
    public function UserProfile($id)
    {
        $user = User::find($id);
        return view('Admin.profile', compact('user'));
    }

    public function yourVerificationMethod(Request $request, $id, $hash)
    {
        $user = User::findOrFail($id);

        if (!hash_equals((string) $hash, sha1($user->getEmailForVerification()))) {
            abort(403, 'Unauthorized action.');
        }

        if ($user->markEmailAsVerified()) {
            event(new Verified($user));
        }

        // Redirecione para a rota desejada após a verificação bem-sucedida
        return redirect()->route('CreateUsers')->with('message', 'E-mail verificado com sucesso!');
    }


    public function authenticate(Request $request)
    {
        $request->validate([
            Fortify::username() => 'required|string',
            'password' => 'required|string',
        ]);

        $user = User::where('email', $request->email)->first();

        if ($user && Hash::check($request->password, $user->password)) {
            return $user;
        }

        return null;
    }

    public static function authenticateAndRedirect(Request $request)
    {
        $user = User::where('email', $request->email)->first();

        if ($user && Hash::check($request->password, $user->password)) {
            Auth::login($user);

            return redirect()->route('CreateUsers');
        }

        return null;
    }


    public function ListCompanies()
    {
        $users = User::with('userType')->where('type_users', '=', 3)->get();
    
        return view('Admin.CrudUsers.listCompany', compact('users'));
    }

    //Busca Todos os utilizadores Exeto as 'Empresas'
    public function ListUsers()
    {
        $users = User::with('userType')->where('type_users', '<>', 3)->get();
    
        return view('Admin.CrudUsers.listUsers', compact('users'));
    }

    public function show($id)
    {
        $user = User::findOrFail($id);
        return view('Admin.CrudUsers.showUsers', compact('user'));
    }

    public function edit($id)
    {
        $user = User::findOrFail($id);
        return view('Admin.CrudUsers.editUsers', compact('user'));
    }

    public function update(Request $request, User $user)
    {
        $request->validate([
            'name' => 'required',
            'email' => 'required|email|unique:users,email,' . $user->user_id,
            'password' => 'nullable|min:8|confirmed',
            'user_type' => 'required',
            'user_phone' => 'required',
            'user_nif' => 'required',
        ]);

        $user->update([
            'name' => $request->get('name'),
            'email' => $request->get('email'),
            'password' => $request->filled('password') ? Hash::make($request->get('password')) : $user->password,
            'user_type' => $request->get('user_type'),
            'user_phone' => $request->get('user_phone'),
            'user_nif' => $request->get('user_nif'),
        ]);

        return redirect('/users/ListUsers')->with('success', 'Usuario atualizado com Sucesso!!');
    }



    public function create()
    {
        return view('users.create');
    }

    public function store(Request $request)
    {
        $request->validate([
            'name' => 'required',
            'email' => 'required|email|unique:users',
            'password' => 'required|min:8|confirmed',
            'user_type' => 'required',
            'user_phone' => 'required',
            'user_nif' => 'required',
        ]);

        $user = new User([
            'name' => $request->get('name'),
            'email' => $request->get('email'),
            'password' => Hash::make($request->get('password')),
            'user_type' => $request->get('user_type'),
            'user_phone' => $request->get('user_phone'),
            'user_nif' => $request->get('user_nif'),
        ]);

        $user->save();
        return redirect('/users')->with('success', 'User created successfully!');
    }

    public function destroy(User $user)
    {
        $user->delete();
        return redirect('/users/ListUsers')->with('success', 'User deleted successfully!');
    }
}
