<?php

use App\Http\Controllers\Api\Admin\BoardController;
use App\Http\Controllers\Api\Admin\CategoryApiController;
use App\Http\Controllers\Api\Admin\CustomerCenterController;
use App\Http\Controllers\Api\Admin\DailyLogController;
use App\Http\Controllers\Api\Admin\DepartmentController;
use App\Http\Controllers\Api\Admin\DocumentApiController;
use App\Http\Controllers\Api\Admin\DocumentTemplateApiController;
use App\Http\Controllers\Api\Admin\GlobalCategoryApiController;
use App\Http\Controllers\Api\Admin\GlobalMenuController;
use App\Http\Controllers\Api\Admin\ItemFieldController;
use App\Http\Controllers\Api\Admin\MeetingLogController;
use App\Http\Controllers\Api\Admin\MenuController;
use App\Http\Controllers\Api\Admin\NumberingRuleController;
use App\Http\Controllers\Api\Admin\PermissionController;
use App\Http\Controllers\Api\Admin\ProjectManagement\ImportController as PmImportController;
use App\Http\Controllers\Api\Admin\ProjectManagement\IssueController as PmIssueController;
use App\Http\Controllers\Api\Admin\ProjectManagement\ProjectController as PmProjectController;
use App\Http\Controllers\Api\Admin\ProjectManagement\TaskController as PmTaskController;
use App\Http\Controllers\Api\Admin\Quote\QuoteFormulaCategoryController;
use App\Http\Controllers\Api\Admin\Quote\QuoteFormulaController;
use App\Http\Controllers\Api\Admin\RoleController;
use App\Http\Controllers\Api\Admin\RolePermissionController;
use App\Http\Controllers\Api\Admin\TenantController;
use App\Http\Controllers\Api\Admin\UserController;
use App\Http\Controllers\Api\GeminiController;
use Illuminate\Support\Facades\Route;

/*
|--------------------------------------------------------------------------
| API Routes
|--------------------------------------------------------------------------
|
| HTMX 요청 시 HTML 반환, 일반 요청 시 JSON 반환
| - auth: 기본 인증 확인
| - hq.member: 본사(HQ) 테넌트 소속 확인
| - super.admin: 슈퍼관리자 전용 (복구, 영구삭제)
*/

