<?php

/*
|--------------------------------------------------------------------------
| Application Routes
|--------------------------------------------------------------------------
|
| Here is where you can register all of the routes for an application.
| It's a breeze. Simply tell Laravel the URIs it should respond to
| and give it the controller to call when that URI is requested.
|
*/

use App\Models\Banque;
use App\Models\Encadrant_Stage_Etudiant;
use App\Models\Note_Examen;
use Illuminate\Http\Response as HttpResponse;

Route::resource('/', 'welcomeController@index');

Route::get('/session/{id_Session}/examen/resultat', 'welcomeController@show')
    ->where('id_Session', '[0-9]+');;
Route::get('/session/{id_Session}/examen/resultat/{id_Examen}/notes', 'welcomeController@showNotes')
    ->where('id_Examen', '[0-9]+')->where('id_Session', '[0-9]+');
// Authentication routes...
Route::get('auth/login', 'Auth\AuthController@getLogin');
Route::post('auth/login', 'Auth\AuthController@postLogin');
Route::get('auth/logout', 'Auth\AuthController@getLogout');


Route::get('email/confirmation/{id_Session}/{id_Etudiant}/{confirmation_Code}', 'InscriptionController@update')
    ->where('id_Session', '[0-9]+')->where('id_Etudiant', '[0-9]+');

// needs admin privilege
Route::group(['middleware' => ['auth', 'ecos.privilege']], function () {

    // banque group
    Route::group(['prefix' => 'banque'], function () {

        // dipslay the hole banque
        Route::match(['get', 'post'], '/', 'BanqueController@show');

        // display banque item
        Route::match(['get', 'post'], '/{banque_id}', 'BanqueController@show')
            ->where('banque_id', '[0-9]+');

        // edit banque
        Route::match(['get', 'post'], '/{banque_id}/edit', 'BanqueController@edit')
            ->where('banque_id', '[0-9]+');

        // store in the banque
        Route::post('/add', 'BanqueController@store');

        // test if the a banque with specified id not found
        Route::filter('banqueId', function (Illuminate\Routing\Route $route) {
            if (is_null($route->getParameter('banque_id'))) {
                return Redirect::to('banque');
            } else {
                if (is_null(Banque::find($route->getParameter('banque_id'))))
                    return Response::json(['error' => 'Banque not found'], HttpResponse::HTTP_NOT_FOUND);
            }

        });

        // item group
        Route::group(['prefix' => '/{banque_id}/item', 'before' => 'banqueId'], function () {

            // display all banque items
            Route::match(['get', 'post'], '/', 'ItemController@show')
                ->where('item_id', '[0-9]+');

            // display specific banque item
            Route::match(['get', 'post'], '/{item_id}', 'ItemController@show')
                ->where('item_id', '[0-9]+');

            // store a banque item
            Route::post('/add', 'ItemController@store');

            // Edit item
            Route::post('/{item_id}/edit', 'ItemController@edit')
                ->where('item_id', '[0-9]+');

        });
        // item group
        Route::group(['prefix' => '/{banque_id}/item', 'before' => 'banqueId'], function () {

            // display all banque items
            Route::match(['get', 'post'], '/', 'ItemController@show')
                ->where('item_id', '[0-9]+');

            // display specific banque item
            Route::match(['get', 'post'], '/{item_id}', 'ItemController@show')
                ->where('item_id', '[0-9]+');

            // store a banque item
            Route::post('/add', 'ItemController@store');

        });

    });

    // titre group
    Route::group(['prefix' => 'titre'], function () {

        // dipslay all titres
        Route::match(['get', 'post'], '/', 'TitreController@show');

        // display specific titre
        Route::match(['get', 'post'], '/{titre_id}', 'TitreController@show')
            ->where('titre_id', '[0-9]+');

        // Edit titre
        Route::match(['get', 'post'], '/{titre_id}/edit', 'TitreController@edit')
            ->where('titre_id', '[0-9]+');

        // add new titre
        Route::post('/add', 'TitreController@store');

    });
    // titre group
    Route::group(['prefix' => 'file'], function () {


        // add new titre
        Route::post('/add', 'FileController@store');

    });

});
//Privilege Group
Route::group(['prefix' => 'privileges'],function(){

     //needs admin privilege
     Route::group(['middleware' => ['auth', 'admin.privilege']], function () {
        //View Enseignant to modfiy our privilege
        Route::match(['get', 'post'], '/', 'PrivilegeController@index');
        //Recherche
        Route::post('/show', 'ObserverController@showRech');
        //Recherche
        Route::post('/{enseignant_id}/edit','PrivilegeController@edit');
     });
});


