<?php
// Routes using the web middleware

require "login.php";

Route::get('/sitemap', 'SitemapController@sitemap')->name('sitemap');

Route::get('/ping', function () {
    die(
        json_encode([
            'user_agent' => request()->userAgent(),
            'ip' => request()->ip(),
            'server_time' => date('r'),
        ], 128)
    );
});

Route::get('/about/terms', function () {
    return view('about.terms');
})->name('terms');

Route::get('/about/privacy', function () {
    return view('about.privacy');
})->name('privacy');

Route::get('/about/faq', function () {
    return view('about.faq');
})->name('faq');

Route::get('/about/donate', function () {
    return view('about.donate');
})->name('donate');


Route::get('/', 'DashController@view')->name('dash');

// redirect home to user home
Route::get('/home', function () {
    if (guest()) return redirect(route('dash'));

    return redirect(route('profile.view', user()->name));
})->name('home');

// Table resource
Route::get('@{user}', 'ProfileController@view')->name('profile.view');
Route::get('@{user}/!favourites', 'ProfileController@viewFavourites')->name('profile.view-favourites');
Route::get('@{user}/{table}', 'TableController@view')->name('table.view');
Route::get('@{user}/{table}/revisions', 'TableController@viewRevisions')->name('table.revisions');
Route::get('@{user}/{table}/export', 'TableController@export')->name('table.export');
Route::get('@{user}/{table}/favourite', 'TableController@favouriteTable')->name('table.favourite');
Route::get('@{user}/{table}/unfavourite', 'TableController@unfavouriteTable')->name('table.unfavourite');
Route::get('@{user}/{table}/favourites', 'TableController@viewFavourites')->name('table.favourites');


// Routes for confirmed users
Route::group(['middleware' => ['auth', 'activated']], function () {
    // Table resource
    Route::group([
        'prefix' => 'table',
    ], function () {
        Route::get('create', 'TableController@create')->name('table.create');
        Route::post('create', 'TableController@storeNew')->name('table.storeNew');
    });

    Route::get('@{user}/{table}/settings', 'TableController@settings')->name('table.conf');
    Route::post('@{user}/{table}/settings', 'TableController@storeSettings')->name('table.storeConf');
    Route::post('@{user}/{table}/delete', 'TableController@delete')->name('table.delete');
    Route::get('@{user}/{table}/revert-to', 'TableController@revertTo')->name('table.revertTo');

    Route::post('@{user}/{table}/draft/update', 'TableEditController@draftUpdate')->name('table.draft-update');
    Route::post('@{user}/{table}/draft/submit', 'TableEditController@draftSubmit')->name('table.draft-submit');
    Route::get('@{user}/{table}/draft/update', 'TableEditController@draftUpdate');
    Route::get('@{user}/{table}/draft/discard', 'TableEditController@discard')->name('table.draft-discard');
    Route::get('@{user}/{table}/draft/{tab?}', 'TableEditController@draft')->name('table.draft');
});

// Routes for all authed users
Route::group(['middleware' => 'auth'], function () {

    Route::group([
        'prefix' => 'profile',
    ], function () {
        Route::get('edit', 'ProfileController@editProfile')->name('profile.edit');
        Route::post('store', 'ProfileController@storeProfile')->name('profile.store');
    });

    Route::group([
        'prefix' => 'account',
    ], function () {
        Route::get('edit', 'AccountController@editAccount')->name('account.edit');
        Route::post('store', 'AccountController@storeAccount')->name('account.store');
        Route::get('forget-social-login/{id}', 'AccountController@forgetSocialLogin')->name('forget-identity');
    });
});

Route::post('toggle-dark-mode', function () {
    if (dark_mode()) {
        setcookie('dark_mode', '0', time() - 1);
    } else {
        setcookie('dark_mode', '1');
    }
    return redirect()->back();
})->name('toggle-dark-mode');