<?php

use App\Enums\UserType;
use App\Http\Controllers\Admin;
use Illuminate\Support\Facades\Route;

/*
|--------------------------------------------------------------------------
| Backoffice Routes
|--------------------------------------------------------------------------
|
*/
$resourceMethod = ['index', 'create', 'edit', 'store', 'update', 'destroy'];

Route::middleware(['auth', "role: " . implode("|", [UserType::Director, UserType::Manager])])
  ->prefix('admin')
  ->name('admin.')
  ->group(function () use ($resourceMethod) {
    Route::resource('/configuration', Admin\ConfigurationController::class)->only('index', 'store', 'destroy');

    Route::resource('/banks', Admin\BankController::class)->only([...$resourceMethod]);
    Route::resource('/user_banks', Admin\UserBankController::class)->only([...$resourceMethod]);
  });

Route::middleware(['auth', "role: " . implode("|", [UserType::Director, UserType::Manager]), 'activeDepartment'])
  ->prefix('admin')
  ->name('admin.')
  ->group(function () use ($resourceMethod) {
    Route::resource('/users', Admin\UsersController::class)->only([...$resourceMethod]);
  });

Route::middleware(['auth', "role: " . implode("|", [UserType::Director, UserType::Manager]), 'activeCompany'])
  ->prefix('admin')
  ->name('admin.')
  ->group(function () use ($resourceMethod) {
    Route::resource('/departments', Admin\DepartmentController::class)->only([...$resourceMethod]);
  });

Route::middleware(['auth', "role: " . implode("|", [UserType::Director])])
  ->prefix('admin')
  ->name('admin.')
  ->group(function () use ($resourceMethod) {
    Route::resource('/companies', Admin\CompanyController::class)->only([...$resourceMethod]);
  });

Route::middleware(['auth', "role: " . implode("|", [UserType::Director, UserType::Manager]), 'activeCompany'])
  ->prefix('admin')
  ->name('admin.')
  ->group(function () use ($resourceMethod) {
    Route::resource('/reports', Admin\ReportController::class)->only(['index']);
    Route::resource('/balances', Admin\BalanceController::class)->only([...$resourceMethod]);
    Route::resource('/code_configs', Admin\CodeConfigController::class)->only([...$resourceMethod]);

    Route::resource('/approval_logs', Admin\Module\ApprovalLogController::class)->only(['index']);
  });

Route::middleware(['auth', "role: " . implode("|", [UserType::Director, UserType::Manager, UserType::User]), 'activeCompany', 'activeDepartment'])
  ->prefix('admin')
  ->name('admin.')
  ->group(function () use ($resourceMethod) {
    Route::resource('/vendors', Admin\VendorController::class)->only([...$resourceMethod]);
    Route::resource('/categories', Admin\CategoryController::class)->only([...$resourceMethod]);

    Route::resource('/budgets', Admin\BudgetController::class)->only([...$resourceMethod]);
    Route::resource('/payments', Admin\PaymentController::class)->only(['index', 'update']);

    Route::prefix('payments/{payment_id}')
      ->name('payments.')
      ->group(function () use ($resourceMethod) {
        Route::resource('/payment_approvals', Admin\Module\PaymentApprovalController::class)->only(['index', 'store']);
      });

    Route::prefix('budgets/{budget_id}')
      ->name('budgets.')
      ->group(function () use ($resourceMethod) {
        Route::resource('/payment_requests', Admin\Module\PaymentRequestController::class)->only(['index', 'store']);
        Route::resource('/purchase_orders', Admin\Module\PurchaseOrderController::class)->only(['index', 'store']);
        Route::resource('/payment_details', Admin\Module\PaymentDetailController::class)->only([...$resourceMethod]);
        Route::resource('/budget_details', Admin\Module\BudgetDetailController::class)->only([...$resourceMethod]);
        Route::resource('/budget_approvals', Admin\Module\BudgetApprovalController::class)->only(['index', 'store']);
        Route::resource('/budget_revisions', Admin\Module\BudgetRevisionController::class)->only(['index', 'store']);
        Route::resource('/approval_logs', Admin\Module\ApprovalLogController::class)->only(['index']);

        Route::prefix('budget_details/{budget_detail_id}')
          ->name('budget_details.')
          ->group(function () use ($resourceMethod) {
            Route::resource('/transactions', Admin\Module\TransactionController::class)->only(['index', 'store']);
          });
      });
  });