//Session Group
Route::group(['prefix' => 'session'], function () {


    // needs admin privilege
    Route::group(['middleware' => ['auth', 'admin.privilege']], function () {

        //Store in th Session
        Route::post('/add', 'SessionController@store');

        //Create Session
        Route::match(['get', 'post'], '/create', 'SessionController@index');

        //Recherche
        Route::post('/store', 'ObserverController@store');
        //Delete Session index
        Route::match(['get', 'post'], '/{session_id}/delete', 'SessionController@destroy');
        //Affich View Session Update index
        Route::match(['get', 'post'], '/{session_id}/update', 'SessionController@index');
        //Update Session index
        Route::match(['get', 'post'], '/{session_id}/edit', 'SessionController@edit');

    });


    //Inscription Group
    Route::group(['prefix' => '/{session_id}/inscription', 'before' => 'sessionId'], function () {
        //Affichage du formulaire d'inscription de l'etudiant dans un session donnée
        Route::match(['get', 'post'], '/', 'InscriptionController@index');
        // Formulaire de selection des examens
        Route::match(['get', 'post'], '/{etudiant_id}', 'InscriptionController@show')
            ->where('etudiant_id', '[0-9]+');
        // Ajout de l'etudiant dans un examen
        Route::match(['get', 'post'], '/{etudiant_id}/{examen_id}/add', 'InscriptionController@store')
            ->where('etudiant_id', '[0-9]+');
        //Verification de l'etudiant
        Route::match(['get', 'post'], '/verif', 'InscriptionController@verif')
            ->where('etudiant_id', '[0-9]+');

    });


    // needs jury or admin privilege
    Route::group(['middleware' => ['auth', 'session.privilege']], function () {
        //display the hole Session
        Route::match(['get', 'post'], '/', 'SessionController@show');
        //display specific Session
        Route::match(['get', 'post'], '/{session_id}', 'SessionController@show')
            ->where('session_id', '[0-9]+');

        //Examen Group
        Route::group(['prefix' => '/{session_id}/examen', 'before' => 'sessionId'], function () {
            //display All examen Session Specefic
            Route::match(['get', 'post'], '/', 'ExamenController@show');

            // display specific banque item
            Route::match(['get', 'post'], '/{examen_id}', 'ExamenController@show')
                ->where('examen_id', '[0-9]+');

            // store examen to Session
            Route::post('/add', 'ExamenController@store');

            //Create Examen index
            Route::match(['get', 'post'], '/create', 'ExamenController@index');
            //Delete Examen index
            Route::match(['get', 'post'], '/{examen_id}/delete', 'ExamenController@destroy');
            //Affich View Examen Update index
            Route::match(['get', 'post'], '/{examen_id}/update', 'ExamenController@index');
            //Update Examen index
            Route::match(['get', 'post'], '/{examen_id}/edit', 'ExamenController@edit');

            //Station group
            Route::group(['prefix' => '/{examen_id}/station', 'before' => 'examenId'], function () {
                //display All examen Session Specefic
                Route::match(['get', 'post'], '/', 'StationController@show');

                // display specific Station
                Route::match(['get', 'post'], '/{station_id}', 'StationController@show')
                    ->where('station_id', '[0-9]+');

                //create Session
                Route::match(['get', 'post'], '/create', 'StationController@index')
                    ->where('station_id', '[0-9]+');

                // store Station to Examen
                Route::post('/add', 'StationController@store');

                Route::match(['get', 'post'], '/{station_id}/delete', 'StationController@destroy')
                    ->where('station_id', '[0-9]+');
                Route::match(['get', 'post'], '/{station_id}/update', 'StationController@update')
                    ->where('station_id', '[0-9]+');

            });
            //Liste Inscrit group
            Route::group(['prefix' => '/{examen_id}/inscrit', 'before' => 'examenId'], function () {
                //display All etudiant inscrit in specifi examen
                Route::match(['get', 'post'], '/', 'InscritController@show');


            });


            //Resultat groupe
            Route::group(['prefix' => '/{examen_id}/resultat', 'before' => 'examenId'], function () {

                // dipslay the hole notes
                Route::match(['get', 'post'], '/', 'ExamenNoteController@show');

                // display the student's exame note
                Route::match(['get', 'post'], '/{examen_note_id}', 'ExamenNoteController@show')
                    ->where('examen_note_id', '[0-9]+');

                // test if the a student with specified id not found
                Route::filter('examenNoteId', function (Illuminate\Routing\Route $route) {
                    if (is_null($route->getParameter('examen_note_id'))) {
                        return Redirect::to('session/'
                            . $route->getParameter('session_id')
                            . '/examen/'
                            . $route->getParameter('examen_id')
                            . '/resultat');
                    } else {
                        if (is_null(Note_Examen::find($route->getParameter('examen_note_id'))))
                            return Response::json(['error' => 'reusltat not found'], HttpResponse::HTTP_NOT_FOUND);
                    }
                });

                // station note group
                Route::group(['prefix' => '/{examen_note_id}/station', 'before' => 'examenNoteId'], function () {

                    // display all station notes
                    Route::match(['get', 'post'], '/', 'StationNoteController@show');

                    // display specific banque item
                    Route::match(['get', 'post'], '/{station_note_id}', 'StationNoteController@show')
                        ->where('station_note_id', '[0-9]+');

                    // test if the a note exam with specified id not found

                    Route::filter('stationNoteId', function (Illuminate\Routing\Route $route) {
                        if (is_null($route->getParameter('station_note_id'))) {
                            return Redirect::to('session/'
                                . $route->getParameter('session_id')
                                . '/examan/'
                                . $route->getParameter('examen_id')
                                . '/resultat/'
                                . $route->getParameter('examen_note_id')
                                . '/station');
                            if (is_null(Note_Station::find($route->getParameter('station_note_id'))))
                                return Response::json(['error' => 'station note not found'], HttpResponse::HTTP_NOT_FOUND);
                        }
                    });

                    // item note group

                    Route::group(['prefix' => '/{station_note_id}/item', 'before' => 'stationNoteId'], function () {
                        // display all station notes
                        Route::match(['get', 'post'], '/', 'ItemNoteController@show');

                        // display specific banque item
                        Route::match(['get', 'post'], '/{item_note_id}', 'ItemNoteController@show')
                            ->where('station_note_id', '[0-9]+');
                    });
                });
            });

        });

    });

});


