<?php

use Illuminate\Support\Facades\Route;
use App\Http\Controllers\AppController;
use App\Http\Controllers\LogInController;
use App\Http\Controllers\MyAccountController;
use App\Http\Controllers\EmailTestController;
use App\Http\Controllers\AdminController;

/*
|--------------------------------------------------------------------------
| 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('', [AppController::class, 'index'])->name('index');

Route::middleware('ensureUserNotLoggedIn')->group(function () {
    Route::get('/log-in', [LoginController::class, 'logIn'])->name('log-in');
    Route::post('/process-log-in', [LoginController::class, 'processLogIn'])->name('process-log-in');

    Route::get('/log-in-with-google', [LoginController::class, 'logInWithGoogle'])->name('log-in-with-google');
    Route::get('/log-in-with-facebook', [LoginController::class, 'logInWithFacebook'])->name('log-in-with-facebook');

    Route::get('/forgot-password', [LoginController::class, 'forgotPassword'])->name('forgot-password');
    Route::post('/process-forgot-password', [LoginController::class, 'processForgotPassword'])->name('process-forgot-password');
    Route::get('/forgot-password-success', [LogInController::class, 'forgotPasswordSuccess'])->name('forgot-password-success');
    Route::get('/reset-password/{passwordResetToken}', [LoginController::class, 'resetPassword'])->name('reset-password');
    Route::post('/process-reset-password', [LoginController::class, 'processResetPassword'])->name('process-reset-password');
});

Route::group(['middleware' => ['ensureUserLoggedIn']], function () {
    Route::prefix('/my-account')->name('my-account')->group(function () {
        Route::get('/', [MyAccountController::class, 'index'])->name('.index');
        Route::get('/log-in-settings', [MyAccountController::class, 'logInSettings'])->name('.log-in-settings');

        Route::get('/user/enable-google-log-in', [MyAccountController::class, 'enableGoogleLogIn'])->name('.enable-google-log-in');
        Route::get('/user/enable-facebook-log-in', [MyAccountController::class, 'enableFacebookLogIn'])->name('.enable-facebook-log-in');
    });

    //ADMIN
    Route::group(['middleware' => ['ensureAdminRole']], function () {
        Route::prefix('/admin')->name('admin')->group(function () {
            Route::get('/dashboard', [AdminController::class, 'dashboard'])->name('.dashboard');
            Route::get('/users', [AdminController::class, 'users'])->name('.users');
            Route::prefix('/users/view/{user}')->name('.users.view')->group(function () {
                Route::get('/dashboard', [AdminController::class, 'userDashboard'])->name('.dashboard');
                Route::get('/orders', [AdminController::class, 'userOrders'])->name('.orders');
                Route::get('/transactions', [AdminController::class, 'userTransactions'])->name('.transactions');
                Route::get('/sessions', [AdminController::class, 'userSessions'])->name('.sessions');
                Route::get('/user-events', [AdminController::class, 'userUserEvents'])->name('.user-events');
                Route::get('/payment-methods', [AdminController::class, 'userPaymentMethods'])->name('.payment-methods');
                Route::get('/messages', [AdminController::class, 'userMessages'])->name('.messages');
                Route::get('messages/{message}/attachments', [AdminController::class, 'userMessageAttachments'])->name('message-attachments');
            });
            
            Route::get('/orders', [AdminController::class, 'orders'])->name('.orders');
            Route::prefix('/orders/view/{order}')->name('.orders.view')->group(function () {
                Route::get('/dashboard', [AdminController::class, 'orderDashboard'])->name('.dashboard');
                Route::get('/financial-transactions', [AdminController::class, 'orderFinancialTransactions'])->name('.financial-transactions');
                Route::post('/order-charge', [AdminController::class, 'orderCharge'])->name('.order-charge');
            });

            Route::get('/payment-methods', [AdminController::class, 'paymentMethods'])->name('.payment-methods');
            Route::prefix('/payment-methods/view/{paymentMethod}')->name('.payment-methods.view')->group(function () {
                Route::get('/dashboard', [AdminController::class, 'paymentMethodDashboard'])->name('.dashboard');
            });

            Route::get('/financial-transactions', [AdminController::class, 'financialTransactions'])->name('.financial-transactions');
            Route::prefix('/financial-transactions/view/{order}')->name('.financial-transactions.view')->group(function () {
                Route::get('/dashboard', [AdminController::class, 'financialTransactionDashboard'])->name('.dashboard');
            });

            Route::get('/report/{report}', [AdminController::class, 'report'])->name('.report');

            Route::get('/sessions', [AdminController::class, 'sessions'])->name('.sessions');
            Route::get('/user-events', [AdminController::class, 'userEvents'])->name('.user-events');
        });

        Route::get('/search-users', [AppController::class, 'searchUsers'])->name('search-users');
    });

    Route::get('/emails/preview/{email?}', [EmailTestController::class, 'previewEmail'])->name('preview-email');
});

Route::get('/unsubscribe-from-emails', [AppController::class, 'unsubscribeFromEmails'])->name('unsubscribe-from-emails');
Route::get('/log-out', [LoginController::class, 'processLogOut'])->name('log-out');

//Google and Facebook callbacks
Route::get('/user/process-log-in-with-facebook', [LogInController::class, 'processLogInWithFacebook'])->name('process-log-in-with-facebook');
Route::get('/user/process-log-in-with-google', [LogInController::class, 'processLogInWithGoogle'])->name('process-log-in-with-google');