Route::middleware(['web', 'auth', 'hq.member'])->prefix('admin')->name('api.admin.')->group(function () {

    // 계좌 관리 API
    Route::prefix('bank-accounts')->name('bank-accounts.')->group(function () {
        // 고정 경로
        Route::get('/all', [\App\Http\Controllers\Api\Admin\BankAccountController::class, 'all'])->name('all');
        Route::get('/summary', [\App\Http\Controllers\Api\Admin\BankAccountController::class, 'summary'])->name('summary');
        Route::post('/bulk-delete', [\App\Http\Controllers\Api\Admin\BankAccountController::class, 'bulkDelete'])->name('bulkDelete');
        Route::post('/bulk-restore', [\App\Http\Controllers\Api\Admin\BankAccountController::class, 'bulkRestore'])->name('bulkRestore');
        Route::middleware('super.admin')->post('/bulk-force-delete', [\App\Http\Controllers\Api\Admin\BankAccountController::class, 'bulkForceDelete'])->name('bulkForceDelete');

        // 기본 CRUD
        Route::get('/', [\App\Http\Controllers\Api\Admin\BankAccountController::class, 'index'])->name('index');
        Route::post('/', [\App\Http\Controllers\Api\Admin\BankAccountController::class, 'store'])->name('store');
        Route::get('/{id}', [\App\Http\Controllers\Api\Admin\BankAccountController::class, 'show'])->name('show');
        Route::put('/{id}', [\App\Http\Controllers\Api\Admin\BankAccountController::class, 'update'])->name('update');
        Route::delete('/{id}', [\App\Http\Controllers\Api\Admin\BankAccountController::class, 'destroy'])->name('destroy');

        // 복원 (일반관리자 가능)
        Route::post('/{id}/restore', [\App\Http\Controllers\Api\Admin\BankAccountController::class, 'restore'])->name('restore');

        // 슈퍼관리자 전용 액션 (영구삭제)
        Route::middleware('super.admin')->group(function () {
            Route::delete('/{id}/force', [\App\Http\Controllers\Api\Admin\BankAccountController::class, 'forceDelete'])->name('force-delete');
        });

        // 추가 액션
        Route::post('/{id}/toggle-active', [\App\Http\Controllers\Api\Admin\BankAccountController::class, 'toggleActive'])->name('toggleActive');

        // 거래내역
        Route::get('/{id}/transactions', [\App\Http\Controllers\Api\Admin\BankAccountController::class, 'transactions'])->name('transactions');
    });

    // 자금계획일정 API
    Route::prefix('fund-schedules')->name('fund-schedules.')->group(function () {
        // 고정 경로
        Route::get('/calendar', [\App\Http\Controllers\Api\Admin\FundScheduleController::class, 'calendar'])->name('calendar');
        Route::get('/summary', [\App\Http\Controllers\Api\Admin\FundScheduleController::class, 'summary'])->name('summary');
        Route::get('/upcoming', [\App\Http\Controllers\Api\Admin\FundScheduleController::class, 'upcoming'])->name('upcoming');
        Route::post('/copy', [\App\Http\Controllers\Api\Admin\FundScheduleController::class, 'copy'])->name('copy');

        // 기본 CRUD
        Route::get('/', [\App\Http\Controllers\Api\Admin\FundScheduleController::class, 'index'])->name('index');
        Route::post('/', [\App\Http\Controllers\Api\Admin\FundScheduleController::class, 'store'])->name('store');
        Route::get('/{id}', [\App\Http\Controllers\Api\Admin\FundScheduleController::class, 'show'])->name('show');
        Route::put('/{id}', [\App\Http\Controllers\Api\Admin\FundScheduleController::class, 'update'])->name('update');
        Route::delete('/{id}', [\App\Http\Controllers\Api\Admin\FundScheduleController::class, 'destroy'])->name('destroy');

        // 상태 변경
        Route::patch('/{id}/status', [\App\Http\Controllers\Api\Admin\FundScheduleController::class, 'updateStatus'])->name('status');
    });

    // 바로빌 설정 API
    Route::prefix('barobill/configs')->name('barobill.configs.')->group(function () {
        Route::get('/', [\App\Http\Controllers\Api\Admin\Barobill\BarobillConfigController::class, 'index'])->name('index');
        Route::post('/', [\App\Http\Controllers\Api\Admin\Barobill\BarobillConfigController::class, 'store'])->name('store');
        Route::get('/{id}', [\App\Http\Controllers\Api\Admin\Barobill\BarobillConfigController::class, 'show'])->name('show');
        Route::put('/{id}', [\App\Http\Controllers\Api\Admin\Barobill\BarobillConfigController::class, 'update'])->name('update');
        Route::delete('/{id}', [\App\Http\Controllers\Api\Admin\Barobill\BarobillConfigController::class, 'destroy'])->name('destroy');
        Route::post('/{id}/toggle-active', [\App\Http\Controllers\Api\Admin\Barobill\BarobillConfigController::class, 'toggleActive'])->name('toggle-active');
    });

    // 바로빌 설정 API (회원사용)
    Route::prefix('barobill/settings')->name('barobill.settings.')->group(function () {
        Route::get('/', [\App\Http\Controllers\Api\Admin\Barobill\BarobillSettingController::class, 'show'])->name('show');
        Route::post('/', [\App\Http\Controllers\Api\Admin\Barobill\BarobillSettingController::class, 'store'])->name('store');
        Route::post('/service', [\App\Http\Controllers\Api\Admin\Barobill\BarobillSettingController::class, 'updateService'])->name('update-service');
        Route::get('/check/{service}', [\App\Http\Controllers\Api\Admin\Barobill\BarobillSettingController::class, 'checkService'])->name('check');
    });

    // 바로빌 회원사 관리 API
    Route::prefix('barobill/members')->name('barobill.members.')->group(function () {
        // 통계
        Route::get('/stats', [\App\Http\Controllers\Api\Admin\Barobill\BarobillMemberController::class, 'stats'])->name('stats');
        // 서비스 코드 목록 (카드사/은행)
        Route::get('/service-codes', [\App\Http\Controllers\Api\Admin\Barobill\BarobillMemberController::class, 'getServiceCodes'])->name('service-codes');

        // 기본 CRUD
        Route::get('/', [\App\Http\Controllers\Api\Admin\Barobill\BarobillMemberController::class, 'index'])->name('index');
        Route::post('/', [\App\Http\Controllers\Api\Admin\Barobill\BarobillMemberController::class, 'store'])->name('store');
        Route::get('/{id}', [\App\Http\Controllers\Api\Admin\Barobill\BarobillMemberController::class, 'show'])->name('show');
        Route::put('/{id}', [\App\Http\Controllers\Api\Admin\Barobill\BarobillMemberController::class, 'update'])->name('update');
        Route::delete('/{id}', [\App\Http\Controllers\Api\Admin\Barobill\BarobillMemberController::class, 'destroy'])->name('destroy');

        // ==========================================
        // 계좌 관련 URL 조회
        // ==========================================
        Route::post('/{id}/bank-account-url', [\App\Http\Controllers\Api\Admin\Barobill\BarobillMemberController::class, 'getBankAccountUrl'])->name('bank-account-url');
        Route::post('/{id}/bank-account-manage-url', [\App\Http\Controllers\Api\Admin\Barobill\BarobillMemberController::class, 'getBankAccountManageUrl'])->name('bank-account-manage-url');
        Route::post('/{id}/bank-account-log-url', [\App\Http\Controllers\Api\Admin\Barobill\BarobillMemberController::class, 'getBankAccountLogUrl'])->name('bank-account-log-url');
        Route::get('/{id}/bank-accounts', [\App\Http\Controllers\Api\Admin\Barobill\BarobillMemberController::class, 'getBankAccounts'])->name('bank-accounts');

        // ==========================================
        // 카드 관련 URL 조회
        // ==========================================
        Route::post('/{id}/card-url', [\App\Http\Controllers\Api\Admin\Barobill\BarobillMemberController::class, 'getCardUrl'])->name('card-url');
        Route::post('/{id}/card-manage-url', [\App\Http\Controllers\Api\Admin\Barobill\BarobillMemberController::class, 'getCardManageUrl'])->name('card-manage-url');
        Route::post('/{id}/card-log-url', [\App\Http\Controllers\Api\Admin\Barobill\BarobillMemberController::class, 'getCardLogUrl'])->name('card-log-url');
        Route::get('/{id}/cards', [\App\Http\Controllers\Api\Admin\Barobill\BarobillMemberController::class, 'getCards'])->name('cards');

        // ==========================================
        // 전자세금계산서 관련 URL 조회
        // ==========================================
        Route::post('/{id}/certificate-url', [\App\Http\Controllers\Api\Admin\Barobill\BarobillMemberController::class, 'getCertificateUrl'])->name('certificate-url');
        Route::post('/{id}/tax-invoice-url', [\App\Http\Controllers\Api\Admin\Barobill\BarobillMemberController::class, 'getTaxInvoiceUrl'])->name('tax-invoice-url');
        Route::post('/{id}/tax-invoice-list-url', [\App\Http\Controllers\Api\Admin\Barobill\BarobillMemberController::class, 'getTaxInvoiceListUrl'])->name('tax-invoice-list-url');

        // ==========================================
        // 공통 조회
        // ==========================================
        Route::post('/{id}/cash-charge-url', [\App\Http\Controllers\Api\Admin\Barobill\BarobillMemberController::class, 'getCashChargeUrl'])->name('cash-charge-url');
        Route::get('/{id}/certificate-status', [\App\Http\Controllers\Api\Admin\Barobill\BarobillMemberController::class, 'getCertificateStatus'])->name('certificate-status');
        Route::get('/{id}/balance', [\App\Http\Controllers\Api\Admin\Barobill\BarobillMemberController::class, 'getBalance'])->name('balance');

        // ==========================================
        // 서버 모드 관리 (회원사별)
        // ==========================================
        Route::get('/{id}/server-mode', [\App\Http\Controllers\Api\Admin\Barobill\BarobillMemberController::class, 'getServerMode'])->name('server-mode.get');
        Route::post('/{id}/server-mode', [\App\Http\Controllers\Api\Admin\Barobill\BarobillMemberController::class, 'updateServerMode'])->name('server-mode.update');
    });

    // 바로빌 사용량조회 API
    Route::prefix('barobill/usage')->name('barobill.usage.')->group(function () {
        // 고정 경로 먼저
        Route::get('/stats', [\App\Http\Controllers\Api\Admin\Barobill\BarobillUsageController::class, 'stats'])->name('stats');
        Route::get('/export', [\App\Http\Controllers\Api\Admin\Barobill\BarobillUsageController::class, 'export'])->name('export');

        // 목록 조회
        Route::get('/', [\App\Http\Controllers\Api\Admin\Barobill\BarobillUsageController::class, 'index'])->name('index');

        // 회원사별 상세
        Route::get('/{memberId}', [\App\Http\Controllers\Api\Admin\Barobill\BarobillUsageController::class, 'show'])->name('show');
    });

    // 바로빌 과금관리 API
    Route::prefix('barobill/billing')->name('barobill.billing.')->group(function () {
        // 구독 관리
        Route::get('/subscriptions', [\App\Http\Controllers\Api\Admin\Barobill\BarobillBillingController::class, 'subscriptions'])->name('subscriptions');
        Route::post('/subscriptions', [\App\Http\Controllers\Api\Admin\Barobill\BarobillBillingController::class, 'saveSubscription'])->name('subscriptions.save');
        Route::delete('/subscriptions/{id}', [\App\Http\Controllers\Api\Admin\Barobill\BarobillBillingController::class, 'cancelSubscription'])->name('subscriptions.cancel');
        Route::get('/subscriptions/member/{memberId}', [\App\Http\Controllers\Api\Admin\Barobill\BarobillBillingController::class, 'memberSubscriptions'])->name('subscriptions.member');

        // 과금 현황
        Route::get('/list', [\App\Http\Controllers\Api\Admin\Barobill\BarobillBillingController::class, 'billingList'])->name('list');
        Route::get('/stats', [\App\Http\Controllers\Api\Admin\Barobill\BarobillBillingController::class, 'billingStats'])->name('stats');
        Route::get('/member/{memberId}', [\App\Http\Controllers\Api\Admin\Barobill\BarobillBillingController::class, 'memberBilling'])->name('member');
        Route::get('/export', [\App\Http\Controllers\Api\Admin\Barobill\BarobillBillingController::class, 'export'])->name('export');

        // 과금 처리
        Route::post('/process', [\App\Http\Controllers\Api\Admin\Barobill\BarobillBillingController::class, 'processBilling'])->name('process');

        // 연간 추이
        Route::get('/yearly-trend', [\App\Http\Controllers\Api\Admin\Barobill\BarobillBillingController::class, 'yearlyTrend'])->name('yearly-trend');

        // 과금 정책 관리
        Route::get('/pricing-policies', [\App\Http\Controllers\Api\Admin\Barobill\BarobillBillingController::class, 'pricingPolicies'])->name('pricing-policies');
        Route::get('/pricing-policies/{id}', [\App\Http\Controllers\Api\Admin\Barobill\BarobillBillingController::class, 'getPricingPolicy'])->name('pricing-policies.show');
        Route::put('/pricing-policies/{id}', [\App\Http\Controllers\Api\Admin\Barobill\BarobillBillingController::class, 'updatePricingPolicy'])->name('pricing-policies.update');
    });

    // 바로빌 카카오톡 API
    Route::prefix('barobill/kakaotalk')->name('barobill.kakaotalk.')->group(function () {
        // 채널 관리
        Route::get('/channels', [\App\Http\Controllers\Api\Admin\Barobill\BarobillKakaotalkController::class, 'getChannels'])->name('channels');
        Route::get('/channels/management-url', [\App\Http\Controllers\Api\Admin\Barobill\BarobillKakaotalkController::class, 'getChannelManagementUrl'])->name('channels.management-url');

        // 템플릿 관리
        Route::get('/templates', [\App\Http\Controllers\Api\Admin\Barobill\BarobillKakaotalkController::class, 'getTemplates'])->name('templates');
        Route::get('/templates/management-url', [\App\Http\Controllers\Api\Admin\Barobill\BarobillKakaotalkController::class, 'getTemplateManagementUrl'])->name('templates.management-url');

        // 알림톡 발송
        Route::post('/send/alimtalk', [\App\Http\Controllers\Api\Admin\Barobill\BarobillKakaotalkController::class, 'sendAlimtalk'])->name('send.alimtalk');
        Route::post('/send/alimtalk-bulk', [\App\Http\Controllers\Api\Admin\Barobill\BarobillKakaotalkController::class, 'sendAlimtalkBulk'])->name('send.alimtalk-bulk');

        // 친구톡 발송
        Route::post('/send/friendtalk', [\App\Http\Controllers\Api\Admin\Barobill\BarobillKakaotalkController::class, 'sendFriendtalk'])->name('send.friendtalk');
        Route::post('/send/friendtalk-image', [\App\Http\Controllers\Api\Admin\Barobill\BarobillKakaotalkController::class, 'sendFriendtalkImage'])->name('send.friendtalk-image');
        Route::post('/send/friendtalk-wide', [\App\Http\Controllers\Api\Admin\Barobill\BarobillKakaotalkController::class, 'sendFriendtalkWide'])->name('send.friendtalk-wide');

        // 전송 결과 조회
        Route::get('/send/{sendKey}', [\App\Http\Controllers\Api\Admin\Barobill\BarobillKakaotalkController::class, 'getSendResult'])->name('send.result');
        Route::post('/send/results', [\App\Http\Controllers\Api\Admin\Barobill\BarobillKakaotalkController::class, 'getSendResults'])->name('send.results');

        // 예약 취소
        Route::delete('/send/{sendKey}/cancel', [\App\Http\Controllers\Api\Admin\Barobill\BarobillKakaotalkController::class, 'cancelReserved'])->name('send.cancel');
    });

    // 바로빌 SMS API
    Route::prefix('barobill/sms')->name('barobill.sms.')->group(function () {
        Route::post('/send', [\App\Http\Controllers\Api\Admin\Barobill\BarobillSmsController::class, 'sendSms'])->name('send');
        Route::get('/from-numbers', [\App\Http\Controllers\Api\Admin\Barobill\BarobillSmsController::class, 'getFromNumbers'])->name('from-numbers');
        Route::post('/check-from-number', [\App\Http\Controllers\Api\Admin\Barobill\BarobillSmsController::class, 'checkFromNumber'])->name('check-from-number');
        Route::get('/send-state/{sendKey}', [\App\Http\Controllers\Api\Admin\Barobill\BarobillSmsController::class, 'getSendState'])->name('send-state');
    });

    // 테넌트 관리 API
    Route::prefix('tenants')->name('tenants.')->group(function () {
        // 고정 경로는 먼저 정의
        Route::get('/stats', [TenantController::class, 'stats'])->name('stats');

        // 동적 경로는 나중에 정의
        Route::get('/', [TenantController::class, 'index'])->name('index');
        Route::post('/', [TenantController::class, 'store'])->name('store');
        Route::get('/{id}', [TenantController::class, 'show'])->name('show');
        Route::put('/{id}', [TenantController::class, 'update'])->name('update');
        Route::delete('/{id}', [TenantController::class, 'destroy'])->name('destroy');

        // 복원 (일반관리자 가능)
        Route::post('/{id}/restore', [TenantController::class, 'restore'])->name('restore');

        // 슈퍼관리자 전용 액션 (영구삭제)
        Route::middleware('super.admin')->group(function () {
            Route::delete('/{id}/force', [TenantController::class, 'forceDestroy'])->name('forceDestroy');
        });

        // 모달 관련 API
        Route::get('/{id}/modal', [TenantController::class, 'modal'])->name('modal');
        Route::get('/{id}/users', [TenantController::class, 'users'])->name('users');
        Route::get('/{id}/departments', [TenantController::class, 'departments'])->name('departments');
        Route::get('/{id}/roles', [TenantController::class, 'roles'])->name('roles');
        Route::get('/{id}/menus', [TenantController::class, 'menus'])->name('menus');
        Route::get('/{id}/subscription', [TenantController::class, 'subscription'])->name('subscription');
    });

    // 역할 관리 API
    Route::prefix('roles')->name('roles.')->group(function () {
        Route::get('/', [RoleController::class, 'index'])->name('index');
        Route::post('/', [RoleController::class, 'store'])->name('store');
        Route::get('/{id}', [RoleController::class, 'show'])->name('show');
        Route::put('/{id}', [RoleController::class, 'update'])->name('update');
        Route::delete('/{id}', [RoleController::class, 'destroy'])->name('destroy');
    });

    // 부서 관리 API
    Route::prefix('departments')->name('departments.')->group(function () {
        Route::get('/', [DepartmentController::class, 'index'])->name('index');
        Route::post('/', [DepartmentController::class, 'store'])->name('store');
        Route::get('/{id}', [DepartmentController::class, 'show'])->name('show');
        Route::put('/{id}', [DepartmentController::class, 'update'])->name('update');
        Route::delete('/{id}', [DepartmentController::class, 'destroy'])->name('destroy');

        // 복원 (일반관리자 가능)
        Route::post('/{id}/restore', [DepartmentController::class, 'restore'])->name('restore');

        // 슈퍼관리자 전용 액션 (영구삭제)
        Route::middleware('super.admin')->group(function () {
            Route::delete('/{id}/force', [DepartmentController::class, 'forceDelete'])->name('forceDelete');
        });
    });

    // 사용자 관리 API
    Route::prefix('users')->name('users.')->group(function () {
        Route::get('/', [UserController::class, 'index'])->name('index');
        Route::post('/', [UserController::class, 'store'])->name('store');
        Route::get('/{id}', [UserController::class, 'show'])->name('show');
        Route::put('/{id}', [UserController::class, 'update'])->name('update');
        Route::delete('/{id}', [UserController::class, 'destroy'])->name('destroy');

        // 복원 (일반관리자 가능 - 슈퍼관리자 복원은 컨트롤러에서 차단)
        Route::post('/{id}/restore', [UserController::class, 'restore'])->name('restore');

        // 비밀번호 초기화 (임의 비밀번호 생성 + 메일 발송)
        Route::post('/{id}/reset-password', [UserController::class, 'resetPassword'])->name('resetPassword');

        // 슈퍼관리자 전용 액션 (영구삭제)
        Route::middleware('super.admin')->group(function () {
            Route::delete('/{id}/force', [UserController::class, 'forceDestroy'])->name('forceDestroy');
        });

        // DEV 사이트 자동 로그인 토큰 생성 (MNG → DEV)
        Route::post('/{id}/login-token', [UserController::class, 'loginToken'])->name('loginToken');

        // 모달 관련 API
        Route::get('/{id}/modal', [UserController::class, 'modal'])->name('modal');
    });

    // 메뉴 관리 API
    Route::prefix('menus')->name('menus.')->group(function () {
        // 고정 경로는 먼저 정의
        Route::get('/tree', [MenuController::class, 'tree'])->name('tree');
        Route::post('/reorder', [MenuController::class, 'reorder'])->name('reorder');
        Route::post('/move', [MenuController::class, 'move'])->name('move');
        Route::get('/available-global', [MenuController::class, 'availableGlobal'])->name('availableGlobal');
        Route::post('/copy-from-global', [MenuController::class, 'copyFromGlobal'])->name('copyFromGlobal');

        // 일괄 작업 (bulk actions)
        Route::post('/bulk-delete', [MenuController::class, 'bulkDelete'])->name('bulkDelete');
        Route::post('/bulk-restore', [MenuController::class, 'bulkRestore'])->name('bulkRestore');
        Route::middleware('super.admin')->post('/bulk-force-delete', [MenuController::class, 'bulkForceDelete'])->name('bulkForceDelete');

        // 동적 경로는 나중에 정의
        Route::get('/', [MenuController::class, 'index'])->name('index');
        Route::post('/', [MenuController::class, 'store'])->name('store');
        Route::get('/{id}', [MenuController::class, 'show'])->name('show');
        Route::put('/{id}', [MenuController::class, 'update'])->name('update');
        Route::delete('/{id}', [MenuController::class, 'destroy'])->name('destroy');

        // 복원 (일반관리자 가능)
        Route::post('/{id}/restore', [MenuController::class, 'restore'])->name('restore');

        // 슈퍼관리자 전용 액션 (영구삭제)
        Route::middleware('super.admin')->group(function () {
            Route::delete('/{id}/force', [MenuController::class, 'forceDestroy'])->name('forceDestroy');
        });

        // 추가 액션
        Route::post('/{id}/toggle-active', [MenuController::class, 'toggleActive'])->name('toggleActive');
        Route::post('/{id}/toggle-hidden', [MenuController::class, 'toggleHidden'])->name('toggleHidden');
    });

    // 글로벌 메뉴 관리 API (슈퍼관리자 전용)
    Route::middleware('super.admin')->prefix('global-menus')->name('global-menus.')->group(function () {
        // 고정 경로
        Route::post('/reorder', [GlobalMenuController::class, 'reorder'])->name('reorder');
        Route::post('/move', [GlobalMenuController::class, 'move'])->name('move');

        // 기본 CRUD
        Route::get('/', [GlobalMenuController::class, 'index'])->name('index');
        Route::post('/', [GlobalMenuController::class, 'store'])->name('store');
        Route::get('/{id}', [GlobalMenuController::class, 'show'])->name('show');
        Route::put('/{id}', [GlobalMenuController::class, 'update'])->name('update');
        Route::delete('/{id}', [GlobalMenuController::class, 'destroy'])->name('destroy');

        // 복원
        Route::post('/{id}/restore', [GlobalMenuController::class, 'restore'])->name('restore');

        // 영구삭제
        Route::delete('/{id}/force', [GlobalMenuController::class, 'forceDestroy'])->name('forceDestroy');

        // 추가 액션
        Route::post('/{id}/toggle-active', [GlobalMenuController::class, 'toggleActive'])->name('toggleActive');
        Route::post('/{id}/toggle-hidden', [GlobalMenuController::class, 'toggleHidden'])->name('toggleHidden');
    });

    // 권한 관리 API
    Route::prefix('permissions')->name('permissions.')->group(function () {
        Route::get('/', [PermissionController::class, 'index'])->name('index');
        Route::post('/', [PermissionController::class, 'store'])->name('store');
        Route::get('/{id}', [PermissionController::class, 'show'])->name('show');
        Route::put('/{id}', [PermissionController::class, 'update'])->name('update');
        Route::delete('/{id}', [PermissionController::class, 'destroy'])->name('destroy');
    });

    // 고객센터 API (활성화된 시스템 게시판 목록)
    Route::get('/customer-center', [CustomerCenterController::class, 'index'])->name('customer-center.index');

    // 시스템 게시판 관리 API
    Route::prefix('boards')->name('boards.')->group(function () {
        // 고정 경로는 먼저 정의
        Route::get('/stats', [BoardController::class, 'stats'])->name('stats');
        Route::get('/templates', [BoardController::class, 'templates'])->name('templates');
        Route::get('/templates/{type}/{key}', [BoardController::class, 'templateDetail'])->name('templateDetail');
        Route::get('/tenants', [BoardController::class, 'tenants'])->name('tenants');
        Route::post('/check-tenant-code', [BoardController::class, 'checkTenantCode'])->name('checkTenantCode');
        Route::post('/create-from-template', [BoardController::class, 'storeFromTemplate'])->name('storeFromTemplate');

        // 기본 CRUD
        Route::get('/', [BoardController::class, 'index'])->name('index');
        Route::post('/', [BoardController::class, 'store'])->name('store');
        Route::get('/{id}', [BoardController::class, 'show'])->name('show');
        Route::put('/{id}', [BoardController::class, 'update'])->name('update');
        Route::delete('/{id}', [BoardController::class, 'destroy'])->name('destroy');

        // 복원 (일반관리자 가능)
        Route::post('/{id}/restore', [BoardController::class, 'restore'])->name('restore');

        // 슈퍼관리자 전용 액션 (영구삭제)
        Route::middleware('super.admin')->group(function () {
            Route::delete('/{id}/force', [BoardController::class, 'forceDestroy'])->name('forceDestroy');
        });

        // 추가 액션
        Route::post('/{id}/toggle-active', [BoardController::class, 'toggleActive'])->name('toggleActive');

        // 필드 관리 API
        Route::get('/{id}/fields', [BoardController::class, 'fields'])->name('fields');
        Route::post('/{id}/fields', [BoardController::class, 'storeField'])->name('storeField');
        Route::put('/{id}/fields/{fieldId}', [BoardController::class, 'updateField'])->name('updateField');
        Route::delete('/{id}/fields/{fieldId}', [BoardController::class, 'destroyField'])->name('destroyField');
        Route::post('/{id}/fields/reorder', [BoardController::class, 'reorderFields'])->name('reorderFields');
    });

    // 역할 권한 관리 API
    Route::prefix('role-permissions')->name('role-permissions.')->group(function () {
        Route::get('/matrix', [RolePermissionController::class, 'getMatrix'])->name('matrix');
        Route::post('/toggle', [RolePermissionController::class, 'toggle'])->name('toggle');
        Route::post('/allow-all', [RolePermissionController::class, 'allowAll'])->name('allowAll');
        Route::post('/deny-all', [RolePermissionController::class, 'denyAll'])->name('denyAll');
        Route::post('/reset', [RolePermissionController::class, 'reset'])->name('reset');
    });

    // 부서 권한 관리 API
    Route::prefix('department-permissions')->name('department-permissions.')->group(function () {
        Route::get('/matrix', [\App\Http\Controllers\Api\Admin\DepartmentPermissionController::class, 'getMatrix'])->name('matrix');
        Route::post('/toggle', [\App\Http\Controllers\Api\Admin\DepartmentPermissionController::class, 'toggle'])->name('toggle');
        Route::post('/allow-all', [\App\Http\Controllers\Api\Admin\DepartmentPermissionController::class, 'allowAll'])->name('allowAll');
        Route::post('/deny-all', [\App\Http\Controllers\Api\Admin\DepartmentPermissionController::class, 'denyAll'])->name('denyAll');
        Route::post('/reset', [\App\Http\Controllers\Api\Admin\DepartmentPermissionController::class, 'reset'])->name('reset');
    });

    // 개인 권한 관리 API
    Route::prefix('user-permissions')->name('user-permissions.')->group(function () {
        Route::get('/matrix', [\App\Http\Controllers\Api\Admin\UserPermissionController::class, 'getMatrix'])->name('matrix');
        Route::post('/toggle', [\App\Http\Controllers\Api\Admin\UserPermissionController::class, 'toggle'])->name('toggle');
        Route::post('/allow-all', [\App\Http\Controllers\Api\Admin\UserPermissionController::class, 'allowAll'])->name('allowAll');
        Route::post('/deny-all', [\App\Http\Controllers\Api\Admin\UserPermissionController::class, 'denyAll'])->name('denyAll');
        Route::post('/reset', [\App\Http\Controllers\Api\Admin\UserPermissionController::class, 'reset'])->name('reset');
    });

    // 권한 분석 API
    Route::prefix('permission-analyze')->name('permission-analyze.')->group(function () {
        Route::get('/menu-tree', [\App\Http\Controllers\Api\Admin\PermissionAnalyzeController::class, 'menuTree'])->name('menuTree');
        Route::get('/analyze', [\App\Http\Controllers\Api\Admin\PermissionAnalyzeController::class, 'analyzeMenu'])->name('analyze');
        Route::get('/trace', [\App\Http\Controllers\Api\Admin\PermissionAnalyzeController::class, 'traceUsers'])->name('trace');
        Route::get('/export-csv', [\App\Http\Controllers\Api\Admin\PermissionAnalyzeController::class, 'exportCsv'])->name('exportCsv');
        Route::post('/recalculate', [\App\Http\Controllers\Api\Admin\PermissionAnalyzeController::class, 'recalculate'])->name('recalculate');
    });

    // 삭제된 데이터 백업 API
    Route::prefix('archived-records')->name('archived-records.')->group(function () {
        Route::get('/stats', [\App\Http\Controllers\Api\Admin\ArchivedRecordController::class, 'stats'])->name('stats');
        Route::get('/', [\App\Http\Controllers\Api\Admin\ArchivedRecordController::class, 'index'])->name('index');
        Route::get('/{id}', [\App\Http\Controllers\Api\Admin\ArchivedRecordController::class, 'show'])->name('show');
    });

    /*
    |--------------------------------------------------------------------------
    | 프로젝트 관리 API
    |--------------------------------------------------------------------------
    */
    Route::prefix('pm')->name('pm.')->group(function () {

        // 프로젝트 관리 API
        Route::prefix('projects')->name('projects.')->group(function () {
            // 고정 경로
            Route::get('/stats', [PmProjectController::class, 'stats'])->name('stats');
            Route::get('/dashboard', [PmProjectController::class, 'dashboard'])->name('dashboard');
            Route::get('/dropdown', [PmProjectController::class, 'dropdown'])->name('dropdown');

            // 기본 CRUD
            Route::get('/', [PmProjectController::class, 'index'])->name('index');
            Route::post('/', [PmProjectController::class, 'store'])->name('store');
            Route::get('/{id}', [PmProjectController::class, 'show'])->name('show');
            Route::put('/{id}', [PmProjectController::class, 'update'])->name('update');
            Route::delete('/{id}', [PmProjectController::class, 'destroy'])->name('destroy');

            // 복원 (일반관리자 가능)
            Route::post('/{id}/restore', [PmProjectController::class, 'restore'])->name('restore');

            // 슈퍼관리자 전용 액션 (영구삭제)
            Route::middleware('super.admin')->group(function () {
                Route::delete('/{id}/force', [PmProjectController::class, 'forceDestroy'])->name('forceDestroy');
            });

            // 추가 액션
            Route::post('/{id}/status', [PmProjectController::class, 'changeStatus'])->name('changeStatus');
            Route::post('/{id}/duplicate', [PmProjectController::class, 'duplicate'])->name('duplicate');
        });

        // 작업 관리 API
        Route::prefix('tasks')->name('tasks.')->group(function () {
            // 고정 경로
            Route::get('/urgent', [PmTaskController::class, 'urgent'])->name('urgent');
            Route::post('/bulk', [PmTaskController::class, 'bulk'])->name('bulk');

            // 기본 CRUD
            Route::get('/', [PmTaskController::class, 'index'])->name('index');
            Route::post('/', [PmTaskController::class, 'store'])->name('store');
            Route::get('/{id}', [PmTaskController::class, 'show'])->name('show');
            Route::put('/{id}', [PmTaskController::class, 'update'])->name('update');
            Route::delete('/{id}', [PmTaskController::class, 'destroy'])->name('destroy');

            // 복원 (일반관리자 가능)
            Route::post('/{id}/restore', [PmTaskController::class, 'restore'])->name('restore');

            // 슈퍼관리자 전용 액션 (영구삭제)
            Route::middleware('super.admin')->group(function () {
                Route::delete('/{id}/force', [PmTaskController::class, 'forceDestroy'])->name('forceDestroy');
            });

            // 추가 액션
            Route::post('/{id}/status', [PmTaskController::class, 'changeStatus'])->name('changeStatus');
            Route::post('/{id}/toggle-urgent', [PmTaskController::class, 'toggleUrgent'])->name('toggleUrgent');

            // 프로젝트별
            Route::get('/project/{projectId}', [PmTaskController::class, 'byProject'])->name('byProject');
            Route::post('/project/{projectId}/reorder', [PmTaskController::class, 'reorder'])->name('reorder');
            Route::get('/project/{projectId}/stats', [PmTaskController::class, 'stats'])->name('stats');
        });

        // 이슈 관리 API
        Route::prefix('issues')->name('issues.')->group(function () {
            // 고정 경로
            Route::get('/stats', [PmIssueController::class, 'stats'])->name('stats');
            Route::get('/open', [PmIssueController::class, 'open'])->name('open');
            Route::post('/bulk', [PmIssueController::class, 'bulk'])->name('bulk');

            // 기본 CRUD
            Route::get('/', [PmIssueController::class, 'index'])->name('index');
            Route::post('/', [PmIssueController::class, 'store'])->name('store');
            Route::get('/{id}', [PmIssueController::class, 'show'])->name('show');
            Route::put('/{id}', [PmIssueController::class, 'update'])->name('update');
            Route::delete('/{id}', [PmIssueController::class, 'destroy'])->name('destroy');

            // 복원 (일반관리자 가능)
            Route::post('/{id}/restore', [PmIssueController::class, 'restore'])->name('restore');

            // 슈퍼관리자 전용 액션 (영구삭제)
            Route::middleware('super.admin')->group(function () {
                Route::delete('/{id}/force', [PmIssueController::class, 'forceDestroy'])->name('forceDestroy');
            });

            // 추가 액션
            Route::post('/{id}/status', [PmIssueController::class, 'changeStatus'])->name('changeStatus');
            Route::post('/{id}/toggle-urgent', [PmIssueController::class, 'toggleUrgent'])->name('toggleUrgent');

            // 연관별
            Route::get('/project/{projectId}', [PmIssueController::class, 'byProject'])->name('byProject');
            Route::get('/task/{taskId}', [PmIssueController::class, 'byTask'])->name('byTask');
        });

        // JSON Import API
        Route::prefix('import')->name('import.')->group(function () {
            Route::get('/template', [PmImportController::class, 'template'])->name('template');
            Route::post('/validate', [PmImportController::class, 'validate'])->name('validate');
            Route::post('/', [PmImportController::class, 'import'])->name('import');
            Route::post('/project/{projectId}/tasks', [PmImportController::class, 'importTasks'])->name('importTasks');
            Route::post('/task/{taskId}/issues', [PmImportController::class, 'importIssues'])->name('importIssues');
        });
    });

    /*
    |--------------------------------------------------------------------------
    | 일일 스크럼 API
    |--------------------------------------------------------------------------
    */
    Route::prefix('daily-logs')->name('daily-logs.')->group(function () {
        // 고정 경로
        Route::get('/stats', [DailyLogController::class, 'stats'])->name('stats');
        Route::get('/today', [DailyLogController::class, 'today'])->name('today');
        Route::get('/assignees', [DailyLogController::class, 'assignees'])->name('assignees');

        // 기본 CRUD
        Route::get('/', [DailyLogController::class, 'index'])->name('index');
        Route::post('/', [DailyLogController::class, 'store'])->name('store');
        Route::get('/{id}', [DailyLogController::class, 'show'])->name('show');
        Route::put('/{id}', [DailyLogController::class, 'update'])->name('update');
        Route::delete('/{id}', [DailyLogController::class, 'destroy'])->name('destroy');

        // 복원 (일반관리자 가능)
        Route::post('/{id}/restore', [DailyLogController::class, 'restore'])->name('restore');

        // 슈퍼관리자 전용 액션 (영구삭제)
        Route::middleware('super.admin')->group(function () {
            Route::delete('/{id}/force', [DailyLogController::class, 'forceDestroy'])->name('forceDestroy');
        });

        // 항목(Entry) 관리
        Route::post('/{logId}/entries', [DailyLogController::class, 'addEntry'])->name('addEntry');
        Route::post('/{logId}/entries/reorder', [DailyLogController::class, 'reorderEntries'])->name('reorderEntries');
    });

    // 항목 개별 API (로그 ID 없이 직접 접근)
    Route::prefix('daily-logs/entries')->name('daily-logs.entries.')->group(function () {
        Route::put('/{entryId}', [DailyLogController::class, 'updateEntry'])->name('update');
        Route::put('/{entryId}/status', [DailyLogController::class, 'updateEntryStatus'])->name('updateStatus');
        Route::delete('/{entryId}', [DailyLogController::class, 'deleteEntry'])->name('delete');
    });

    /*
    |--------------------------------------------------------------------------
    | 품목기준 필드 관리 API
    |--------------------------------------------------------------------------
    */
    Route::prefix('item-fields')->name('item-fields.')->group(function () {
        // 시딩 상태 (HTMX partial)
        Route::get('/seeding-status', [ItemFieldController::class, 'seedingStatus'])->name('seedingStatus');

        // 소스 테이블 목록
        Route::get('/source-tables', [ItemFieldController::class, 'sourceTables'])->name('sourceTables');

        // 단일 테이블 시딩
        Route::post('/seed', [ItemFieldController::class, 'seed'])->name('seed');

        // 전체 테이블 시딩
        Route::post('/seed-all', [ItemFieldController::class, 'seedAll'])->name('seedAll');

        // 단일 테이블 초기화
        Route::post('/reset', [ItemFieldController::class, 'reset'])->name('reset');

        // 전체 테이블 초기화
        Route::post('/reset-all', [ItemFieldController::class, 'resetAll'])->name('resetAll');

        // 커스텀 필드 목록 (HTMX partial)
        Route::get('/custom-fields', [ItemFieldController::class, 'customFields'])->name('customFields');

        // 커스텀 필드 추가
        Route::post('/custom-fields', [ItemFieldController::class, 'storeCustomField'])->name('storeCustomField');

        // 커스텀 필드 수정
        Route::put('/custom-fields/{id}', [ItemFieldController::class, 'updateCustomField'])->name('updateCustomField');

        // 커스텀 필드 삭제
        Route::delete('/custom-fields/{id}', [ItemFieldController::class, 'destroyCustomField'])->name('destroyCustomField');

        // 커스텀 필드 일괄 삭제
        Route::delete('/custom-fields', [ItemFieldController::class, 'destroyCustomFields'])->name('destroyCustomFields');

        // 커스텀 필드 복원
        Route::post('/custom-fields/{id}/restore', [ItemFieldController::class, 'restoreCustomField'])->name('restoreCustomField');

        // 커스텀 필드 영구 삭제
        Route::delete('/custom-fields/{id}/force', [ItemFieldController::class, 'forceDestroyCustomField'])->name('forceDestroyCustomField');

        // 삭제된 필드 일괄 영구 삭제 (휴지통 비우기)
        Route::post('/purge-deleted', [ItemFieldController::class, 'purgeDeleted'])->name('purgeDeleted');

        // 오류 로그 조회 (HTMX partial)
        Route::get('/error-logs', [ItemFieldController::class, 'errorLogs'])->name('errorLogs');

        // 오류 로그 초기화
        Route::delete('/error-logs', [ItemFieldController::class, 'clearErrorLogs'])->name('clearErrorLogs');

        // AI 문의용 오류 보고서 생성
        Route::get('/error-report', [ItemFieldController::class, 'generateErrorReport'])->name('errorReport');

        // 시스템 필드 정의 관리 (마스터 데이터)
        Route::prefix('system-definitions')->name('systemDefinitions.')->group(function () {
            Route::get('/', [ItemFieldController::class, 'systemFieldDefinitions'])->name('index');
            Route::post('/', [ItemFieldController::class, 'storeSystemFieldDefinition'])->name('store');
            Route::put('/{id}', [ItemFieldController::class, 'updateSystemFieldDefinition'])->name('update');
            Route::delete('/{id}', [ItemFieldController::class, 'destroySystemFieldDefinition'])->name('destroy');
            Route::post('/reorder', [ItemFieldController::class, 'reorderSystemFieldDefinitions'])->name('reorder');
        });

        // 소스 테이블 관리
        Route::prefix('source-tables')->name('sourceTables.')->group(function () {
            Route::post('/', [ItemFieldController::class, 'storeSourceTable'])->name('store');
            Route::delete('/{sourceTable}', [ItemFieldController::class, 'destroySourceTable'])->name('destroy');
            Route::post('/{sourceTable}/sync-field-names', [ItemFieldController::class, 'syncSourceTableFieldNames'])->name('syncFieldNames');
        });

        // DB 테이블 조회 (등록 가능한 테이블 목록)
        Route::get('/database-tables', [ItemFieldController::class, 'databaseTables'])->name('databaseTables');
        Route::get('/database-tables/{table}/columns', [ItemFieldController::class, 'tableColumns'])->name('tableColumns');
    });

    // 채번 규칙 관리 API
    Route::prefix('numbering-rules')->name('numbering-rules.')->group(function () {
        Route::get('/', [NumberingRuleController::class, 'index'])->name('index');
        Route::post('/', [NumberingRuleController::class, 'store'])->name('store');
        Route::put('/{id}', [NumberingRuleController::class, 'update'])->name('update');
        Route::delete('/{id}', [NumberingRuleController::class, 'destroy'])->name('destroy');
        Route::post('/preview', [NumberingRuleController::class, 'preview'])->name('preview');
    });

    /*
    |--------------------------------------------------------------------------
    | 견적수식 관리 API
    |--------------------------------------------------------------------------
    */
    Route::prefix('quote-formulas')->name('quote-formulas.')->group(function () {

        // 카테고리 관리 API
        Route::prefix('categories')->name('categories.')->group(function () {
            // 고정 경로
            Route::get('/stats', [QuoteFormulaCategoryController::class, 'stats'])->name('stats');
            Route::get('/dropdown', [QuoteFormulaCategoryController::class, 'dropdown'])->name('dropdown');
            Route::post('/reorder', [QuoteFormulaCategoryController::class, 'reorder'])->name('reorder');

            // 기본 CRUD
            Route::get('/', [QuoteFormulaCategoryController::class, 'index'])->name('index');
            Route::post('/', [QuoteFormulaCategoryController::class, 'store'])->name('store');
            Route::get('/{id}', [QuoteFormulaCategoryController::class, 'show'])->name('show');
            Route::put('/{id}', [QuoteFormulaCategoryController::class, 'update'])->name('update');
            Route::delete('/{id}', [QuoteFormulaCategoryController::class, 'destroy'])->name('destroy');

            // 복원 (일반관리자 가능)
            Route::post('/{id}/restore', [QuoteFormulaCategoryController::class, 'restore'])->name('restore');

            // 슈퍼관리자 전용 액션 (영구삭제)
            Route::middleware('super.admin')->group(function () {
                Route::delete('/{id}/force', [QuoteFormulaCategoryController::class, 'forceDestroy'])->name('forceDestroy');
            });

            // 추가 액션
            Route::post('/{id}/toggle-active', [QuoteFormulaCategoryController::class, 'toggleActive'])->name('toggleActive');
        });

        // 수식 관리 API
        Route::prefix('formulas')->name('formulas.')->group(function () {
            // 고정 경로
            Route::get('/stats', [QuoteFormulaController::class, 'stats'])->name('stats');
            Route::get('/variables', [QuoteFormulaController::class, 'variables'])->name('variables');
            Route::post('/reorder', [QuoteFormulaController::class, 'reorder'])->name('reorder');
            Route::post('/validate', [QuoteFormulaController::class, 'validate'])->name('validate');
            Route::post('/test', [QuoteFormulaController::class, 'test'])->name('test');
            Route::post('/simulate', [QuoteFormulaController::class, 'simulate'])->name('simulate');
            Route::post('/simulate-bom', [QuoteFormulaController::class, 'simulateBom'])->name('simulate-bom');
            Route::get('/items', [QuoteFormulaController::class, 'items'])->name('items');

            // 기본 CRUD
            Route::get('/', [QuoteFormulaController::class, 'index'])->name('index');
            Route::post('/', [QuoteFormulaController::class, 'store'])->name('store');
            Route::get('/{id}', [QuoteFormulaController::class, 'show'])->name('show');
            Route::put('/{id}', [QuoteFormulaController::class, 'update'])->name('update');
            Route::delete('/{id}', [QuoteFormulaController::class, 'destroy'])->name('destroy');

            // 복원 (일반관리자 가능)
            Route::post('/{id}/restore', [QuoteFormulaController::class, 'restore'])->name('restore');

            // 슈퍼관리자 전용 액션 (영구삭제)
            Route::middleware('super.admin')->group(function () {
                Route::delete('/{id}/force', [QuoteFormulaController::class, 'forceDestroy'])->name('forceDestroy');
            });

            // 추가 액션
            Route::post('/{id}/toggle-active', [QuoteFormulaController::class, 'toggleActive'])->name('toggleActive');
            Route::post('/{id}/duplicate', [QuoteFormulaController::class, 'duplicate'])->name('duplicate');

            // 카테고리별
            Route::get('/category/{categoryId}', [QuoteFormulaController::class, 'byCategory'])->name('byCategory');

            // 수식별 범위 관리 API
            Route::prefix('{formulaId}/ranges')->name('ranges.')->group(function () {
                Route::get('/', [\App\Http\Controllers\Api\Admin\Quote\QuoteFormulaRangeController::class, 'index'])->name('index');
                Route::post('/', [\App\Http\Controllers\Api\Admin\Quote\QuoteFormulaRangeController::class, 'store'])->name('store');
                Route::get('/{rangeId}', [\App\Http\Controllers\Api\Admin\Quote\QuoteFormulaRangeController::class, 'show'])->name('show');
                Route::put('/{rangeId}', [\App\Http\Controllers\Api\Admin\Quote\QuoteFormulaRangeController::class, 'update'])->name('update');
                Route::delete('/{rangeId}', [\App\Http\Controllers\Api\Admin\Quote\QuoteFormulaRangeController::class, 'destroy'])->name('destroy');
                Route::post('/reorder', [\App\Http\Controllers\Api\Admin\Quote\QuoteFormulaRangeController::class, 'reorder'])->name('reorder');
            });

            // 수식별 매핑 관리 API
            Route::prefix('{formulaId}/mappings')->name('mappings.')->group(function () {
                Route::get('/', [\App\Http\Controllers\Api\Admin\Quote\QuoteFormulaMappingController::class, 'index'])->name('index');
                Route::post('/', [\App\Http\Controllers\Api\Admin\Quote\QuoteFormulaMappingController::class, 'store'])->name('store');
                Route::get('/{mappingId}', [\App\Http\Controllers\Api\Admin\Quote\QuoteFormulaMappingController::class, 'show'])->name('show');
                Route::put('/{mappingId}', [\App\Http\Controllers\Api\Admin\Quote\QuoteFormulaMappingController::class, 'update'])->name('update');
                Route::delete('/{mappingId}', [\App\Http\Controllers\Api\Admin\Quote\QuoteFormulaMappingController::class, 'destroy'])->name('destroy');
                Route::post('/reorder', [\App\Http\Controllers\Api\Admin\Quote\QuoteFormulaMappingController::class, 'reorder'])->name('reorder');
            });

            // 수식별 품목 관리 API
            Route::prefix('{formulaId}/items')->name('items.')->group(function () {
                Route::get('/', [\App\Http\Controllers\Api\Admin\Quote\QuoteFormulaItemController::class, 'index'])->name('index');
                Route::post('/', [\App\Http\Controllers\Api\Admin\Quote\QuoteFormulaItemController::class, 'store'])->name('store');
                Route::get('/{itemId}', [\App\Http\Controllers\Api\Admin\Quote\QuoteFormulaItemController::class, 'show'])->name('show');
                Route::put('/{itemId}', [\App\Http\Controllers\Api\Admin\Quote\QuoteFormulaItemController::class, 'update'])->name('update');
                Route::delete('/{itemId}', [\App\Http\Controllers\Api\Admin\Quote\QuoteFormulaItemController::class, 'destroy'])->name('destroy');
                Route::post('/reorder', [\App\Http\Controllers\Api\Admin\Quote\QuoteFormulaItemController::class, 'reorder'])->name('reorder');
            });
        });
    });
});

