<?php

use Illuminate\Support\Facades\Route;
use Stancl\Tenancy\Middleware\InitializeTenancyByDomain;
use Modules\Travel\Http\Controllers\TravelPlanController;
use Modules\Travel\Http\Controllers\TravelExpenseController;
use Modules\Travel\Http\Controllers\TravelMeetingController;
use Modules\Travel\Http\Controllers\TravelWorkflowController;
use Stancl\Tenancy\Middleware\PreventAccessFromCentralDomains;

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

if (!in_array(url('/'), config('tenancy.central_domains')) && config('app.mood') === 'Saas' && isModuleActive('Saas') ) {
    $middleware = [];

    if (!config('app.single_db')) {
        $middleware = [
            'web',
            InitializeTenancyByDomain::class,
            PreventAccessFromCentralDomains::class
        ];
    } else {
        $middleware = [
            'web',
            'check.company.subdomain'
        ];
    }
} else {
    $middleware = ['web'];
}

Route::middleware($middleware)->group(function () {
    Route::middleware(['xss', 'admin', 'TimeZone'])->group(function () {

        Route::prefix('travel/plans')->group(function () {
            Route::get('/', [TravelPlanController::class, 'index'])->name('travel.plans.index')->middleware('PermissionCheck:travel_plan_read');
            Route::get('create', [TravelPlanController::class, 'create'])->name('travel.plans.create')->middleware('PermissionCheck:travel_plan_create');
            Route::post('/', [TravelPlanController::class, 'store'])->name('travel.plans.store')->middleware('PermissionCheck:travel_plan_create');
            Route::get('{id}', [TravelPlanController::class, 'show'])->name('travel.plans.show')->middleware('PermissionCheck:travel_plan_read');
            Route::get('/edit/{id}', [TravelPlanController::class, 'edit'])->name('travel.plans.edit')->middleware('PermissionCheck:travel_plan_update');
            Route::put('{id}', [TravelPlanController::class, 'update'])->name('travel.plans.update')->middleware('PermissionCheck:travel_plan_update');
            Route::get('/delete/{id}', [TravelPlanController::class, 'destroy'])->name('travel.plans.destroy')->middleware('PermissionCheck:travel_plan_delete');
            Route::post('/{id}/approve-or-reject', [TravelPlanController::class, 'approveOrReject'])->name('travel.plans.approve-or-reject');
    
            
            Route::get('/{plan_id}/meetings', [TravelMeetingController::class, 'index'])->name('travel.plans.meetings.index')->middleware('PermissionCheck:travel_meeting_read');
            Route::get('/{plan_id}/meetings/create', [TravelMeetingController::class, 'create'])->name('travel.plans.meetings.create')->middleware('PermissionCheck:travel_meeting_create');
            Route::post('/{plan_id}/meetings', [TravelMeetingController::class, 'store'])->name('travel.plans.meetings.store')->middleware('PermissionCheck:travel_meeting_create');
            Route::get('/{plan_id}/meetings/{id}', [TravelMeetingController::class, 'show'])->name('travel.plans.meetings.show')->middleware('PermissionCheck:travel_meeting_read');
            Route::get('/{plan_id}/meetings/{id}/edit', [TravelMeetingController::class, 'edit'])->name('travel.plans.meetings.edit')->middleware('PermissionCheck:travel_meeting_update');
            Route::put('/{plan_id}/meetings/{id}', [TravelMeetingController::class, 'update'])->name('travel.plans.meetings.update')->middleware('PermissionCheck:travel_meeting_update');
            Route::get('/meetings/delete/{id}', [TravelMeetingController::class, 'destroy'])->name('travel.plans.meetings.destroy')->middleware('PermissionCheck:travel_meeting_delete');
    
            
            Route::get('/{plan_id}/expenses', [TravelExpenseController::class, 'index'])->name('travel.plans.expenses.index')->middleware('PermissionCheck:travel_expense_read');
            Route::get('/{plan_id}/expenses/create', [TravelExpenseController::class, 'create'])->name('travel.plans.expenses.create')->middleware('PermissionCheck:travel_expense_create');
            Route::post('/{plan_id}/expenses', [TravelExpenseController::class, 'store'])->name('travel.plans.expenses.store')->middleware('PermissionCheck:travel_expense_create');
            Route::get('/{plan_id}/expenses/{id}', [TravelExpenseController::class, 'show'])->name('travel.plans.expenses.show')->middleware('PermissionCheck:travel_expense_read');
            Route::get('/{plan_id}/expenses/{id}/edit', [TravelExpenseController::class, 'edit'])->name('travel.plans.expenses.edit')->middleware('PermissionCheck:travel_expense_update');
            Route::put('/{plan_id}/expenses/{id}/edit', [TravelExpenseController::class, 'update'])->name('travel.plans.expenses.update')->middleware('PermissionCheck:travel_expense_update');
            Route::get('/expenses/delete/{id}', [TravelExpenseController::class, 'destroy'])->name('travel.plans.expenses.destroy')->middleware('PermissionCheck:travel_expense_delete');
            Route::post('/expenses/{id}/approve', [TravelExpenseController::class, 'approve'])->name('travel.plans.expenses.approve')->middleware('PermissionCheck:travel_expense_approve');
        });
    
        Route::prefix('travel/workflows')->group(function () {
            Route::get('/', [TravelWorkflowController::class, 'index'])->name('travel.workflows.index')->middleware('PermissionCheck:travel_workflow_read');
            Route::get('create', [TravelWorkflowController::class, 'create'])->name('travel.workflows.create')->middleware('PermissionCheck:travel_workflow_create');
            Route::post('/', [TravelWorkflowController::class, 'store'])->name('travel.workflows.store')->middleware('PermissionCheck:travel_workflow_create');
            Route::get('/{id}', [TravelWorkflowController::class, 'show'])->name('travel.workflows.show')->middleware('PermissionCheck:travel_workflow_read');
            Route::get('{id}/edit', [TravelWorkflowController::class, 'edit'])->name('travel.workflows.edit')->middleware('PermissionCheck:travel_workflow_update');
            Route::put('{id}', [TravelWorkflowController::class, 'update'])->name('travel.workflows.update')->middleware('PermissionCheck:travel_workflow_update');
            Route::get('/delete/{id}', [TravelWorkflowController::class, 'destroy'])->name('travel.workflows.destroy')->middleware('PermissionCheck:travel_workflow_delete');
        });
    });

    Route::get('generate-travel-plan-signature/{id}', [TravelPlanController::class, 'generateTravelPlanSignature'])->name('generate-travel-plan-signature');
});