<?php

use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Route;
use Leenooks\Controllers\SwitchUserController;

use App\Http\Controllers\{AdminController,
	Auth\LoginController,
	Auth\SocialLoginController,
	CheckoutController,
	HomeController,
	InvoiceController,
	OrderController,
	PaypalController,
	ProductController,
	SearchController,
	ServiceController,
	SupplierController,
	UserController,
	Wholesale\ReportController};
use App\Models\Supplier;

/*
|--------------------------------------------------------------------------
| 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!
|
*/

// Global Routes
Auth::routes([
	'login'    => true,
	'logout'   => true,
	'register' => false,
	'reset'    => true,   // for resetting passwords
	'confirm'  => false,  // for additional password confirmations
	'verify'   => false,  // for email verification
]);
Route::get('logout',[LoginController::class,'logout'])
	->name('logout-get');

// Frontend Routes (Non-Authed Users)
Route::view('/','theme.frontend.metronic.welcome.home');

// Logged in users home
Route::redirect('home','/u/home');
Route::redirect('passkey/loggedin','/u/home');

Route::get('search',[SearchController::class,'search']);

Route::get('pay/paypal/authorise',[PaypalController::class,'authorise']);
Route::get('pay/paypal/cancel',[PaypalController::class,'cancel']);
Route::get('pay/paypal/capture',[PaypalController::class,'capture']);

// Account linking to OPENID host
Route::get('auth/{socialProvider}',[SocialLoginController::class,'redirectToProvider']);
Route::get('auth/{socialProvider}/callback',[SocialLoginController::class,'handleProviderCallback']);
Route::get('auth/{socialProvider}/token',[SocialLoginController::class,'handleBearerTokenCallback']);
Route::get('auth/{socialProvider}/link',[SocialLoginController::class,'link']);
Route::post('auth/{socialProvider}/linkcomplete',[SocialLoginController::class,'linkcomplete']);

// Return from user switch
Route::get('admin/switch/stop',[SwitchUserController::class,'switch_stop'])
	->middleware('auth')
	->name('switch.stop');

// Our Admin Routes - for wholesalers
Route::group(['middleware'=>['auth','role:wholesaler'],'prefix'=>'a'],function() {
	// Linking supplier to account
	Route::post('account/supplier/add/{o}',[UserController::class,'supplier_addedit'])
		->where('o','[0-9]+');
	Route::get('account/supplier/delete/{o}/{so}',[UserController::class,'supplier_delete'])
		->where('o','[0-9]+')
		->where('so','[0-9]+');

	// Site Setup
	Route::view('setup','theme.backend.adminlte.a.setup');
	Route::post('setup',[AdminController::class,'setup']);

	// Checkout Setup (Payments)
	Route::get('checkout',[CheckoutController::class,'home']);
	Route::get('checkout/{o?}',[CheckoutController::class,'view'])
		->where('o','[0-9]+');
	Route::post('checkout/{o?}',[CheckoutController::class,'addedit'])
		->where('o','[0-9]+');

	// Product Setup
	Route::match(['get'],'product',[ProductController::class,'home']);
	Route::get('product/details/{o?}',[ProductController::class,'details'])
		->where('o','[0-9]+');
	Route::post('product/details/{o?}',[ProductController::class,'details_addedit'])
		->where('o','[0-9]+');

	// Supplier Setup
	Route::get('supplier',[SupplierController::class,'admin_home']);
	Route::get('supplier/cost/new/{o}',[SupplierController::class,'cost_add']);
	Route::post('supplier/cost/new/{o}',[SupplierController::class,'cost_submit']);
	Route::get('supplier/cost/{o}',[SupplierController::class,'cost'])
		->where('o','[0-9]+');
	Route::get('supplier/details/{o?}',[SupplierController::class,'view'])
		->where('o','[0-9]+');
	Route::post('supplier/details/{o?}',[SupplierController::class,'addedit'])
		->where('o','[0-9]+');
	Route::get('supplier/product/add',[SupplierController::class,'product_add']);
	Route::get('supplier/product/addedit/{o}/{oo}/{type}',[SupplierController::class,'product_view'])
		->where('o','[0-9]+')
		->where('oo','[0-9]+')
		->whereIn('type',Supplier::offeringTypeKeys()->toArray());
	Route::post('supplier/product/addedit/{o}/{oo}/{type}',[SupplierController::class,'product_addedit'])
		->where('o','[0-9]+')
		->where('oo','[0-9]+')
		->whereIn('type',Supplier::offeringTypeKeys()->toArray());
	Route::post('supplier/product/view/{type}/{oo?}',[SupplierController::class,'product_view_type'])
		->whereIn('type',Supplier::offeringTypeKeys()->toArray())
		->where('oo','[0-9]+');

	Route::get('report/accounts',[ReportController::class,'accounts']);
	Route::get('report/products',[ReportController::class,'products']);
	Route::get('report/services',[ReportController::class,'services']);

	// Payments - @todo This should probably go to resellers
	Route::match(['get','post'],'payment/addedit/{o?}',[AdminController::class,'pay_addedit']);
	Route::get('payment/unapplied',[AdminController::class,'pay_unapplied']);

	// Services
	// @todo This should probably go to resellers - implement a change audit log first
	Route::post('service/update/{o}',[ServiceController::class,'update'])
		->where('o','[0-9]+');

//@deprecated
//	Route::get('service/{o}','AdminHomeController@service');
//	Route::post('service/{o}','AdminHomeController@service_update');
//	Route::get('accounting/connect','AccountingController@connect');
});