/*
|--------------------------------------------------------------------------
| Gemini AI API
|--------------------------------------------------------------------------
|
| SAM AI 음성 어시스턴트용 Gemini API 키 제공
| 인증된 사용자만 접근 가능
|
*/
Route::middleware(['web', 'auth'])->prefix('gemini')->name('api.gemini.')->group(function () {
    Route::get('/api-key', [GeminiController::class, 'getApiKey'])->name('api-key');
});

/*
|--------------------------------------------------------------------------
| 사업자등록증 OCR API
|--------------------------------------------------------------------------
|
| Lab > AI > 사업자등록증 OCR 기능
| Claude Vision API를 사용한 OCR 처리
|
*/
Route::middleware(['web', 'auth'])->prefix('biz-cert')->name('api.biz-cert.')->group(function () {
    Route::post('/ocr', [\App\Http\Controllers\Api\BizCertController::class, 'ocr'])->name('ocr');
    Route::get('/', [\App\Http\Controllers\Api\BizCertController::class, 'index'])->name('index');
    Route::post('/', [\App\Http\Controllers\Api\BizCertController::class, 'store'])->name('store');
    Route::delete('/{id}', [\App\Http\Controllers\Api\BizCertController::class, 'destroy'])->name('destroy');
});

/*
|--------------------------------------------------------------------------
| 문서양식 관리 API
|--------------------------------------------------------------------------
*/
Route::middleware(['web', 'auth', 'hq.member'])->prefix('admin/document-templates')->name('api.admin.document-templates.')->group(function () {
    Route::get('/', [DocumentTemplateApiController::class, 'index'])->name('index');
    Route::post('/', [DocumentTemplateApiController::class, 'store'])->name('store');
    Route::get('/{id}', [DocumentTemplateApiController::class, 'show'])->name('show');
    Route::put('/{id}', [DocumentTemplateApiController::class, 'update'])->name('update');
    Route::delete('/{id}', [DocumentTemplateApiController::class, 'destroy'])->name('destroy');
    Route::delete('/{id}/force', [DocumentTemplateApiController::class, 'forceDestroy'])->name('force-destroy');
    Route::post('/{id}/restore', [DocumentTemplateApiController::class, 'restore'])->name('restore');
    Route::post('/{id}/toggle-active', [DocumentTemplateApiController::class, 'toggleActive'])->name('toggle-active');
    Route::post('/{id}/duplicate', [DocumentTemplateApiController::class, 'duplicate'])->name('duplicate');
    Route::post('/upload-image', [DocumentTemplateApiController::class, 'uploadImage'])->name('upload-image');
});

