<?php

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

use App\Http\Controllers\{AdminController,
	Auth\LoginController,
	Auth\SocialLoginController,
	ChargeController,
	CheckoutController,
	HomeController,
	InvoiceController,
	OrderController,
	PaymentController,
	PaypalController,
	ProductController,
	SearchController,
	ServiceController,
	SupplierController,
	UserController};
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
]);
// Auth::routes doesnt provide a get /logout path, so we'll specify it here
Route::get('logout',[LoginController::class,'logout']);

// 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']);

// Paypal paths
Route::controller(PaypalController::class)
	->prefix('pay/paypal')
	->group(function() {
		Route::get('authorise','authorise');
		Route::get('cancel','cancel');
		Route::get('capture','capture');
	});

// Account linking to OPENID host
Route::controller(SocialLoginController::class)
	->prefix('auth')
	->group(function() {
		Route::get('{socialProvider}','redirectToProvider');
		Route::get('{socialProvider}/callback','handleProviderCallback');
		Route::get('{socialProvider}/token','handleBearerTokenCallback');
		Route::get('{socialProvider}/link','link');
		Route::post('{socialProvider}/linkcomplete','linkcomplete');
	});

// User Switch
Route::controller(SwitchUserController::class)
	->prefix('switch')
	->middleware('auth')
	->group(function() {
		Route::get('stop','switch_stop');
		Route::get('start/{user}','switch_start')
			->middleware(['role:reseller','can:assume,user']);
	});

// 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.setup');
	Route::post('setup',[AdminController::class,'setup']);

	// Checkout Setup (Payments)
	Route::view('checkout','theme.backend.adminlte.checkout.choose');
	Route::view('checkout/new','theme.backend.adminlte.checkout.view');
	Route::view('checkout/{co}','theme.backend.adminlte.checkout.view')
		->where('co','[0-9]+');
	Route::post('checkout/{o?}',[CheckoutController::class,'addedit'])
		->where('o','[0-9]+');

	// Product Setup
	Route::view('product','theme.backend.adminlte.product.home');
	Route::view('product/{pdo}','theme.backend.adminlte.product.details',['breadcrumb'=>['Products'=>'a/product']])
		->where('pdo','[0-9]+')
		->name('product');
	Route::post('product/{o?}',[ProductController::class,'addedit'])
		->where('o','[0-9]+');

	Route::get('supplied_products',[ProductController::class,'api_supplied_products']);

	// Supplier Setup
	Route::view('supplier','theme.backend.adminlte.supplier.home');
	Route::view('supplier/cost/new/{spo}','theme.backend.adminlte.supplier.cost.add')
		->where('spo','[0-9]+');
	Route::post('supplier/cost/new/{spo}',[SupplierController::class,'cost_submit'])
		->where('spo','[0-9]+');
	Route::view('supplier/cost/{cso}','theme.backend.adminlte.supplier.cost.view')
		->where('cso','[0-9]+');
	Route::view('supplier/details/{spo}','theme.backend.adminlte.supplier.details')
		->where('spo','[0-9]+')
		->name('supplier.details');
	Route::post('supplier/details/{o?}',[SupplierController::class,'addedit'])
		->where('o','[0-9]+');
	Route::view('supplier/product/new','theme.backend.adminlte.supplier.product.addedit');
	Route::post('supplier/product/new',[SupplierController::class,'product_new']);
	Route::view('supplier/product/{spo}/{id}/{type}','theme.backend.adminlte.supplier.product.addedit')
		->where('spo','[0-9]+')
		->where('id','[0-9]+')
		->whereIn('type',Supplier::offeringTypeKeys()->toArray())
		->name('supplier.product.type');
	Route::post('supplier/product/{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::view('report/accounts','theme.backend.adminlte.account.report');
	Route::view('report/products','theme.backend.adminlte.product.report');
	Route::view('report/services','theme.backend.adminlte.service.report');

	// 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]+');
});

// Our Reseller Routes
Route::group(['middleware'=>['auth','role:reseller'],'prefix'=>'r'],function() {
	// Reseller Reports
	Route::group(['prefix'=>'report'],function() {
		Route::view('charge/pending','theme.backend.adminlte.charge.pending');
		Route::get('domain',[ServiceController::class,'domain_list']);
		Route::get('email',[ServiceController::class,'email_list']);
		Route::get('hosting',[ServiceController::class,'hosting_list']);
	});

	Route::post('account/invoices',[InvoiceController::class,'api_account_invoices']);

	// Charges
	Route::post('charge/addedit',[ChargeController::class,'addedit']);
	Route::post('charge/delete/{o}',[ChargeController::class,'delete'])
		->where('o','[0-9]+');
	Route::post('charge/edit',[ChargeController::class,'edit']);

	// Payments
	Route::view('payment/new','theme.backend.adminlte.payment.view');
	Route::view('payment/{po}','theme.backend.adminlte.payment.view')
		->middleware('can:update,po')
		->where('po','[0-9]+');
	Route::post('payment/{o?}',[PaymentController::class,'addedit'])
		->where('o','[0-9]+');
	Route::view('payment/unapplied','theme.backend.adminlte.payment.unapplied');

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

// Our User Routes
Route::group(['middleware'=>['auth'],'prefix'=>'u'],function() {
	Route::get('home',[HomeController::class,'home']);
	Route::get('home/{o}',[HomeController::class,'home'])
		->middleware('can:view,o')
		->where('o','[0-9]+');

	Route::view('checkout/cart','theme.backend.adminlte.checkout.cart');
	Route::get('checkout/cart/{o}',[CheckoutController::class,'cart_invoice'])
		->middleware('can:view,o')
		->where('o','[0-9]+');
	Route::post('checkout/cart/remove',[CheckoutController::class,'cart_remove']);
	Route::post('checkout/fee',[CheckoutController::class,'fee']);
	Route::post('checkout/pay',[CheckoutController::class,'pay']);

	Route::get('invoice/{o}',[InvoiceController::class,'view'])
		->middleware('can:view,o')
		->where('o','[0-9]+');
	Route::get('invoice/{o}/pdf',[InvoiceController::class,'pdf'])
		->middleware('can:view,o')
		->where('o','[0-9]+');

	Route::get('service/{o}',[ServiceController::class,'home'])
		->middleware('can:view,o')
		->where('o','[0-9]+');
	Route::view('service/{so}/cancel-request','theme.backend.adminlte.service.cancel_request')
		->where('so','[0-9]+');
	Route::post('service/{o}/cancel-request',[ServiceController::class,'cancel_request'])
		->middleware('can:progress,o,"cancel-request"')
		->where('o','[0-9]+');
	Route::view('service/{so}/change-request','theme.backend.adminlte.service.change_request')
		->where('so','[0-9]+');
	Route::post('service/{o}/change-request',[ServiceController::class,'change_request'])
		->middleware('can:progress,o,"change-request"')
		->where('o','[0-9]+');
	// @todo This shouldnt be a user privilege.
	Route::match(['get','post'],'service/{o}/change-pending',[ServiceController::class,'change_pending'])
		->middleware('can:progress,o,"change-pending"')
		->where('o','[0-9]+');
	Route::get('service/{o}/change/{status}',[ServiceController::class,'change'])
		->middleware('can:progress,o,status')
		->where('o','[0-9]+');

	// 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::view('product_order/{pdo}','theme.frontend.metronic.order.widget.order');
Route::view('product_info/{pdo}','theme.frontend.metronic.order.widget.info');