<?php

use Illuminate\Support\Facades\Route;
use Stancl\Tenancy\Middleware\InitializeTenancyByDomain;
use Modules\Break\Http\Controllers\Api\V11\BreakApiController;
use Stancl\Tenancy\Middleware\PreventAccessFromCentralDomains;
use Modules\Travel\Http\Controllers\Api\V11\TravelPlanApiController;
use Modules\Travel\Http\Controllers\Api\V11\TravelExpenseApiController;
use Modules\Travel\Http\Controllers\Api\V11\TravelMeetingApiController;
use Modules\Travel\Http\Controllers\Api\V11\TravelWorkflowApiController;

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

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

Route::middleware($middleware)->group(
    function () {
        Route::group(['middleware' => ['auth:sanctum', 'cors'], 'prefix' => 'V11'], function () {
            Route::controller(TravelPlanApiController::class)
                ->prefix('travel-plans')
                ->group(function () {
                    Route::get('/', 'index');
                    Route::post('/', 'store');
                    Route::post('/update/{id}', 'update');
                });

            Route::controller(TravelMeetingApiController::class)
                ->prefix('travel-meetings')
                ->group(function () {
                    Route::get('/', 'index');
                    Route::post('/', 'store');
                    Route::post('/update/{id}', 'update');
                });

            Route::controller(TravelExpenseApiController::class)
                ->prefix('travel-expenses')
                ->group(function () {
                    Route::get('/', 'index');
                    Route::get('/{id}/expense-logs', 'expenseLogs');
                    Route::get('/categories', 'categories');
                    Route::get('/modes', 'modes');
                    Route::post('/', 'store');
                    Route::post('/update/{id}', 'update');
                });

            Route::controller(TravelWorkflowApiController::class)
                ->prefix('travel-workflows')
                ->group(function () {
                    Route::get('/', 'index');
                    Route::post('/', 'store');
                    Route::post('/update/{id}', 'update');
                });
        });
    });