/*
|--------------------------------------------------------------------------
| 공통코드 JSON API (문서양식 등에서 사용)
|--------------------------------------------------------------------------
*/
Route::middleware(['web', 'auth', 'hq.member'])->prefix('admin/common-codes')->name('api.admin.common-codes.')->group(function () {
    Route::get('/{group}', [DocumentTemplateApiController::class, 'getCommonCodes'])->name('group');
});

/*
|--------------------------------------------------------------------------
| 소스 테이블 통합 검색 API (문서양식 연결 설정용)
|--------------------------------------------------------------------------
*/
Route::middleware(['web', 'auth', 'hq.member'])->prefix('admin/source-tables')->name('api.admin.source-tables.')->group(function () {
    Route::get('/', [\App\Http\Controllers\Api\Admin\SourceTableSearchController::class, 'tables'])->name('index');
    Route::get('/{table}/search', [\App\Http\Controllers\Api\Admin\SourceTableSearchController::class, 'search'])->name('search');
});

/*
|--------------------------------------------------------------------------
| 품목 검색 API (문서 작성 시 품명 자동완성)
|--------------------------------------------------------------------------
*/
Route::middleware(['web', 'auth', 'hq.member'])->prefix('admin/items')->name('api.admin.items.')->group(function () {
    Route::get('/search', [\App\Http\Controllers\Api\Admin\ItemApiController::class, 'search'])->name('search');

    // 품목관리 페이지 API
    Route::get('/', [\App\Http\Controllers\Api\Admin\ItemManagementApiController::class, 'index'])->name('index');
    Route::get('/{id}/bom-tree', [\App\Http\Controllers\Api\Admin\ItemManagementApiController::class, 'bomTree'])->name('bom-tree');
    Route::get('/{id}/detail', [\App\Http\Controllers\Api\Admin\ItemManagementApiController::class, 'detail'])->name('detail');
    Route::post('/{id}/calculate-formula', [\App\Http\Controllers\Api\Admin\ItemManagementApiController::class, 'calculateFormula'])->name('calculate-formula');
});