// creating and updating Note groupe
Route::group(['prefix' => 'note'/*, 'middleware' => 'auth'*/], function () {

    // needs admin privilege
    Route::group(['middleware' => 'admin.privilege'], function () {
        // Calcul of all notes
        Route::match(['get', 'post'], '/{id_Session}/update/{id_Examen}', 'NoteController@update')
            ->where('id_Examen', '[0-9]+')->where('id_Session', '[0-9]+');
    });

    // needs observateur privilege
   // Route::group(['middleware' => 'observateur.privilege'], function () {
        // store station note
        Route::post('/add', 'NoteController@store');
  //  });

});

// stage, encadrant, student group needs encadrant privilege
Route::group(['prefix' => 'encadrant', 'middleware' => ['auth', 'encadrant.privilege']], function () {

    // dipslay the hole banque
    Route::match(['get', 'post'], '/', 'StageEncadrantController@show');

    // display banque item
    Route::match(['get', 'post'], '/{encadrant_id}', 'StageEncadrantController@show')
        ->where('encadrant_id', '[0-9]+');

    Route::group(['middleware' => 'admin.privilege'], function () {
        //Affectation des stages
        Route::match(['get', 'post'], '/create', 'StageController@index');

        // store encadrant to etudiant
        Route::post('/add', 'StageController@store');

        // store encadrant to etudiant
        Route::post('/store', 'ObserverController@index');

    });

    Route::group(['prefix' => '/{encadrant_id}/stage', 'before' => 'encadrantId'], function () {

        // dipslay the hole banque
        Route::match(['get', 'post'], '/', 'StageController@show');

        // display banque item
        Route::match(['get', 'post'], '/{stage_id}', 'StageController@show')
            ->where('stage_id', '[0-9]+');

        //Liste des etudiants non validées
        Route::match(['get', 'post'], '/show', 'StageEncadrantController@index');

        // valider les stage pour un etudiant
        Route::post('/valide', 'StageEncadrantController@store');

        Route::filter('stageId', function (Illuminate\Routing\Route $route) {
            if (is_null($route->getParameter('stage_id'))) {
                return Redirect::to('encadrant'
                    . $route->getParameter('encadrant_id')
                    . '/stage');
            } else {
                if (is_null(Encadrant_Stage_Etudiant::find($route->getParameter('stage_id'))))
                    return Response::json(['error' => 'stage not found'], HttpResponse::HTTP_NOT_FOUND);
            }

        });

        Route::group(['prefix' => '/{stage_id}/student', 'before' => 'stageId'], function () {

            // dipslay the hole banque
            Route::match(['get', 'post'], '/', 'StageStudentController@show');

            // display banque item
            Route::match(['get', 'post'], '/{student_id}', 'StageStudentController@show')
                ->where('student_id', '[0-9]+');

            Route::match(['get', 'post'], '/{student_id}/valid', 'StageStudentController@update');

            Route::filter('studentId', function (Illuminate\Routing\Route $route) {
                if (is_null($route->getParameter('student_id'))) {
                    return Redirect::to('encadrant'
                        . $route->getParameter('encadrant_id')
                        . '/stage/'
                        . $route->getParameter('stage_id')
                        . '/student');
                } else {
                    if (is_null(Encadrant_Stage_Etudiant::find($route->getParameter('student_id'))))
                        return Response::json(['error' => 'stage not found'], HttpResponse::HTTP_NOT_FOUND);
                }
            });
        });
    });
});

//});

//jury group
Route::group(['prefix' => 'jury', 'middleware' => ['auth', 'session.privilege']], function () {
    // dipslay the jury's exames
    Route::match(['get', 'post'], '/{id_Jury}', 'JuryController@index')
        ->where('id_Jury', '[0-9]+');
});

Route::group(['prefix'=>'mobile'],function(){
    Route::match(['get','post'],'/session','MobileController@showSessions');
    Route::match(['get','post'],'/session/{id_Session}/examen','MobileController@showExamens');
    Route::match(['get','post'],'/session/{id_Session}/examen/{id_Examen}/station','MobileController@showStations');
    Route::match(['get','post'],'/session/{id_Session}/examen/{id_Examen}/station/{id_Station}/grille','MobileController@showGrille');
});
