<?php

/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/

Route::get('/', function () {

    $user = Auth::getUser();
    if ($user) {
        if ($user->can(\App\Permission::LIST_USERS))
            return redirect('users');
        elseif ($user->can(\App\Permission::LIST_DEVICE_CLASSES))
            return redirect('entity-classes');
        elseif ($user->can(\App\Permission::LIST_DEVICES))
            return redirect('entities');
        elseif ($user->can(\App\Permission::LIST_LOCATIONS))
            return redirect('locations');
        elseif ($user->can(\App\Permission::LIST_MANUFACTURERS))
            return redirect('manufacturers');
        elseif ($user->can(\App\Permission::LIST_SUPPLIERS))
            return redirect('suppliers');
        else
            return redirect('user');
    }

    return redirect('login');

})->name('index');

Route::group(['middleware' => ['role:admin']], function () {

    Route::resource('users', 'UserController');

    Route::resource('entity-classes', 'EntityClassController',
        ['except' => [
            'create',
            'store',
            'edit',
            'update'
        ]]);
});

Route::group(['middleware' => ['role:admin|entity-manager']], function () {

    // See https://laravel.com/docs/5.4/controllers#resource-controllers for route names
    Route::get('entities/create', 'EntityController@createDistinguish')->name('entities.createDistinguish');
    Route::get('entities/{classId}/create', 'EntityController@create')->name('entities.create');
    Route::post('entities/{classId}', 'EntityController@store')->name('entities.store');

    Route::get('entities/{classId}/{id}/edit', 'EntityController@edit')->name('entities.edit');
    Route::put('entities/{classId}/{id}', 'EntityController@update')->name('entities.update');
    Route::delete('entities/{classId}/{id}', 'EntityController@destroy')->name('entities.destroy');

    Route::get('entity-types/create', 'EntityTypeController@createDistinguish')->name('entity-types.createDistinguish');
    Route::get('entity-types/{classId}/create', 'EntityTypeController@create')->name('entity-types.create');
    Route::post('entity-types/{classId}', 'EntityTypeController@store')->name('entity-types.store');

    Route::get('entity-types/{classId}/{id}/edit', 'EntityTypeController@edit')->name('entity-types.edit');
    Route::put('entity-types/{classId}/{id}', 'EntityTypeController@update')->name('entity-types.update');
    Route::delete('entity-types/{classId}/{id}', 'EntityTypeController@destroy')->name('entity-types.destroy');

    // See https://laravel.com/docs/5.4/controllers#resource-controllers for route names
    Route::resource('locations', 'LocationController',
        ['except' => [
            'index',
            'show'
        ]]);
    Route::resource('manufacturers', 'ManufacturerController',
        ['except' => [
            'index',
            'show'
        ]]);
    Route::resource('suppliers', 'SupplierController',
        ['except' => [
            'index',
            'show'
        ]]);
});

Route::group(['middleware' => ['role:admin|entity-manager|auditor']], function () {

    Route::get('entities', 'EntityController@indexDistinguish')->name('entities.indexDistinguish');
    Route::get('entities/{classId}', 'EntityController@index')->name('entities.index');
    Route::get('entities/{classId}/{id}', 'EntityController@show')->name('entities.show');


    Route::get('entity-types', 'EntityTypeController@indexDistinguish')->name('entity-types.indexDistinguish');
    Route::get('entity-types/{classId}', 'EntityTypeController@index')->name('entity-types.index');
    Route::get('entity-types/{classId}/{id}', 'EntityTypeController@show')->name('entity-types.show');

    // See https://laravel.com/docs/5.4/controllers#resource-controllers for route names
    Route::resource('locations', 'LocationController',
        ['only' => [
            'index',
            'show'
        ]]);

    Route::get('/api/locations', function () {
        $validation = Validator::make(Request::all(), [
            'q' => 'required|string|min:1',
            'limit' => 'required|numeric|max:100'
        ]);

        if ($validation->fails())
            return response()->json(['errors' => $validation->errors()], 400);

        $query = Input::get('q');
        $limit = Input::get('limit');
        $locations = DB::table('locations')
            ->select(['name'])
            ->where('name', 'like', '%' . $query . '%')
            ->limit($limit)
            ->get();
        return response()->json($locations);
    });

    Route::resource('manufacturers', 'ManufacturerController',
        ['only' => [
            'index',
            'show'
        ]]);

    Route::get('/api/manufacturers', function () {
        $validation = Validator::make(Request::all(), [
            'q' => 'required|string|min:1',
            'limit' => 'required|numeric|max:100'
        ]);

        if ($validation->fails())
            return response()->json(['errors' => $validation->errors()], 400);

        $query = Input::get('q');
        $limit = Input::get('limit');
        $manufacturers = DB::table('manufacturers')
            ->select(['name'])
            ->where('name', 'like', '%' . $query . '%')
            ->limit($limit)
            ->get();
        return response()->json($manufacturers);
    });

    Route::resource('suppliers', 'SupplierController',
        ['only' => [
            'index',
            'show'
        ]]);

    Route::get('/api/suppliers', function () {
        $validation = Validator::make(Request::all(), [
            'q' => 'required|string|min:1',
            'limit' => 'required|numeric|max:100'
        ]);

        if ($validation->fails())
            return response()->json(['errors' => $validation->errors()], 400);

        $query = Input::get('q');
        $limit = Input::get('limit');
        $suppliers = DB::table('suppliers')
            ->select(['name'])
            ->where('name', 'like', '%' . $query . '%')
            ->limit($limit)
            ->get();
        return response()->json($suppliers);
    });
});

// Route for user himself ..
Route::get('user', function () {
    $user = Auth::user();
    if ($user)
        return app('App\Http\Controllers\UserController')->show($user);
    else
        return Redirect::route('login');
})->name('user.show');

// Route for user himself ..
Route::get('user/edit', function () {
    $user = Auth::user();
    if ($user)
        return app('App\Http\Controllers\UserController')->edit($user);
    else
        return Redirect::route('login');
})->name('user.edit');

// Routes for user himself ..
Route::put('user', function (Illuminate\Http\Request $request) {
    $user = Auth::user();
    if ($user)
        return app('App\Http\Controllers\UserController')->update($request, $user);
    else
        return Redirect::route('login');
})->name('user.update');

// Login / Logout routes ...
Route::get('login', 'Auth\LoginController@showLoginForm')->name('login');
Route::post('login', 'Auth\LoginController@login');
Route::get('logout', 'Auth\LoginController@logout');
Route::post('logout', 'Auth\LoginController@logout')->name('logout');

// Password Reset Routes...
Route::get('password/reset', 'Auth\ForgotPasswordController@showLinkRequestForm')->name('password.request');
Route::post('password/email', 'Auth\ForgotPasswordController@sendResetLinkEmail')->name('password.email');
Route::get('password/reset/{token}', 'Auth\ResetPasswordController@showResetForm')->name('password.reset');
Route::post('password/reset', 'Auth\ResetPasswordController@reset');