/*
|--------------------------------------------------------------------------
| 공정 검색 API (문서양식 품질검사 연결용)
|--------------------------------------------------------------------------
*/
Route::middleware(['web', 'auth', 'hq.member'])->prefix('admin/processes')->name('api.admin.processes.')->group(function () {
    Route::get('/search', [\App\Http\Controllers\Api\Admin\ProcessApiController::class, 'search'])->name('search');
});

/*
|--------------------------------------------------------------------------
| 테넌트 사용자 검색 API (결재라인 담당자 선택용)
|--------------------------------------------------------------------------
*/
Route::middleware(['web', 'auth', 'hq.member'])->prefix('admin/tenant-users')->name('api.admin.tenant-users.')->group(function () {
    Route::get('/search', [\App\Http\Controllers\Api\Admin\TenantUserApiController::class, 'search'])->name('search');
});

/*
|--------------------------------------------------------------------------
| 문서 관리 API
|--------------------------------------------------------------------------
*/
Route::middleware(['web', 'auth', 'hq.member'])->prefix('admin/documents')->name('api.admin.documents.')->group(function () {
    Route::get('/', [DocumentApiController::class, 'index'])->name('index');
    Route::post('/', [DocumentApiController::class, 'store'])->name('store');
    Route::get('/{id}', [DocumentApiController::class, 'show'])->name('show');
    Route::patch('/{id}', [DocumentApiController::class, 'update'])->name('update');
    Route::delete('/{id}', [DocumentApiController::class, 'destroy'])->name('destroy');
    Route::delete('/{id}/force', [DocumentApiController::class, 'forceDestroy'])->name('force-destroy');
    Route::post('/{id}/restore', [DocumentApiController::class, 'restore'])->name('restore');
    // 결재 워크플로우
    Route::post('/{id}/submit', [DocumentApiController::class, 'submit'])->name('submit');
    Route::post('/{id}/approve', [DocumentApiController::class, 'approve'])->name('approve');
    Route::post('/{id}/reject', [DocumentApiController::class, 'reject'])->name('reject');
});