// Our Reseller Routes
Route::group(['middleware'=>['auth','role:reseller'],'prefix'=>'r'],function() {
	// Enable user switch
	Route::get('switch/start/{user}',[SwitchUserController::class,'switch_start'])
		->middleware('can:assume,user')
		->name('switch.start');

	// Reseller Reports
	Route::group(['middleware'=>['auth','role:reseller'],'prefix'=>'report'],function() {
		Route::get('domain',[ServiceController::class,'domain_list']);
		Route::get('email',[ServiceController::class,'email_list']);
		Route::get('hosting',[ServiceController::class,'hosting_list']);
	});

	// Charges
	Route::post('charge/addedit',[ServiceController::class,'charge_addedit']);
	Route::post('charge/edit',[ServiceController::class,'charge_edit']);

	// Reseller API calls
	Route::post('service_change_charges/{o}',[ServiceController::class,'service_change_charges_display'])
		->where('o','[0-9]+');

	// Charges
	Route::view('report/charge/pending','theme.backend.adminlte.charge.pending');
});

// Our User Routes
Route::group(['middleware'=>['auth'],'prefix'=>'u'],function() {
	Route::get('home',[HomeController::class,'home']);
	Route::get('home/{o}',[HomeController::class,'home'])
		->where('o','[0-9]+')
		->middleware('can:view,o');
//	Route::get('account/{o}/invoice','User\AccountController@view_invoice_next')
//		->where('o','[0-9]+')
//		->middleware('can:view,o');
	Route::post('checkout/pay',[CheckoutController::class,'pay']);
	Route::get('invoice/{o}',[InvoiceController::class,'view'])
		->where('o','[0-9]+')
		->middleware('can:view,o');
	Route::get('invoice/{o}/pdf',[InvoiceController::class,'pdf'])
		->where('o','[0-9]+')
		->middleware('can:view,o');
	Route::get('invoice/cart',[CheckoutController::class,'cart_invoice']);
	Route::get('invoice/cart/{o}',[CheckoutController::class,'cart_invoice'])
		->where('o','[0-9]+')
		->middleware('can:view,o');
	Route::get('service/{o}',[ServiceController::class,'home'])
		->where('o','[0-9]+')
		->middleware('can:view,o');
	Route::match(['get','post'],'service/{o}/cancel-request',[ServiceController::class,'cancel_request'])
		->where('o','[0-9]+')
		->middleware('can:progress,o,"cancel-request"');
	Route::match(['get','post'],'service/{o}/change-request',[ServiceController::class,'change_request'])
		->where('o','[0-9]+')
		->middleware('can:progress,o,"change-request"');
	// @todo This shouldnt be a user privilege.
	Route::match(['get','post'],'service/{o}/change-pending',[ServiceController::class,'change_pending'])
		->where('o','[0-9]+')
		->middleware('can:progress,o,"change-pending"');
	Route::get('service/{o}/change/{status}',[ServiceController::class,'change'])
		->where('o','[0-9]+')
		->middleware('can:progress,o,status');

	// User settings
	Route::view('settings','theme.backend.adminlte.user.settings');
	Route::post('settings/{o}',[UserController::class,'edit']);
});

// Doorman Code Routes
Route::group(['prefix'=>'u'],function() {
	Route::get('invoice/{o}/email/{code}',[InvoiceController::class,'view'])
		->where('o','[0-9]+')
		->where('code','[0-9A-Z]{6}');
});

// Frontend
Route::view('order','theme.frontend.metronic.order.home');
Route::post('order',[OrderController::class,'submit']);

Route::get('product_order/{o}',[OrderController::class,'product_order']);
Route::get('product_info/{o}',[OrderController::class,'product_info']);