<?php

use Illuminate\Support\Facades\Route;
use Stancl\Tenancy\Middleware\InitializeTenancyByDomain;
use Stancl\Tenancy\Middleware\PreventAccessFromCentralDomains;
use Modules\SpecialAttendance\Http\Controllers\DeductionController;

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(['demo.mode'])->group(function () {
        Route::middleware(['xss', 'TimeZone', 'MaintenanceMode', 'admin'])->group(function () {

            Route::prefix('admin/special-attendance')->group(function () {
                Route::get('/', 'SpecialAttendanceController@index');

                Route::controller(DeductionController::class)->middleware('FeatureCheck:attendance_deduction')->prefix('deduction')->group(function () {
                    Route::any('/', 'index')->name('deduction.index')->middleware('PermissionCheck:deduction_generate_read');
                    Route::post('/store', 'store')->name('deduction.store')->middleware('PermissionCheck:deduction_generate_create');
                    Route::get('create-modal', 'createModal')->name('deduction.create_modal')->middleware('PermissionCheck:deduction_generate_create');
                    Route::get('/{id}', 'show')->name('deduction.show')->middleware(['PermissionCheck:deduction_generate_read']);
                    Route::post('consider/{id}', 'consider')->name('deduction.consider')->middleware(['PermissionCheck:deduction_generate_update']);
                    Route::post('/appeal/{id}', 'appeal')->name('deduction.appeal')->middleware(['PermissionCheck:deduction_generate_read']);
                });
            });

            Route::prefix('admin/duty')->group(function () {
                Route::get('calendars', 'DutyCalendarController@calendar')->name('duty.calendar.index')->middleware('PermissionCheck:duty_calendar_read');
                Route::get('calendars/event', 'DutyCalendarController@calendarEvent')->name('duty.calendar.event')->middleware('PermissionCheck:duty_calendar_read');
                Route::post('calendars/store', 'DutyCalendarController@calendarStore')->name('duty.calendar.store')->middleware('PermissionCheck:duty_calendar_create');
                Route::post('calendars/update', 'DutyCalendarController@calendarUpdate')->name('duty.calendar.update')->middleware('PermissionCheck:duty_calendar_update');
            });
        });
    });
});