/*
|--------------------------------------------------------------------------
| 카테고리 관리 API
|--------------------------------------------------------------------------
*/
Route::middleware(['web', 'auth', 'hq.member'])->prefix('admin/categories')->name('api.admin.categories.')->group(function () {
    // 고정 경로
    Route::get('/list', [CategoryApiController::class, 'list'])->name('list');
    Route::get('/tree', [CategoryApiController::class, 'tree'])->name('tree');
    Route::post('/reorder', [CategoryApiController::class, 'reorder'])->name('reorder');

    // 기본 CRUD
    Route::get('/{id}', [CategoryApiController::class, 'show'])->name('show');
    Route::post('/', [CategoryApiController::class, 'store'])->name('store');
    Route::put('/{id}', [CategoryApiController::class, 'update'])->name('update');
    Route::delete('/{id}', [CategoryApiController::class, 'destroy'])->name('destroy');

    // 추가 액션
    Route::post('/{id}/toggle', [CategoryApiController::class, 'toggle'])->name('toggle');
    Route::post('/{id}/move', [CategoryApiController::class, 'move'])->name('move');
    Route::post('/{id}/promote-to-global', [CategoryApiController::class, 'promoteToGlobal'])->name('promoteToGlobal');
});

/*
|--------------------------------------------------------------------------
| 글로벌 카테고리 관리 API
|--------------------------------------------------------------------------
*/
Route::middleware(['web', 'auth', 'hq.member'])->prefix('admin/global-categories')->name('api.admin.global-categories.')->group(function () {
    // 고정 경로
    Route::get('/list', [GlobalCategoryApiController::class, 'list'])->name('list');
    Route::post('/bulk-copy-to-tenant', [GlobalCategoryApiController::class, 'bulkCopyToTenant'])->name('bulkCopyToTenant');

    // 기본 CRUD
    Route::get('/{id}', [GlobalCategoryApiController::class, 'show'])->name('show');
    Route::post('/', [GlobalCategoryApiController::class, 'store'])->name('store');
    Route::put('/{id}', [GlobalCategoryApiController::class, 'update'])->name('update');
    Route::delete('/{id}', [GlobalCategoryApiController::class, 'destroy'])->name('destroy');

    // 추가 액션
    Route::post('/{id}/toggle', [GlobalCategoryApiController::class, 'toggle'])->name('toggle');
    Route::post('/{id}/copy-to-tenant', [GlobalCategoryApiController::class, 'copyToTenant'])->name('copyToTenant');
});

