<?php

namespace App\Http\Controllers;

use App\Models\AreaCode;
use App\Models\Plantel;
use App\Models\User;
use Illuminate\Http\Request;
use Illuminate\Routing\Controller;
use Illuminate\Support\Facades\Password;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Str;
use Spatie\Permission\Models\Role;
use Illuminate\Auth\Notifications\ResetPassword;

class UserController extends Controller
{

    public function __construct() {
        $this->middleware('can:user.index')->only('index','show');
        $this->middleware('can:user.create')->only('create','store');
        $this->middleware('can:user.edit')->only('edit','update');
        $this->middleware('can:user.destroy')->only('destroy');
    }

    public function index()
    {
        $users=User::with(['roles','plantelUsuarios'])->get();
        return view('user.index',compact('users'));
    }


    public function create()
    {
        $plantels = Plantel::where('status','=','1')->get();
        $codigos = AreaCode::all();
        $roles= Role::all();
        return view('user.create',compact('roles','plantels','codigos'));
    }

    public function store(Request $request)
    {
        $passwordTemporal = Str::random(12);

        $user = User::create([
            'name' => $request->name,
            'email' => $request->email,
            'telefono' => $request->telefono ?? null,
            'direccion' => $request->direccion ?? null,
            'code' => $request->code ?? null,
            'status' => $request->status,
            'password' => Hash::make($passwordTemporal),
        ]);

        $user->roles()->sync($request->roles);

        $user->plantelUsuarios()->sync($request->plantels);

        $token = Password::createToken($user);
        $user->notify(new ResetPassword($token));

        return redirect()->route('user.edit',compact('user'))->with('info','Se creó el usuario correctamente');
    }

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

    public function edit(User $user)
    {
        $plantels=Plantel::all();
        $codigos = AreaCode::all();
        $roles= Role::all();
        return view('user.edit',compact('user','roles','plantels','codigos'));
    }

    public function update(Request $request, User $user)
    {
        $user->update($request->except(['plantels']));
        $user->roles()->sync($request->roles);
        $user->plantelUsuarios()->sync($request->plantels);
        return redirect()->route('user.edit',compact('user'))->with('info','Se modifico el usuario correctamente');
    }


    public function destroy(User $user)
    {
        $user->plantelUsuarios()->detach();
        $user->delete();
        session()->flash('swal',[
            'icon'=>'success',
            'title'=>'Eliminado!',
            'text'=>'Se ha eliminado el registro correctamente'
        ]);
        return redirect()->route('user.index');
    }

    public function profile(){
        return view('profile.show');
    }
}
