<?php

use Illuminate\Support\Facades\Route;
use App\Http\Controllers\UserController;
use App\Http\Controllers\ServiceController;
use App\Http\Controllers\OrderController;
use App\Http\Controllers\BillingController;
use App\Http\Controllers\Ticket;

use Illuminate\Foundation\Auth\EmailVerificationRequest;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;

/*
|--------------------------------------------------------------------------
| 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 () {
	if (Auth::check()) {
		return redirect('panel');
	}
    return view('home');
})->name('login');

Route::view('/panel', 'panel')->middleware([ 'auth', 'verified' ])->name('panel');

Route::view('/terms-and-policy', 'terms-and-policy');

//Verification routes
Route::get('/verify-email', function() {
	if (Auth::user()->email_verified_at) {
		abort(404);
	} else {
		return view('verify-email');
	}
})->middleware('auth')->name('verification.notice');

Route::get('/verify/{id}/{hash}', function (EmailVerificationRequest $request) {
	$request->fulfill();

	return redirect('/panel');
})->middleware(['auth', 'signed'])->name('verification.verify');

Route::post('/resend-verification', function (Request $request) {
	if (Auth::user()->email_verified_at) {
		abort(404);
	}

	$request->user()->sendEmailVerificationNotification();
	return back()->with('message', 'Verification link sent!');
})->middleware(['auth', 'throttle:6,1'])->name('verification.send');


//Creation and recovery
Route::post('/register', [UserController::class, 'create']);

Route::post('/forgot-password', [UserController::class,
	'forgotPassword'])->middleware('guest')->name('password.email');

Route::get('/reset-password/{token}', function ($token) {
	    return view('reset-password', ['token' => $token]);
})->middleware('guest')->name('password.reset');

Route::post('/reset-passowrd', [UserController::class,
	'resetPassword'])->middleware('guest');


Route::post('/login', [UserController::class,
	'login'])->middleware('guest');

Route::get('/logout', [UserController::class,
	'logout'])->middleware('auth');

//These should probably be grouped by name later
Route::get('/panel/user', function (Request $request) {
	return $request->user();
})->middleware([ 'auth', 'verified' ]);

Route::post('/panel/change-card', function (Request $request) {
	return App::make(BillingController::class)->changeDefaultCard($request->card);
})->middleware([ 'auth', 'verified' ]);

Route::post('/panel/delete-card', [BillingController::class,
	'deleteCard'])->middleware([ 'auth', 'verified' ]);

Route::get('/panel/orders', [UserController::class,
	'getOrders'])->middleware([ 'auth', 'verified' ]);

Route::get('/panel/services', [ServiceController::class,
	'getServices'])->middleware([ 'auth', 'verified' ]);

Route::post('/panel/change-name', [UserController::class,
	'changeName'])->middleware([ 'auth', 'verified' ]);

Route::post('/panel/change-email', [UserController::class,
	'changeEmail'])->middleware([ 'auth', 'verified' ]);

Route::post('/panel/change-password', [UserController::class,
	'changePassword'])->middleware([ 'auth', 'verified' ]);

Route::get('/reset-email', [UserController::class,
	'resetEmail'])->name('reset-email');

Route::post('/panel/orders', [OrderController::class,
	'newOrder'])->middleware([ 'auth', 'verified' ]);

Route::post('/panel/secret', [BillingController::class,
	'stripeSecret'])->middleware([ 'auth', 'verified' ]);

//Initiate a payeer payment
Route::post('/panel/payeer', [BillingController::class,
	'payeer'])->middleware([ 'auth', 'verified' ]);

//Initiate a Perfect Money payment
Route::post('/panel/pm', [BillingController::class,
	'pm'])->middleware([ 'auth', 'verified' ]);

Route::post('/panel/pm-complete', [BillingController::class,
	'completePM'])->middleware([ 'auth', 'verified' ]);

Route::post('/panel/pm-fail', [BillingController::class,
	'failPM'])->middleware([ 'auth', 'verified' ]);

Route::get('/panel/cards', [BillingController::class,
	'getCards'])->middleware([ 'auth', 'verified' ]);

Route::post('/panel/save-url', [OrderController::class,
	'changeURL'])->middleware([ 'auth', 'verified' ]);

//Stripe webhooks
Route::post('/hooks/charge',
	[BillingController::class, 'chargeEvent']);

//Payeer handler function
Route::post('/hooks/payeer-transaction',
	[BillingController::class, 'processPayeer']);

//PM handler function
Route::post('/hooks/pm-transaction',
	[BillingController::class, 'processPM']);

//Payment attempt is over
Route::get('/panel/clear-paying',
	[UserController::class, 'clearPaying'])->middleware(['auth', 'verified']);

Route::post('/panel/support',
	[Ticket::class, 'send'])->middleware(['auth', 'verified']);

Route::get('/panel/transaction-failed', function (Request $request) {
	return redirect('/panel#transaction-failed');
})->middleware(['auth', 'verified']);

Route::get('/panel/transaction-complete', function (Request $request) {
	return redirect('/panel#transaction-complete');
})->middleware(['auth', 'verified']);