/*
|--------------------------------------------------------------------------
| 웹 녹음 AI 요약 API
|--------------------------------------------------------------------------
|
| Lab > AI > 웹 녹음 AI 요약 기능
| Google STT + Claude API를 사용한 회의록 생성
|
*/
/*
|--------------------------------------------------------------------------
| 설비관리 API (Equipment Management)
|--------------------------------------------------------------------------
*/
Route::middleware(['web', 'auth', 'hq.member'])->prefix('admin/equipment')->name('api.admin.equipment.')->group(function () {
    // 설비 CRUD
    Route::get('/', [\App\Http\Controllers\Api\Admin\EquipmentController::class, 'index'])->name('index');
    Route::post('/', [\App\Http\Controllers\Api\Admin\EquipmentController::class, 'store'])->name('store');
    Route::get('/{id}', [\App\Http\Controllers\Api\Admin\EquipmentController::class, 'show'])->whereNumber('id')->name('show');
    Route::put('/{id}', [\App\Http\Controllers\Api\Admin\EquipmentController::class, 'update'])->whereNumber('id')->name('update');
    Route::delete('/{id}', [\App\Http\Controllers\Api\Admin\EquipmentController::class, 'destroy'])->whereNumber('id')->name('destroy');
    Route::post('/{id}/restore', [\App\Http\Controllers\Api\Admin\EquipmentController::class, 'restore'])->whereNumber('id')->name('restore');

    // 점검 템플릿
    Route::get('/{id}/templates', [\App\Http\Controllers\Api\Admin\EquipmentController::class, 'templates'])->whereNumber('id')->name('templates');
    Route::post('/{id}/templates', [\App\Http\Controllers\Api\Admin\EquipmentInspectionController::class, 'storeTemplate'])->whereNumber('id')->name('templates.store');
    Route::put('/templates/{templateId}', [\App\Http\Controllers\Api\Admin\EquipmentInspectionController::class, 'updateTemplate'])->whereNumber('templateId')->name('templates.update');
    Route::delete('/templates/{templateId}', [\App\Http\Controllers\Api\Admin\EquipmentInspectionController::class, 'deleteTemplate'])->whereNumber('templateId')->name('templates.destroy');

    // 점검 기록
    Route::get('/inspections', [\App\Http\Controllers\Api\Admin\EquipmentInspectionController::class, 'index'])->name('inspections.index');
    Route::patch('/inspections/detail', [\App\Http\Controllers\Api\Admin\EquipmentInspectionController::class, 'toggleDetail'])->name('inspections.toggle');
    Route::patch('/inspections/notes', [\App\Http\Controllers\Api\Admin\EquipmentInspectionController::class, 'updateNotes'])->name('inspections.notes');

    // 수리이력
    Route::get('/repairs', [\App\Http\Controllers\Api\Admin\EquipmentRepairController::class, 'index'])->name('repairs.index');
    Route::post('/repairs', [\App\Http\Controllers\Api\Admin\EquipmentRepairController::class, 'store'])->name('repairs.store');
    Route::put('/repairs/{id}', [\App\Http\Controllers\Api\Admin\EquipmentRepairController::class, 'update'])->whereNumber('id')->name('repairs.update');
    Route::delete('/repairs/{id}', [\App\Http\Controllers\Api\Admin\EquipmentRepairController::class, 'destroy'])->whereNumber('id')->name('repairs.destroy');

    // 사진 관리
    Route::post('/{id}/photos', [\App\Http\Controllers\Api\Admin\EquipmentController::class, 'uploadPhotos'])->whereNumber('id')->name('photos.upload');
    Route::get('/{id}/photos', [\App\Http\Controllers\Api\Admin\EquipmentController::class, 'photos'])->whereNumber('id')->name('photos.index');
    Route::delete('/{id}/photos/{fileId}', [\App\Http\Controllers\Api\Admin\EquipmentController::class, 'deletePhoto'])->whereNumber('id')->whereNumber('fileId')->name('photos.destroy');

    // 엑셀 Import
    Route::post('/import/preview', [\App\Http\Controllers\Api\Admin\EquipmentController::class, 'importPreview'])->name('import.preview');
    Route::post('/import', [\App\Http\Controllers\Api\Admin\EquipmentController::class, 'importExecute'])->name('import.execute');
});

