<?php

namespace App\Http\Controllers;

use App\Models\Alumno;
use App\Models\User;
use Illuminate\Http\Request;
use Illuminate\Routing\Controller;
use Illuminate\Support\Facades\Auth;


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

    public function index()
    {
        $user = Auth::user();

        if ($user->hasRole('Promotor')) {
            $plantelesUsuario = $user->plantelUsuarios->pluck('id');

            $alumnos = User::with(['alumnos' => function ($query) use ($user, $plantelesUsuario) {

                $query->where('status', 1)
                    ->where(function ($q) use ($user, $plantelesUsuario) {

                        $q->where('usuario_registro', $user->id)
                            ->orWhereHas('alumnos.plantelUsuarios', function ($sub) use ($plantelesUsuario) {
                                $sub->whereIn('plantels.id', $plantelesUsuario);
                            });
                    });

            }])
            ->whereHas('alumnos', function ($query) use ($user, $plantelesUsuario) {

                $query->where('status', 1)
                    ->where(function ($q) use ($user, $plantelesUsuario) {

                        $q->where('usuario_registro', $user->id)
                            ->orWhereHas('alumnos.plantelUsuarios', function ($sub) use ($plantelesUsuario) {
                                $sub->whereIn('plantels.id', $plantelesUsuario);
                            });

                    });

            })
            ->get();
        }else{
        $plantelesUsuario = $user->plantelUsuarios->pluck('id');

        $alumnos = Alumno::with(['alumnos.plantelUsuarios','statusAlumno'])
            ->where('status','!=',1)
            ->whereHas('alumnos.plantelUsuarios', function ($q) use ($plantelesUsuario) {
                $q->whereIn('plantels.id', $plantelesUsuario);
            })
            ->get();
        }




        return view('alumno.index', compact('alumnos'));
    }

    public function edit(Alumno $alumno)
    {
        return view('alumno.edit',compact('alumno'));
    }


    public function update(Request $request, Alumno $alumno)
    {
        $alumno->update($request->all());
        return redirect()->route('alumno.edit',$alumno)->with('info','El alumno se actualizo correctamente');
    }

    public function destroy(Alumno $alumno)
    {
        $user = $alumno->alumnos()->first();

        if ($user) {
            $user->roles()->detach();

            $user->plantelUsuarios()->detach();

            $alumno->alumnos()->detach($user->id);

            $user->delete();
        }

            $alumno->delete();
            session()->flash('swal',[
                'icon'=>'success',
                'title'=>'Eliminado!',
                'text'=>'Se ha eliminado el registro correctamente'
            ]);
        return redirect()->route('alumno.index');
    }
}