Route::middleware(['web', 'auth'])->prefix('meeting-logs')->name('api.admin.meeting-logs.')->group(function () {
    // 목록 조회 (HTMX 지원)
    Route::get('/', [MeetingLogController::class, 'index'])->name('index');

    // 회의록 생성 (녹음 시작)
    Route::post('/', [MeetingLogController::class, 'store'])->name('store');

    // 파일 업로드 (회의록 AI 요약)
    Route::post('/upload', [MeetingLogController::class, 'uploadFile'])->name('upload');

    // 상세 조회
    Route::get('/{id}', [MeetingLogController::class, 'show'])->name('show');

    // 오디오 업로드 및 처리
    Route::post('/{id}/process', [MeetingLogController::class, 'processAudio'])->name('process');

    // 제목 수정
    Route::put('/{id}/title', [MeetingLogController::class, 'updateTitle'])->name('update-title');

    // 삭제
    Route::delete('/{id}', [MeetingLogController::class, 'destroy'])->name('destroy');

    // 처리 상태 확인 (폴링용)
    Route::get('/{id}/status', [MeetingLogController::class, 'status'])->name('status');

    // 요약 결과 조회 (HTMX 지원)
    Route::get('/{id}/summary', [MeetingLogController::class, 'summary'])->name('summary');
});

/*
|--------------------------------------------------------------------------
| HR (인사관리) API
|--------------------------------------------------------------------------
*/
Route::middleware(['web', 'auth', 'hq.member'])->prefix('admin/hr/employees')->name('api.admin.hr.employees.')->group(function () {
    Route::get('/search-users', [\App\Http\Controllers\Api\Admin\HR\EmployeeController::class, 'searchUsers'])->name('search-users');
    Route::get('/stats', [\App\Http\Controllers\Api\Admin\HR\EmployeeController::class, 'stats'])->name('stats');
    Route::get('/tenure', [\App\Http\Controllers\Api\Admin\HR\EmployeeController::class, 'tenure'])->name('tenure');
    Route::get('/tenure-export', [\App\Http\Controllers\Api\Admin\HR\EmployeeController::class, 'tenureExport'])->name('tenure-export');
    Route::get('/', [\App\Http\Controllers\Api\Admin\HR\EmployeeController::class, 'index'])->name('index');
    Route::post('/', [\App\Http\Controllers\Api\Admin\HR\EmployeeController::class, 'store'])->name('store');
    Route::get('/{id}', [\App\Http\Controllers\Api\Admin\HR\EmployeeController::class, 'show'])->name('show');
    Route::put('/{id}', [\App\Http\Controllers\Api\Admin\HR\EmployeeController::class, 'update'])->name('update');
    Route::delete('/{id}', [\App\Http\Controllers\Api\Admin\HR\EmployeeController::class, 'destroy'])->name('destroy');

    // 첨부파일
    Route::post('/{id}/files', [\App\Http\Controllers\Api\Admin\HR\EmployeeController::class, 'uploadFile'])->name('upload-file');
    Route::delete('/{id}/files/{fileId}', [\App\Http\Controllers\Api\Admin\HR\EmployeeController::class, 'deleteFile'])->name('delete-file');
    Route::get('/{id}/files/{fileId}/download', [\App\Http\Controllers\Api\Admin\HR\EmployeeController::class, 'downloadFile'])->name('download-file');
});

// 직급/직책 관리 API
Route::middleware(['web', 'auth', 'hq.member'])->prefix('admin/hr/positions')->name('api.admin.hr.positions.')->group(function () {
    Route::post('/', [\App\Http\Controllers\Api\Admin\HR\EmployeeController::class, 'storePosition'])->name('store');
});

// 근태현황 API
Route::middleware(['web', 'auth', 'hq.member'])->prefix('admin/hr/attendances')->name('api.admin.hr.attendances.')->group(function () {
    Route::get('/stats', [\App\Http\Controllers\Api\Admin\HR\AttendanceController::class, 'stats'])->name('stats');
    Route::get('/calendar', [\App\Http\Controllers\Api\Admin\HR\AttendanceController::class, 'calendar'])->name('calendar');
    Route::get('/summary', [\App\Http\Controllers\Api\Admin\HR\AttendanceController::class, 'summary'])->name('summary');
    Route::get('/overtime-alerts', [\App\Http\Controllers\Api\Admin\HR\AttendanceController::class, 'overtimeAlerts'])->name('overtime-alerts');
    Route::get('/export', [\App\Http\Controllers\Api\Admin\HR\AttendanceController::class, 'export'])->name('export');
    Route::get('/leave-balance/{userId}', [\App\Http\Controllers\Api\Admin\HR\AttendanceController::class, 'leaveBalance'])->name('leave-balance');
    Route::post('/bulk-store', [\App\Http\Controllers\Api\Admin\HR\AttendanceController::class, 'bulkStore'])->name('bulk-store');
    Route::post('/bulk-delete', [\App\Http\Controllers\Api\Admin\HR\AttendanceController::class, 'bulkDestroy'])->name('bulk-delete');
    Route::get('/', [\App\Http\Controllers\Api\Admin\HR\AttendanceController::class, 'index'])->name('index');
    Route::post('/', [\App\Http\Controllers\Api\Admin\HR\AttendanceController::class, 'store'])->name('store');
    Route::put('/{id}', [\App\Http\Controllers\Api\Admin\HR\AttendanceController::class, 'update'])->name('update');
    Route::delete('/{id}', [\App\Http\Controllers\Api\Admin\HR\AttendanceController::class, 'destroy'])->name('destroy');
});

// 근태 신청/승인 API
Route::middleware(['web', 'auth', 'hq.member'])->prefix('admin/hr/attendance-requests')->name('api.admin.hr.attendances.requests.')->group(function () {
    Route::get('/', [\App\Http\Controllers\Api\Admin\HR\AttendanceRequestController::class, 'index'])->name('index');
    Route::post('/', [\App\Http\Controllers\Api\Admin\HR\AttendanceRequestController::class, 'store'])->name('store');
    Route::post('/{id}/approve', [\App\Http\Controllers\Api\Admin\HR\AttendanceRequestController::class, 'approve'])->name('approve');
    Route::post('/{id}/reject', [\App\Http\Controllers\Api\Admin\HR\AttendanceRequestController::class, 'reject'])->name('reject');
});

// 휴가관리 API
Route::middleware(['web', 'auth', 'hq.member'])->prefix('admin/hr/leaves')->name('api.admin.hr.leaves.')->group(function () {
    Route::get('/balance', [\App\Http\Controllers\Api\Admin\HR\LeaveController::class, 'balance'])->name('balance');
    Route::get('/balance/{userId}', [\App\Http\Controllers\Api\Admin\HR\LeaveController::class, 'userBalance'])->name('user-balance');
    Route::get('/stats', [\App\Http\Controllers\Api\Admin\HR\LeaveController::class, 'stats'])->name('stats');
    Route::get('/export', [\App\Http\Controllers\Api\Admin\HR\LeaveController::class, 'export'])->name('export');
    Route::get('/', [\App\Http\Controllers\Api\Admin\HR\LeaveController::class, 'index'])->name('index');
    Route::post('/', [\App\Http\Controllers\Api\Admin\HR\LeaveController::class, 'store'])->name('store');
    Route::post('/{id}/approve', [\App\Http\Controllers\Api\Admin\HR\LeaveController::class, 'approve'])->name('approve');
    Route::post('/{id}/reject', [\App\Http\Controllers\Api\Admin\HR\LeaveController::class, 'reject'])->name('reject');
    Route::post('/{id}/cancel', [\App\Http\Controllers\Api\Admin\HR\LeaveController::class, 'cancel'])->name('cancel');
});

// 급여관리 API
Route::middleware(['web', 'auth', 'hq.member'])->prefix('admin/hr/payrolls')->name('api.admin.hr.payrolls.')->group(function () {
    Route::get('/stats', [\App\Http\Controllers\Api\Admin\HR\PayrollController::class, 'stats'])->name('stats');
    Route::get('/export', [\App\Http\Controllers\Api\Admin\HR\PayrollController::class, 'export'])->name('export');
    Route::post('/bulk-generate', [\App\Http\Controllers\Api\Admin\HR\PayrollController::class, 'bulkGenerate'])->name('bulk-generate');
    Route::post('/calculate', [\App\Http\Controllers\Api\Admin\HR\PayrollController::class, 'calculate'])->name('calculate');
    Route::get('/', [\App\Http\Controllers\Api\Admin\HR\PayrollController::class, 'index'])->name('index');
    Route::post('/', [\App\Http\Controllers\Api\Admin\HR\PayrollController::class, 'store'])->name('store');
    Route::put('/{id}', [\App\Http\Controllers\Api\Admin\HR\PayrollController::class, 'update'])->name('update');
    Route::delete('/{id}', [\App\Http\Controllers\Api\Admin\HR\PayrollController::class, 'destroy'])->name('destroy');
    Route::post('/{id}/confirm', [\App\Http\Controllers\Api\Admin\HR\PayrollController::class, 'confirm'])->name('confirm');
    Route::post('/{id}/pay', [\App\Http\Controllers\Api\Admin\HR\PayrollController::class, 'pay'])->name('pay');
});

// 급여 설정 API
Route::middleware(['web', 'auth', 'hq.member'])->prefix('admin/hr/payroll-settings')->name('api.admin.hr.payroll-settings.')->group(function () {
    Route::get('/', [\App\Http\Controllers\Api\Admin\HR\PayrollController::class, 'settingsIndex'])->name('index');
    Route::put('/', [\App\Http\Controllers\Api\Admin\HR\PayrollController::class, 'settingsUpdate'])->name('update');
});
