<?php

use App\Http\Controllers\Additional\DocxController;
use App\Http\Controllers\Additional\KioskController;
use App\Http\Controllers\Additional\NotionSearchController;
use App\Http\Controllers\Additional\PptxController;
use App\Http\Controllers\Additional\RagSearchController;
use App\Http\Controllers\Api\BusinessCardOcrController;
use App\Http\Controllers\ApiLogController;
use App\Http\Controllers\AppVersionController;
use App\Http\Controllers\ArchivedRecordController;
use App\Http\Controllers\AuditLogController;
use App\Http\Controllers\Auth\LoginController;
use App\Http\Controllers\BoardController;
use App\Http\Controllers\CategoryController;
use App\Http\Controllers\CategorySyncController;
use App\Http\Controllers\CommonCodeController;
use App\Http\Controllers\CommonCodeSyncController;
use App\Http\Controllers\CustomerCenterController;
use App\Http\Controllers\DailyLogController;
use App\Http\Controllers\DepartmentController;
use App\Http\Controllers\DevTools\ApiExplorerController;
use App\Http\Controllers\DevTools\FlowTesterController;
use App\Http\Controllers\DocumentController;
use App\Http\Controllers\DocumentTemplateController;
use App\Http\Controllers\ESign\EsignApiController;
use App\Http\Controllers\ESign\EsignController;
use App\Http\Controllers\ESign\EsignPublicController;
use App\Http\Controllers\FcmController;
use App\Http\Controllers\ItemFieldController;
use App\Http\Controllers\ItemManagementController;
use App\Http\Controllers\Juil\ConstructionSitePhotoController;
use App\Http\Controllers\Juil\MeetingMinuteController;
use App\Http\Controllers\Juil\PlanningController;
use App\Http\Controllers\Lab\StrategyController;
use App\Http\Controllers\MenuController;
use App\Http\Controllers\MenuSyncController;
use App\Http\Controllers\NumberingRuleController;
use App\Http\Controllers\PermissionController;
use App\Http\Controllers\PostController;
use App\Http\Controllers\ProfileController;
use App\Http\Controllers\ProjectManagementController;
use App\Http\Controllers\QuoteFormulaController;
use App\Http\Controllers\RoleController;
use App\Http\Controllers\RolePermissionController;
use App\Http\Controllers\Sales\SalesProductController;
use App\Http\Controllers\Stats\StatDashboardController;
use App\Http\Controllers\System\AiConfigController;
use App\Http\Controllers\System\AiTokenUsageController;
use App\Http\Controllers\System\AiVoiceRecordingController;
use App\Http\Controllers\System\HolidayController;
use App\Http\Controllers\System\SystemAlertController;
use App\Http\Controllers\System\SystemGuideController;
use App\Http\Controllers\TenantController;
use App\Http\Controllers\TenantSettingController;
use App\Http\Controllers\TriggerAuditController;
use App\Http\Controllers\UserController;
use Illuminate\Support\Facades\Route;

/*
|--------------------------------------------------------------------------
| Guest Routes (인증 불필요)
|--------------------------------------------------------------------------
*/

Route::middleware('guest')->group(function () {
    Route::get('/login', [LoginController::class, 'showLoginForm'])->name('login');
    Route::post('/login', [LoginController::class, 'login']);
});

/*
|--------------------------------------------------------------------------
| Session Refresh Route (인증 미들웨어 없음)
|--------------------------------------------------------------------------
| 세션 만료 시 Remember Token으로 재인증을 시도합니다.
| HTMX 401 에러 핸들러에서 호출됩니다.
*/
Route::post('/auth/refresh-session', [LoginController::class, 'refreshSession'])
    ->name('auth.refresh-session');

/*
|--------------------------------------------------------------------------
| Menu Sync API (외부 서버 호출용, API Key 인증)
|--------------------------------------------------------------------------
*/
Route::prefix('menu-sync')->group(function () {
    Route::get('/export', [MenuSyncController::class, 'export']);
    Route::post('/import', [MenuSyncController::class, 'import']);
});

Route::prefix('common-code-sync')->group(function () {
    Route::get('/export', [CommonCodeSyncController::class, 'export']);
    Route::post('/import', [CommonCodeSyncController::class, 'import']);
});

Route::prefix('category-sync')->group(function () {
    Route::get('/export', [CategorySyncController::class, 'export']);
    Route::post('/import', [CategorySyncController::class, 'import']);
});

/*
|--------------------------------------------------------------------------
| Authenticated Routes (인증 필요)
|--------------------------------------------------------------------------
| - auth: 기본 인증 확인
| - hq.member: 본사(HQ) 테넌트 소속 확인
*/

// GET /logout 요청 시 로그인 페이지로 리다이렉트
Route::get('/logout', fn () => redirect()->route('login'));

Route::middleware(['auth', 'hq.member', 'password.changed'])->group(function () {
    Route::post('/logout', [LoginController::class, 'logout'])->name('logout');

    // 테넌트 전환
    Route::post('/tenant/switch', [TenantController::class, 'switch'])->name('tenant.switch');

    // 프로필 설정
    Route::prefix('profile')->name('profile.')->group(function () {
        Route::get('/', [ProfileController::class, 'index'])->name('index');
        Route::post('/update', [ProfileController::class, 'update'])->name('update');
        Route::post('/password', [ProfileController::class, 'changePassword'])->name('password');
    });

    // 테넌트 관리 (Blade 화면만)
    Route::prefix('tenants')->name('tenants.')->group(function () {
        Route::get('/', [TenantController::class, 'index'])->name('index');
        Route::get('/create', [TenantController::class, 'create'])->name('create');
        Route::get('/{id}/edit', [TenantController::class, 'edit'])->name('edit');
    });

    // 역할 관리 (Blade 화면만)
    Route::prefix('roles')->name('roles.')->group(function () {
        Route::get('/', [RoleController::class, 'index'])->name('index');
        Route::get('/create', [RoleController::class, 'create'])->name('create');
        Route::get('/{id}/edit', [RoleController::class, 'edit'])->name('edit');
    });

    // 부서 관리 (Blade 화면만)
    Route::prefix('departments')->name('departments.')->group(function () {
        Route::get('/', [DepartmentController::class, 'index'])->name('index');
        Route::get('/create', [DepartmentController::class, 'create'])->name('create');
        Route::get('/{id}/edit', [DepartmentController::class, 'edit'])->name('edit');
    });

    // 채번 규칙 관리 (Blade 화면만)
    Route::prefix('numbering-rules')->name('numbering-rules.')->group(function () {
        Route::get('/', [NumberingRuleController::class, 'index'])->name('index');
        Route::get('/create', [NumberingRuleController::class, 'create'])->name('create');
        Route::get('/{id}/edit', [NumberingRuleController::class, 'edit'])->name('edit');
    });

    // 사용자 관리 (Blade 화면만)
    Route::prefix('users')->name('users.')->group(function () {
        Route::get('/', [UserController::class, 'index'])->name('index');
        Route::get('/create', [UserController::class, 'create'])->name('create');
        Route::get('/{id}/edit', [UserController::class, 'edit'])->name('edit');
    });

    // 메뉴 관리 (Blade 화면만)
    Route::prefix('menus')->name('menus.')->group(function () {
        Route::get('/', [MenuController::class, 'index'])->name('index');
        Route::get('/create', [MenuController::class, 'create'])->name('create');
        Route::get('/{id}/edit', [MenuController::class, 'edit'])->name('edit');

        // 글로벌 메뉴 관리 (슈퍼관리자 전용)
        Route::get('/global', [MenuController::class, 'globalIndex'])->name('global.index');
        Route::get('/global/create', [MenuController::class, 'globalCreate'])->name('global.create');
        Route::get('/global/{id}/edit', [MenuController::class, 'globalEdit'])->name('global.edit');

        // 메뉴 동기화
        Route::prefix('sync')->name('sync.')->group(function () {
            Route::get('/', [MenuSyncController::class, 'index'])->name('index');
            Route::post('/settings', [MenuSyncController::class, 'saveSettings'])->name('settings');
            Route::post('/test', [MenuSyncController::class, 'testConnection'])->name('test');
            Route::post('/push', [MenuSyncController::class, 'push'])->name('push');
            Route::post('/pull', [MenuSyncController::class, 'pull'])->name('pull');
        });
    });

    // 권한 관리 (Blade 화면만)
    Route::prefix('permissions')->name('permissions.')->group(function () {
        Route::get('/', [PermissionController::class, 'index'])->name('index');
        Route::get('/create', [PermissionController::class, 'create'])->name('create');
        Route::get('/{id}/edit', [PermissionController::class, 'edit'])->name('edit');
    });

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

    // 고객센터 게시판 리다이렉트 (board_code → boards/{code}/posts)
    Route::get('/customer-center/{code}', function (string $code) {
        $board = \App\Models\Boards\Board::where('board_code', $code)
            ->whereNull('tenant_id')
            ->where('is_active', true)
            ->firstOrFail();

        return redirect()->route('boards.posts.index', $board->board_code);
    })->name('customer-center.board');

    // 시스템 게시판 관리 (Blade 화면만)
    Route::prefix('boards')->name('boards.')->group(function () {
        Route::get('/', [BoardController::class, 'index'])->name('index');
        Route::get('/create', [BoardController::class, 'create'])->name('create');
        Route::get('/{id}/edit', [BoardController::class, 'edit'])->name('edit');

        // 사이드바 메뉴에서 접근 시 리다이렉트 (board_code → boards/{boardCode}/posts?t=)
        // 좌측 메뉴는 로그인 회원의 테넌트 게시판 사용
        Route::get('/{code}', function (string $code) {
            // 숫자만 있는 경우 (기존 라우트와 충돌 방지)
            if (is_numeric($code)) {
                abort(404);
            }

            // 로그인 회원의 tenant_id 사용 (사이드바 메뉴용)
            // SidebarMenuService와 동일하게 tenant_id가 null이면 1 사용
            $userTenantId = auth()->user()?->tenant_id ?? 1;

            // 시스템 게시판 우선, 그 다음 로그인 회원의 테넌트 게시판
            $board = \App\Models\Boards\Board::where('board_code', $code)
                ->whereNull('tenant_id')
                ->where('is_active', true)
                ->first();

            if (! $board) {
                $board = \App\Models\Boards\Board::where('board_code', $code)
                    ->where('tenant_id', $userTenantId)
                    ->where('is_active', true)
                    ->firstOrFail();
            }

            // t 파라미터 포함하여 리다이렉트
            return redirect()->route('boards.posts.index', [
                'boardCode' => $board->board_code,
                't' => $board->tenant_id,
            ]);
        })->name('tenant-board')->where('code', '[a-zA-Z][a-zA-Z0-9_-]*');

        // 게시글 CRUD (board 하위 중첩 라우트)
        // board_code + tenant_id(query param 't')로 게시판 조회
        // tenant_id 없으면 로그인 회원의 tenant_id 사용
        Route::prefix('{boardCode}/posts')->name('posts.')->group(function () {
            Route::get('/', [PostController::class, 'index'])->name('index');
            Route::get('/create', [PostController::class, 'create'])->name('create');
            Route::post('/', [PostController::class, 'store'])->name('store');
            Route::get('/{post}', [PostController::class, 'show'])->name('show');
            Route::get('/{post}/edit', [PostController::class, 'edit'])->name('edit');
            Route::put('/{post}', [PostController::class, 'update'])->name('update');
            Route::delete('/{post}', [PostController::class, 'destroy'])->name('destroy');

            // 슈퍼관리자 전용: 복원/영구삭제
            Route::post('/{post}/restore', [PostController::class, 'restore'])->name('restore');
            Route::delete('/{post}/force', [PostController::class, 'forceDestroy'])->name('forceDestroy');

            // 파일 관리
            Route::prefix('{post}/files')->name('files.')->group(function () {
                Route::get('/{fileId}/download', [PostController::class, 'downloadFile'])->name('download');
                Route::get('/{fileId}/preview', [PostController::class, 'previewFile'])->name('preview');
                Route::post('/', [PostController::class, 'uploadFiles'])->name('upload');
                Route::delete('/{fileId}', [PostController::class, 'deleteFile'])->name('delete');
            });

            // 댓글 관리
            Route::prefix('{post}/comments')->name('comments.')->group(function () {
                Route::post('/', [PostController::class, 'storeComment'])->name('store');
                Route::put('/{comment}', [PostController::class, 'updateComment'])->name('update');
                Route::delete('/{comment}', [PostController::class, 'destroyComment'])->name('destroy');
            });
        })->where('boardCode', '[a-zA-Z][a-zA-Z0-9_-]*');
    });

    // 역할 권한 관리 (Blade 화면만)
    Route::get('/role-permissions', [RolePermissionController::class, 'index'])->name('role-permissions.index');

    // 부서 권한 관리 (Blade 화면만)
    Route::get('/department-permissions', [\App\Http\Controllers\DepartmentPermissionController::class, 'index'])->name('department-permissions.index');

    // 개인 권한 관리 (Blade 화면만)
    Route::get('/user-permissions', [\App\Http\Controllers\UserPermissionController::class, 'index'])->name('user-permissions.index');

    // 권한 분석 (Blade 화면만)
    Route::get('/permission-analyze', [\App\Http\Controllers\PermissionAnalyzeController::class, 'index'])->name('permission-analyze.index');

    // 삭제된 데이터 백업 (Blade 화면만)
    Route::prefix('archived-records')->name('archived-records.')->group(function () {
        Route::get('/', [ArchivedRecordController::class, 'index'])->name('index');
        Route::get('/{batchId}', [ArchivedRecordController::class, 'show'])->name('show');
        Route::get('/{batchId}/restore-check', [ArchivedRecordController::class, 'checkRestore'])->name('restore-check');
        Route::post('/{batchId}/restore', [ArchivedRecordController::class, 'restore'])->name('restore');
    });

    // 감사 로그 (Blade 화면만)
    Route::prefix('audit-logs')->name('audit-logs.')->group(function () {
        Route::get('/', [AuditLogController::class, 'index'])->name('index');
        Route::get('/{id}', [AuditLogController::class, 'show'])->name('show');
    });

    // 트리거 감사 로그 (DB 레벨 변경 추적)
    Route::prefix('trigger-audit')->name('trigger-audit.')->group(function () {
        Route::get('/', [TriggerAuditController::class, 'index'])->name('index');

        // 4.4 트리거 관리
        Route::get('/triggers', [TriggerAuditController::class, 'triggers'])->name('triggers');
        Route::post('/triggers/regenerate', [TriggerAuditController::class, 'regenerateTrigger'])->name('triggers.regenerate');
        Route::post('/triggers/drop', [TriggerAuditController::class, 'dropTrigger'])->name('triggers.drop');

        // 4.6 파티션 관리
        Route::get('/partitions', [TriggerAuditController::class, 'partitions'])->name('partitions');
        Route::post('/partitions/add', [TriggerAuditController::class, 'addPartitions'])->name('partitions.add');
        Route::post('/partitions/drop', [TriggerAuditController::class, 'dropPartition'])->name('partitions.drop');

        // 작업 단위 일괄 롤백
        Route::get('/operation/{operationId}', [TriggerAuditController::class, 'operation'])->name('operation');
        Route::post('/operation/{operationId}/batch-rollback', [TriggerAuditController::class, 'batchRollbackExecute'])->name('batch-rollback');

        // 기존 (동적 라우트는 정적 뒤에 배치)
        Route::get('/{id}', [TriggerAuditController::class, 'show'])->name('show')->whereNumber('id');
        Route::get('/{id}/rollback-preview', [TriggerAuditController::class, 'rollbackPreview'])->name('rollback-preview')->whereNumber('id');
        Route::post('/{id}/rollback', [TriggerAuditController::class, 'rollbackExecute'])->name('rollback-execute')->whereNumber('id');
        Route::get('/{tableName}/{rowId}/history', [TriggerAuditController::class, 'recordHistory'])->name('history');
    });

    // 프로젝트 관리 (Blade 화면만)
    Route::prefix('project-management')->name('pm.')->group(function () {
        // 대시보드
        Route::get('/', [ProjectManagementController::class, 'index'])->name('index');

        // 프로젝트
        Route::get('/projects', [ProjectManagementController::class, 'projects'])->name('projects.index');
        Route::get('/projects/create', [ProjectManagementController::class, 'createProject'])->name('projects.create');
        Route::get('/projects/{id}', [ProjectManagementController::class, 'showProject'])->name('projects.show');
        Route::get('/projects/{id}/edit', [ProjectManagementController::class, 'editProject'])->name('projects.edit');

        // JSON Import
        Route::get('/import', [ProjectManagementController::class, 'import'])->name('import');
    });

    // 일일 스크럼 (Blade 화면만)
    Route::prefix('daily-logs')->name('daily-logs.')->group(function () {
        Route::get('/', [DailyLogController::class, 'index'])->name('index');
        Route::get('/today', [DailyLogController::class, 'today'])->name('today');
        Route::get('/{id}', [DailyLogController::class, 'show'])->name('show');
    });

    // 품목관리 (3-Panel: 리스트 + BOM 트리 + 상세)
    Route::get('/item-management', [ItemManagementController::class, 'index'])->name('item-management.index');

    // 품목기준 필드 관리 (Blade 화면만)
    Route::prefix('item-fields')->name('item-fields.')->group(function () {
        Route::get('/', [ItemFieldController::class, 'index'])->name('index');
    });

    // 견적수식 관리 (Blade 화면만)
    Route::prefix('quote-formulas')->name('quote-formulas.')->group(function () {
        // 수식 관리
        Route::get('/', [QuoteFormulaController::class, 'index'])->name('index');
        Route::get('/create', [QuoteFormulaController::class, 'create'])->name('create');
        Route::get('/{id}/edit', [QuoteFormulaController::class, 'edit'])->name('edit');

        // 카테고리 관리
        Route::get('/categories', [QuoteFormulaController::class, 'categories'])->name('categories.index');
        Route::get('/categories/create', [QuoteFormulaController::class, 'createCategory'])->name('categories.create');
        Route::get('/categories/{id}/edit', [QuoteFormulaController::class, 'editCategory'])->name('categories.edit');

        // 시뮬레이터
        Route::get('/simulator', [QuoteFormulaController::class, 'simulator'])->name('simulator');
    });

    // 테넌트 설정 (재고 설정 등)
    Route::prefix('tenant-settings')->name('tenant-settings.')->group(function () {
        Route::get('/', [TenantSettingController::class, 'index'])->name('index');
        Route::post('/', [TenantSettingController::class, 'store'])->name('store');
    });

    // 공통코드 관리
    Route::prefix('common-codes')->name('common-codes.')->group(function () {
        Route::get('/', [CommonCodeController::class, 'index'])->name('index');
        Route::post('/', [CommonCodeController::class, 'store'])->name('store');
        Route::post('/store-group', [CommonCodeController::class, 'storeGroup'])->name('store-group');
        Route::post('/bulk-copy', [CommonCodeController::class, 'bulkCopy'])->name('bulk-copy');
        Route::post('/bulk-promote', [CommonCodeController::class, 'bulkPromoteToGlobal'])->name('bulk-promote');
        Route::put('/{id}', [CommonCodeController::class, 'update'])->name('update');
        Route::post('/{id}/toggle', [CommonCodeController::class, 'toggle'])->name('toggle');
        Route::post('/{id}/copy', [CommonCodeController::class, 'copy'])->name('copy');
        Route::post('/{id}/promote', [CommonCodeController::class, 'promoteToGlobal'])->name('promote');
        Route::delete('/{id}', [CommonCodeController::class, 'destroy'])->name('destroy');

        // 공통코드 동기화
        Route::prefix('sync')->name('sync.')->group(function () {
            Route::get('/', [CommonCodeSyncController::class, 'index'])->name('index');
            Route::post('/push', [CommonCodeSyncController::class, 'push'])->name('push');
            Route::post('/pull', [CommonCodeSyncController::class, 'pull'])->name('pull');
        });
    });

    // 문서양식 관리
    Route::prefix('document-templates')->name('document-templates.')->group(function () {
        Route::get('/', [DocumentTemplateController::class, 'index'])->name('index');
        Route::get('/create', [DocumentTemplateController::class, 'create'])->name('create');
        Route::get('/{id}/edit', [DocumentTemplateController::class, 'edit'])->name('edit');
    });

    // 문서 관리
    Route::prefix('documents')->name('documents.')->group(function () {
        Route::get('/', [DocumentController::class, 'index'])->name('index');
        Route::get('/create', [DocumentController::class, 'create'])->name('create');
        Route::get('/{id}', [DocumentController::class, 'show'])->whereNumber('id')->name('show');
        Route::get('/{id}/print', [DocumentController::class, 'print'])->whereNumber('id')->name('print');
        Route::get('/{id}/edit', [DocumentController::class, 'edit'])->whereNumber('id')->name('edit');
    });

    // 앱 버전 관리
    Route::prefix('app-versions')->name('app-versions.')->group(function () {
        Route::get('/', [AppVersionController::class, 'index'])->name('index');
        Route::post('/', [AppVersionController::class, 'store'])->name('store');
        Route::put('/{id}', [AppVersionController::class, 'update'])->name('update');
        Route::get('/{id}/download', [AppVersionController::class, 'download'])->name('download');
        Route::post('/{id}/toggle', [AppVersionController::class, 'toggleActive'])->name('toggle');
        Route::delete('/{id}', [AppVersionController::class, 'destroy'])->name('destroy');
        Route::post('/{id}/restore', [AppVersionController::class, 'restore'])->name('restore');
        Route::delete('/{id}/force-delete', [AppVersionController::class, 'forceDestroy'])->name('force-destroy');
    });

    // 통계 대시보드
    Route::get('/stats/dashboard', [StatDashboardController::class, 'index'])->name('stats.dashboard');

    // 시스템 알림 관리
    Route::prefix('system/alerts')->name('system.alerts.')->group(function () {
        Route::get('/', [SystemAlertController::class, 'index'])->name('index');
        Route::post('/{id}/read', [SystemAlertController::class, 'markAsRead'])->name('read');
        Route::post('/{id}/resolve', [SystemAlertController::class, 'resolve'])->name('resolve');
        Route::post('/read-all', [SystemAlertController::class, 'markAllAsRead'])->name('read-all');
    });

    // AI 설정 관리
    Route::prefix('system/ai-config')->name('system.ai-config.')->group(function () {
        Route::get('/', [AiConfigController::class, 'index'])->name('index');
        Route::post('/', [AiConfigController::class, 'store'])->name('store');
        Route::put('/{id}', [AiConfigController::class, 'update'])->name('update');
        Route::delete('/{id}', [AiConfigController::class, 'destroy'])->name('destroy');
        Route::post('/{id}/toggle', [AiConfigController::class, 'toggle'])->name('toggle');
        Route::post('/test', [AiConfigController::class, 'test'])->name('test');
        Route::post('/test-gcs', [AiConfigController::class, 'testGcs'])->name('test-gcs');
    });

    // 달력 휴일 관리
    Route::prefix('system/holidays')->name('system.holidays.')->group(function () {
        Route::get('/', [HolidayController::class, 'index'])->name('index');
        Route::get('/list', [HolidayController::class, 'list'])->name('list');
        Route::post('/', [HolidayController::class, 'store'])->name('store');
        Route::post('/bulk', [HolidayController::class, 'bulkStore'])->name('bulk');
        Route::post('/destroy-year', [HolidayController::class, 'destroyByYear'])->name('destroy-year');
        Route::put('/{id}', [HolidayController::class, 'update'])->name('update');
        Route::delete('/{id}', [HolidayController::class, 'destroy'])->name('destroy');
    });

    // AI 토큰 사용량 관리
    Route::prefix('system/ai-token-usage')->name('system.ai-token-usage.')->group(function () {
        Route::get('/', [AiTokenUsageController::class, 'index'])->name('index');
        Route::get('/list', [AiTokenUsageController::class, 'list'])->name('list');
        Route::get('/pricing', [AiTokenUsageController::class, 'pricingList'])->name('pricing.list');
        Route::put('/pricing', [AiTokenUsageController::class, 'pricingUpdate'])->name('pricing.update');
    });

    // AI 음성녹음 관리
    Route::prefix('system/ai-voice-recording')->name('system.ai-voice-recording.')->group(function () {
        Route::get('/', [AiVoiceRecordingController::class, 'index'])->name('index');
        Route::get('/list', [AiVoiceRecordingController::class, 'list'])->name('list');
        Route::get('/categories', [AiVoiceRecordingController::class, 'categories'])->name('categories');
        Route::post('/', [AiVoiceRecordingController::class, 'store'])->name('store');
        Route::post('/upload', [AiVoiceRecordingController::class, 'uploadFile'])->name('upload');
        Route::get('/{id}', [AiVoiceRecordingController::class, 'show'])->name('show');
        Route::post('/{id}/process', [AiVoiceRecordingController::class, 'processAudio'])->name('process');
        Route::delete('/{id}', [AiVoiceRecordingController::class, 'destroy'])->name('destroy');
        Route::get('/{id}/status', [AiVoiceRecordingController::class, 'status'])->name('status');
        Route::get('/{id}/download', [AiVoiceRecordingController::class, 'download'])->name('download');
    });

    // 시스템 가이드
    Route::get('/system/git-deploy-flow', [SystemGuideController::class, 'gitDeployFlow'])->name('system.guide.git-deploy-flow');

    // 명함 OCR API
    Route::post('/api/business-card-ocr', [BusinessCardOcrController::class, 'process'])->name('api.business-card-ocr');

    // 카테고리 관리
    Route::prefix('categories')->name('categories.')->group(function () {
        Route::get('/', [CategoryController::class, 'index'])->name('index');
        Route::post('/store-group', [CategoryController::class, 'storeGroup'])->name('store-group');

        // 카테고리 동기화
        Route::prefix('sync')->name('sync.')->group(function () {
            Route::get('/', [CategorySyncController::class, 'index'])->name('index');
            Route::post('/push', [CategorySyncController::class, 'push'])->name('push');
            Route::post('/pull', [CategorySyncController::class, 'pull'])->name('pull');
        });
    });

    /*
    |--------------------------------------------------------------------------
    | 바로빌 Routes
    |--------------------------------------------------------------------------
    */
    Route::prefix('barobill')->name('barobill.')->group(function () {
        Route::get('/settings', [\App\Http\Controllers\Barobill\BarobillController::class, 'settings'])->name('settings.index');
        Route::get('/members', [\App\Http\Controllers\Barobill\BarobillController::class, 'members'])->name('members.index');
        Route::get('/tax-invoice', [\App\Http\Controllers\Barobill\BarobillController::class, 'taxInvoice'])->name('tax-invoice.index');
        Route::get('/tax-invoice/search-partners', [\App\Http\Controllers\Barobill\BarobillController::class, 'searchTradingPartners'])->name('tax-invoice.search-partners');
        Route::get('/bank-account', [\App\Http\Controllers\Barobill\BarobillController::class, 'bankAccount'])->name('bank-account.index');
        Route::get('/card-usage', [\App\Http\Controllers\Barobill\BarobillController::class, 'cardUsage'])->name('card-usage.index');
        // Route::get('/hometax', ...) - 아래 hometax 그룹으로 이동됨
        Route::get('/usage', [\App\Http\Controllers\Barobill\BarobillController::class, 'usage'])->name('usage.index');
        Route::get('/billing', [\App\Http\Controllers\Barobill\BarobillController::class, 'billing'])->name('billing.index');
        // 기존 config 라우트 (호환성)
        Route::get('/config', [\App\Http\Controllers\Barobill\BarobillController::class, 'config'])->name('config.index');

        // 전자세금계산서 (React 페이지)
        Route::prefix('etax')->name('etax.')->group(function () {
            Route::get('/', [\App\Http\Controllers\Barobill\EtaxController::class, 'index'])->name('index');
            Route::get('/invoices', [\App\Http\Controllers\Barobill\EtaxController::class, 'getInvoices'])->name('invoices');
            Route::post('/issue', [\App\Http\Controllers\Barobill\EtaxController::class, 'issue'])->name('issue');
            Route::post('/send-to-nts', [\App\Http\Controllers\Barobill\EtaxController::class, 'sendToNts'])->name('send-to-nts');
            Route::post('/delete', [\App\Http\Controllers\Barobill\EtaxController::class, 'delete'])->name('delete');
            Route::get('/supplier', [\App\Http\Controllers\Barobill\EtaxController::class, 'getSupplier'])->name('supplier');
            Route::post('/supplier', [\App\Http\Controllers\Barobill\EtaxController::class, 'updateSupplier'])->name('supplier.update');
        });

        // 계좌 입출금내역 (재무관리로 이동됨 - 데이터 API만 유지)
        Route::prefix('eaccount')->name('eaccount.')->group(function () {
            // index 페이지는 재무관리로 리디렉션
            Route::get('/', function () {
                if (request()->header('HX-Request')) {
                    return response('', 200)->header('HX-Redirect', route('finance.account-transactions'));
                }

                return redirect()->route('finance.account-transactions');
            })->name('index');
            Route::get('/accounts', [\App\Http\Controllers\Barobill\EaccountController::class, 'accounts'])->name('accounts');
            Route::get('/latest-balances', [\App\Http\Controllers\Barobill\EaccountController::class, 'latestBalances'])->name('latest-balances');
            Route::get('/transactions', [\App\Http\Controllers\Barobill\EaccountController::class, 'transactions'])->name('transactions');
            Route::get('/account-codes', [\App\Http\Controllers\Barobill\EaccountController::class, 'accountCodes'])->name('account-codes');
            Route::get('/account-codes/all', [\App\Http\Controllers\Barobill\EaccountController::class, 'accountCodesAll'])->name('account-codes.all');
            Route::post('/account-codes', [\App\Http\Controllers\Barobill\EaccountController::class, 'accountCodeStore'])->name('account-codes.store');
            Route::put('/account-codes/{id}', [\App\Http\Controllers\Barobill\EaccountController::class, 'accountCodeUpdate'])->name('account-codes.update');
            Route::delete('/account-codes/{id}', [\App\Http\Controllers\Barobill\EaccountController::class, 'accountCodeDestroy'])->name('account-codes.destroy');
            Route::post('/save', [\App\Http\Controllers\Barobill\EaccountController::class, 'save'])->name('save');
            Route::get('/export', [\App\Http\Controllers\Barobill\EaccountController::class, 'exportExcel'])->name('export');
            Route::post('/save-override', [\App\Http\Controllers\Barobill\EaccountController::class, 'saveOverride'])->name('save-override');
            // 수동 입력 관련
            Route::post('/manual', [\App\Http\Controllers\Barobill\EaccountController::class, 'storeManual'])->name('manual.store');
            Route::put('/manual/{id}', [\App\Http\Controllers\Barobill\EaccountController::class, 'updateManual'])->name('manual.update');
            Route::delete('/manual/{id}', [\App\Http\Controllers\Barobill\EaccountController::class, 'destroyManual'])->name('manual.destroy');
            // 분개 관련
            Route::get('/splits', [\App\Http\Controllers\Barobill\EaccountController::class, 'splits'])->name('splits');
            Route::post('/splits', [\App\Http\Controllers\Barobill\EaccountController::class, 'saveSplits'])->name('splits.save');
            Route::delete('/splits', [\App\Http\Controllers\Barobill\EaccountController::class, 'deleteSplits'])->name('splits.delete');
            // 거래처 검색
            Route::get('/clients/search', [\App\Http\Controllers\Barobill\EaccountController::class, 'searchClients'])->name('clients.search');
        });

        // 카드 사용내역 (재무관리로 이동됨 - 데이터 API만 유지)
        Route::prefix('ecard')->name('ecard.')->group(function () {
            // index 페이지는 재무관리로 리디렉션
            Route::get('/', function () {
                if (request()->header('HX-Request')) {
                    return response('', 200)->header('HX-Redirect', route('finance.card-transactions'));
                }

                return redirect()->route('finance.card-transactions');
            })->name('index');
            Route::get('/cards', [\App\Http\Controllers\Barobill\EcardController::class, 'cards'])->name('cards');
            Route::get('/transactions', [\App\Http\Controllers\Barobill\EcardController::class, 'transactions'])->name('transactions');
            Route::get('/account-codes', [\App\Http\Controllers\Barobill\EcardController::class, 'accountCodes'])->name('account-codes');
            Route::post('/save', [\App\Http\Controllers\Barobill\EcardController::class, 'save'])->name('save');
            Route::post('/export', [\App\Http\Controllers\Barobill\EcardController::class, 'exportExcel'])->name('export');
            // 분개 관련
            Route::get('/splits', [\App\Http\Controllers\Barobill\EcardController::class, 'splits'])->name('splits');
            Route::post('/splits', [\App\Http\Controllers\Barobill\EcardController::class, 'saveSplits'])->name('splits.save');
            Route::delete('/splits', [\App\Http\Controllers\Barobill\EcardController::class, 'deleteSplits'])->name('splits.delete');
            // 수동 입력 관련
            Route::post('/manual', [\App\Http\Controllers\Barobill\EcardController::class, 'storeManual'])->name('manual.store');
            Route::put('/manual/{id}', [\App\Http\Controllers\Barobill\EcardController::class, 'updateManual'])->name('manual.update');
            Route::delete('/manual/{id}', [\App\Http\Controllers\Barobill\EcardController::class, 'destroyManual'])->name('manual.destroy');
            // 거래 숨김/복원 관련
            Route::post('/hide', [\App\Http\Controllers\Barobill\EcardController::class, 'hideTransaction'])->name('hide');
            Route::post('/restore', [\App\Http\Controllers\Barobill\EcardController::class, 'restoreTransaction'])->name('restore');
            Route::get('/hidden', [\App\Http\Controllers\Barobill\EcardController::class, 'hiddenTransactions'])->name('hidden');
            // 복식부기 분개 (journal_entries 통합)
            Route::post('/journal', [\App\Http\Controllers\Barobill\EcardController::class, 'storeJournal'])->name('journal.store');
            Route::get('/journal', [\App\Http\Controllers\Barobill\EcardController::class, 'getJournal'])->name('journal.show');
            Route::delete('/journal/{id}', [\App\Http\Controllers\Barobill\EcardController::class, 'deleteJournal'])->name('journal.delete');
            Route::get('/journal-statuses', [\App\Http\Controllers\Barobill\EcardController::class, 'getJournalStatuses'])->name('journal.statuses');
        });

        // 홈택스 매출/매입 (React 페이지)
        Route::prefix('hometax')->name('hometax.')->group(function () {
            Route::get('/', [\App\Http\Controllers\Barobill\HometaxController::class, 'index'])->name('index');
            Route::get('/sales', [\App\Http\Controllers\Barobill\HometaxController::class, 'sales'])->name('sales');
            Route::get('/purchases', [\App\Http\Controllers\Barobill\HometaxController::class, 'purchases'])->name('purchases');
            Route::post('/request-collect', [\App\Http\Controllers\Barobill\HometaxController::class, 'requestCollect'])->name('request-collect');
            Route::get('/collect-status', [\App\Http\Controllers\Barobill\HometaxController::class, 'collectStatus'])->name('collect-status');
            Route::post('/export', [\App\Http\Controllers\Barobill\HometaxController::class, 'exportExcel'])->name('export');
            // 홈택스 스크래핑 관련
            Route::get('/scrap-url', [\App\Http\Controllers\Barobill\HometaxController::class, 'getScrapRequestUrl'])->name('scrap-url');
            Route::post('/refresh-scrap', [\App\Http\Controllers\Barobill\HometaxController::class, 'refreshScrap'])->name('refresh-scrap');
            Route::get('/diagnose', [\App\Http\Controllers\Barobill\HometaxController::class, 'diagnose'])->name('diagnose');
            // 로컬 DB 조회 및 동기화
            Route::get('/local-sales', [\App\Http\Controllers\Barobill\HometaxController::class, 'localSales'])->name('local-sales');
            Route::get('/local-purchases', [\App\Http\Controllers\Barobill\HometaxController::class, 'localPurchases'])->name('local-purchases');
            Route::post('/sync', [\App\Http\Controllers\Barobill\HometaxController::class, 'sync'])->name('sync');
            Route::post('/auto-sync', [\App\Http\Controllers\Barobill\HometaxController::class, 'autoSync'])->name('auto-sync');
            Route::post('/update-memo', [\App\Http\Controllers\Barobill\HometaxController::class, 'updateMemo'])->name('update-memo');
            Route::post('/toggle-checked', [\App\Http\Controllers\Barobill\HometaxController::class, 'toggleChecked'])->name('toggle-checked');
            // 수동입력 CRUD
            Route::post('/manual-store', [\App\Http\Controllers\Barobill\HometaxController::class, 'manualStore'])->name('manual-store');
            Route::put('/manual/{id}', [\App\Http\Controllers\Barobill\HometaxController::class, 'manualUpdate'])->name('manual-update');
            Route::delete('/manual/{id}', [\App\Http\Controllers\Barobill\HometaxController::class, 'manualDestroy'])->name('manual-destroy');
            // 분개
            Route::post('/create-journal-entry', [\App\Http\Controllers\Barobill\HometaxController::class, 'createJournalEntry'])->name('create-journal-entry');
            Route::get('/journals', [\App\Http\Controllers\Barobill\HometaxController::class, 'getJournals'])->name('journals');
            Route::delete('/journals', [\App\Http\Controllers\Barobill\HometaxController::class, 'deleteJournals'])->name('journals.delete');
            // 카드내역 참조
            Route::get('/card-transactions', [\App\Http\Controllers\Barobill\HometaxController::class, 'cardTransactions'])->name('card-transactions');
        });

        // 카카오톡 (알림톡/친구톡)
        Route::prefix('kakaotalk')->name('kakaotalk.')->group(function () {
            Route::get('/', [\App\Http\Controllers\Barobill\KakaotalkController::class, 'index'])->name('index');
            Route::get('/channels', [\App\Http\Controllers\Barobill\KakaotalkController::class, 'channels'])->name('channels');
            Route::get('/templates', [\App\Http\Controllers\Barobill\KakaotalkController::class, 'templates'])->name('templates');
            Route::get('/send', [\App\Http\Controllers\Barobill\KakaotalkController::class, 'send'])->name('send');
            Route::get('/history', [\App\Http\Controllers\Barobill\KakaotalkController::class, 'history'])->name('history');
            Route::get('/guide', [\App\Http\Controllers\Barobill\KakaotalkController::class, 'guide'])->name('guide');
        });

        // SMS 발송 테스트
        Route::prefix('sms')->name('sms.')->group(function () {
            Route::get('/send', [\App\Http\Controllers\Barobill\SmsController::class, 'send'])->name('send');
        });
    });

    /*
    |--------------------------------------------------------------------------
    | 신용평가 Routes
    |--------------------------------------------------------------------------
    */
    Route::prefix('credit')->name('credit.')->group(function () {
        // 조회 이력 목록
        Route::get('/inquiry', [\App\Http\Controllers\Credit\CreditController::class, 'inquiry'])->name('inquiry.index');
        Route::post('/inquiry/search', [\App\Http\Controllers\Credit\CreditController::class, 'search'])->name('inquiry.search');
        Route::post('/inquiry/test', [\App\Http\Controllers\Credit\CreditController::class, 'testConnection'])->name('inquiry.test');

        // 조회 이력 상세 데이터
        Route::get('/inquiry/{inquiryKey}/raw', [\App\Http\Controllers\Credit\CreditController::class, 'getRawData'])->name('inquiry.raw');
        Route::get('/inquiry/{inquiryKey}/report', [\App\Http\Controllers\Credit\CreditController::class, 'getReportData'])->name('inquiry.report');
        Route::delete('/inquiry/{id}', [\App\Http\Controllers\Credit\CreditController::class, 'deleteInquiry'])->name('inquiry.destroy');

        // 조회회수 집계
        Route::get('/usage', [\App\Http\Controllers\Credit\CreditUsageController::class, 'index'])->name('usage.index');

        // 설정 관리
        Route::get('/settings', [\App\Http\Controllers\Credit\CreditController::class, 'settings'])->name('settings.index');
        Route::get('/settings/create', [\App\Http\Controllers\Credit\CreditController::class, 'createConfig'])->name('settings.create');
        Route::post('/settings', [\App\Http\Controllers\Credit\CreditController::class, 'storeConfig'])->name('settings.store');
        Route::get('/settings/{id}/edit', [\App\Http\Controllers\Credit\CreditController::class, 'editConfig'])->name('settings.edit');
        Route::put('/settings/{id}', [\App\Http\Controllers\Credit\CreditController::class, 'updateConfig'])->name('settings.update');
        Route::delete('/settings/{id}', [\App\Http\Controllers\Credit\CreditController::class, 'deleteConfig'])->name('settings.destroy');
        Route::post('/settings/{id}/toggle', [\App\Http\Controllers\Credit\CreditController::class, 'toggleConfig'])->name('settings.toggle');
    });

    // 대시보드
    Route::get('/dashboard', function () {
        if (request()->header('HX-Request')) {
            return response('', 200)->header('HX-Redirect', route('dashboard'));
        }

        return view('dashboard.index');
    })->name('dashboard');

    // 대시보드 날씨
    Route::get('/dashboard/weather', [\App\Http\Controllers\DashboardWeatherController::class, 'weather'])->name('dashboard.weather');

    // 대시보드 달력
    Route::get('/dashboard/calendar', [\App\Http\Controllers\DashboardCalendarController::class, 'calendar'])->name('dashboard.calendar');
    Route::post('/dashboard/schedules', [\App\Http\Controllers\DashboardCalendarController::class, 'store'])->name('dashboard.schedules.store');
    Route::get('/dashboard/schedules/{id}', [\App\Http\Controllers\DashboardCalendarController::class, 'show'])->name('dashboard.schedules.show');
    Route::put('/dashboard/schedules/{id}', [\App\Http\Controllers\DashboardCalendarController::class, 'update'])->name('dashboard.schedules.update');
    Route::delete('/dashboard/schedules/{id}', [\App\Http\Controllers\DashboardCalendarController::class, 'destroy'])->name('dashboard.schedules.destroy');

    // 대시보드 일정 첨부파일
    Route::post('/dashboard/schedules/{id}/files', [\App\Http\Controllers\DashboardCalendarController::class, 'uploadFiles'])->name('dashboard.schedules.files.upload');
    Route::delete('/dashboard/schedules/{scheduleId}/files/{fileId}', [\App\Http\Controllers\DashboardCalendarController::class, 'deleteFile'])->name('dashboard.schedules.files.delete');
    Route::get('/dashboard/schedules/{scheduleId}/files/{fileId}/download', [\App\Http\Controllers\DashboardCalendarController::class, 'downloadFile'])->name('dashboard.schedules.files.download');

    // 루트 리다이렉트
    Route::get('/', function () {
        return redirect()->route('dashboard');
    });

    /*
    |--------------------------------------------------------------------------
    | R&D Labs Routes (5130 마이그레이션)
    |--------------------------------------------------------------------------
    */
    Route::prefix('lab')->name('lab.')->group(function () {
        // S. 전략 (Strategy)
        Route::prefix('strategy')->name('strategy.')->group(function () {
            Route::get('/labor', [StrategyController::class, 'labor'])->name('labor');
            Route::get('/chatbot', [StrategyController::class, 'chatbot'])->name('chatbot');
            Route::get('/knowledge-search', [StrategyController::class, 'knowledgeSearch'])->name('knowledge-search');
            Route::get('/chatbot-compare', [StrategyController::class, 'chatbotCompare'])->name('chatbot-compare');
            Route::get('/rag-startups', [StrategyController::class, 'ragStartups'])->name('rag-startups');
            Route::get('/douzone', [StrategyController::class, 'douzone'])->name('douzone');
            Route::get('/confluence-vs-notion', [StrategyController::class, 'confluenceVsNotion'])->name('confluence-vs-notion');
            Route::get('/sales-strategy', [StrategyController::class, 'salesStrategy'])->name('sales-strategy');
        });
    });

    /*
    |--------------------------------------------------------------------------
    | 추가기능 Routes
    |--------------------------------------------------------------------------
    */
    Route::prefix('additional')->name('additional.')->group(function () {
        Route::prefix('kiosk')->name('kiosk.')->group(function () {
            Route::get('/', [KioskController::class, 'index'])->name('index');
            Route::get('/showroom', [KioskController::class, 'showroom'])->name('showroom');
            Route::get('/factory', [KioskController::class, 'factory'])->name('factory');
        });

        Route::prefix('notion-search')->name('notion-search.')->group(function () {
            Route::get('/', [NotionSearchController::class, 'index'])->name('index');
            Route::post('/search', [NotionSearchController::class, 'search'])->name('search');
        });

        Route::prefix('rag')->name('rag.')->group(function () {
            Route::get('/', [RagSearchController::class, 'index'])->name('index');
            Route::post('/search', [RagSearchController::class, 'search'])->name('search');
        });

        Route::prefix('pptx')->name('pptx.')->group(function () {
            Route::get('/', [PptxController::class, 'index'])->name('index');
            Route::get('/download', [PptxController::class, 'download'])->name('download');
        });

        Route::prefix('docx')->name('docx.')->group(function () {
            Route::get('/', [DocxController::class, 'index'])->name('index');
            Route::get('/download', [DocxController::class, 'download'])->name('download');
        });
    });

    /*
    |--------------------------------------------------------------------------
    | FCM 관리 Routes
    |--------------------------------------------------------------------------
    */
    Route::prefix('fcm')->name('fcm.')->group(function () {
        // 토큰 관리
        Route::get('/tokens', [FcmController::class, 'tokens'])->name('tokens');
        Route::get('/tokens/list', [FcmController::class, 'tokenList'])->name('tokens.list');
        Route::get('/tokens/stats', [FcmController::class, 'tokenStats'])->name('tokens.stats');
        Route::post('/tokens/{id}/toggle', [FcmController::class, 'toggleToken'])->name('tokens.toggle');
        Route::delete('/tokens/{id}', [FcmController::class, 'deleteToken'])->name('tokens.delete');
        Route::delete('/tokens-error/bulk', [FcmController::class, 'deleteErrorTokens'])->name('tokens.deleteError');

        // 테스트 발송
        Route::get('/send', [FcmController::class, 'send'])->name('send');
        Route::post('/send', [FcmController::class, 'sendPush'])->name('send.push');
        Route::get('/preview-count', [FcmController::class, 'previewCount'])->name('preview-count');

        // 발송 이력
        Route::get('/history', [FcmController::class, 'history'])->name('history');
        Route::get('/history/list', [FcmController::class, 'historyList'])->name('history.list');
    });

    /*
    |--------------------------------------------------------------------------
    | 개발 도구 Routes
    |--------------------------------------------------------------------------
    */
    Route::prefix('dev-tools')->name('dev-tools.')->group(function () {
        // API 요청 로그
        Route::prefix('api-logs')->name('api-logs.')->group(function () {
            Route::get('/', [ApiLogController::class, 'index'])->name('index');
            Route::post('/prune', [ApiLogController::class, 'prune'])->name('prune');
            Route::post('/truncate', [ApiLogController::class, 'truncate'])->name('truncate');
            Route::get('/{id}', [ApiLogController::class, 'show'])->name('show');
            Route::post('/{id}/resend', [ApiLogController::class, 'resend'])->name('resend');
        });

        // API 플로우 테스터
        Route::prefix('flow-tester')->name('flow-tester.')->group(function () {
            // 고정 경로 먼저 (구체적인 경로)
            Route::get('/', [FlowTesterController::class, 'index'])->name('index');
            Route::get('/create', [FlowTesterController::class, 'create'])->name('create');
            Route::post('/', [FlowTesterController::class, 'store'])->name('store');
            Route::post('/validate-json', [FlowTesterController::class, 'validateJson'])->name('validate-json');

            // 토큰 및 인증 관리 라우트 (API Explorer와 공유)
            Route::post('/login-to-api', [FlowTesterController::class, 'loginToApi'])->name('login-to-api');
            Route::post('/token/save', [FlowTesterController::class, 'saveToken'])->name('token.save');
            Route::post('/token/clear', [FlowTesterController::class, 'clearToken'])->name('token.clear');
            Route::get('/token/status', [FlowTesterController::class, 'tokenStatus'])->name('token.status');

            // /runs/* 관련 라우트 (고정 경로)
            Route::get('/runs/{runId}/status', [FlowTesterController::class, 'runStatus'])->name('run-status');
            Route::get('/runs/{runId}', [FlowTesterController::class, 'runDetail'])->name('run-detail');

            // /{id}/* 관련 라우트 (와일드카드는 마지막에)
            Route::get('/{id}', [FlowTesterController::class, 'edit'])->name('edit');
            Route::put('/{id}', [FlowTesterController::class, 'update'])->name('update');
            Route::delete('/{id}', [FlowTesterController::class, 'destroy'])->name('destroy');
            Route::post('/{id}/clone', [FlowTesterController::class, 'clone'])->name('clone');
            Route::post('/{id}/run', [FlowTesterController::class, 'run'])->name('run');
            Route::get('/{id}/history', [FlowTesterController::class, 'history'])->name('history');
        });

        // API Explorer (OpenAPI 3.0 뷰어)
        Route::prefix('api-explorer')->name('api-explorer.')->group(function () {
            // 메인 뷰
            Route::get('/', [ApiExplorerController::class, 'index'])->name('index');

            // 엔드포인트 (HTMX)
            Route::get('/endpoints', [ApiExplorerController::class, 'endpoints'])->name('endpoints');
            Route::get('/endpoints/{operationId}', [ApiExplorerController::class, 'endpoint'])->name('endpoint');

            // API 실행
            Route::post('/execute', [ApiExplorerController::class, 'execute'])->name('execute');

            // 즐겨찾기
            Route::get('/bookmarks', [ApiExplorerController::class, 'bookmarks'])->name('bookmarks.index');
            Route::post('/bookmarks', [ApiExplorerController::class, 'storeBookmark'])->name('bookmarks.store');
            Route::put('/bookmarks/{id}', [ApiExplorerController::class, 'updateBookmark'])->name('bookmarks.update');
            Route::delete('/bookmarks/{id}', [ApiExplorerController::class, 'deleteBookmark'])->name('bookmarks.destroy');
            Route::post('/bookmarks/toggle', [ApiExplorerController::class, 'toggleBookmark'])->name('bookmarks.toggle');
            Route::post('/bookmarks/reorder', [ApiExplorerController::class, 'reorderBookmarks'])->name('bookmarks.reorder');

            // 템플릿
            Route::get('/templates/{endpoint}', [ApiExplorerController::class, 'templatesForEndpoint'])->name('templates.index');
            Route::post('/templates', [ApiExplorerController::class, 'saveTemplate'])->name('templates.store');
            Route::delete('/templates/{id}', [ApiExplorerController::class, 'deleteTemplate'])->name('templates.destroy');

            // 히스토리
            Route::get('/history', [ApiExplorerController::class, 'history'])->name('history.index');
            Route::post('/history/{id}/replay', [ApiExplorerController::class, 'replayHistory'])->name('history.replay');
            Route::delete('/history', [ApiExplorerController::class, 'clearHistory'])->name('history.clear');

            // 환경 설정
            Route::get('/environments', [ApiExplorerController::class, 'environments'])->name('environments.index');
            Route::post('/environments', [ApiExplorerController::class, 'saveEnvironment'])->name('environments.store');
            Route::delete('/environments/{id}', [ApiExplorerController::class, 'deleteEnvironment'])->name('environments.destroy');

            // 사용자 목록 (인증용)
            Route::get('/users', [ApiExplorerController::class, 'users'])->name('users');
            Route::post('/issue-token', [ApiExplorerController::class, 'issueToken'])->name('issue-token');

            // API 사용 현황 및 폐기 관리
            Route::get('/usage', [ApiExplorerController::class, 'usage'])->name('usage');
            Route::get('/usage/stats', [ApiExplorerController::class, 'usageStats'])->name('usage.stats');
            Route::get('/usage/trend', [ApiExplorerController::class, 'dailyTrend'])->name('usage.trend');
            Route::get('/usage/popular', [ApiExplorerController::class, 'popularApis'])->name('usage.popular');
            Route::get('/usage/stale', [ApiExplorerController::class, 'staleApis'])->name('usage.stale');

            // 폐기 후보 관리
            Route::get('/deprecations', [ApiExplorerController::class, 'deprecations'])->name('deprecations.index');
            Route::post('/deprecations', [ApiExplorerController::class, 'addDeprecation'])->name('deprecations.store');
            Route::post('/deprecations/bulk-unused', [ApiExplorerController::class, 'addAllUnusedAsDeprecation'])->name('deprecations.bulk-unused');
            Route::put('/deprecations/{id}', [ApiExplorerController::class, 'updateDeprecation'])->name('deprecations.update');
            Route::delete('/deprecations/{id}', [ApiExplorerController::class, 'removeDeprecation'])->name('deprecations.destroy');
        });
    });
});

/*
|--------------------------------------------------------------------------
| Demo Pages (데모 페이지)
|--------------------------------------------------------------------------
*/
Route::get('/재무관리', function () {
    return response()->file(public_path('재무관리.html'));
});

Route::get('/일일자금일보', function () {
    return response()->file(public_path('일일자금일보.html'));
});

/*
|--------------------------------------------------------------------------
| HR Routes (인사관리)
|--------------------------------------------------------------------------
*/
Route::middleware('auth')->prefix('hr')->name('hr.')->group(function () {
    // 사원관리
    Route::prefix('employees')->name('employees.')->group(function () {
        Route::get('/', [\App\Http\Controllers\HR\EmployeeController::class, 'index'])->name('index');
        Route::get('/create', [\App\Http\Controllers\HR\EmployeeController::class, 'create'])->name('create');
        Route::get('/{id}', [\App\Http\Controllers\HR\EmployeeController::class, 'show'])->name('show');
        Route::get('/{id}/edit', [\App\Http\Controllers\HR\EmployeeController::class, 'edit'])->name('edit');
    });

    // 입퇴사자 현황
    Route::get('/employee-tenure', [\App\Http\Controllers\HR\EmployeeTenureController::class, 'index'])->name('employee-tenure');

    // 근태현황
    Route::prefix('attendances')->name('attendances.')->group(function () {
        Route::get('/', [\App\Http\Controllers\HR\AttendanceController::class, 'index'])->name('index');
        Route::get('/manage', [\App\Http\Controllers\HR\AttendanceController::class, 'manage'])->name('manage');
    });

    // 휴가관리
    Route::prefix('leaves')->name('leaves.')->group(function () {
        Route::get('/', [\App\Http\Controllers\HR\LeaveController::class, 'index'])->name('index');
    });

    // 급여관리
    Route::prefix('payrolls')->name('payrolls.')->group(function () {
        Route::get('/', [\App\Http\Controllers\HR\PayrollController::class, 'index'])->name('index');
    });
});

/*
|--------------------------------------------------------------------------
| Finance Routes (재무 관리)
|--------------------------------------------------------------------------
*/
Route::middleware('auth')->prefix('finance')->name('finance.')->group(function () {
    // 대시보드
    Route::get('/dashboard', [\App\Http\Controllers\Finance\FinanceDashboardController::class, 'index'])->name('dashboard');

    // 보유계좌관리 (실제 구현)
    Route::prefix('accounts')->name('accounts.')->group(function () {
        Route::get('/', [\App\Http\Controllers\Finance\BankAccountController::class, 'index'])->name('index');
        Route::get('/create', [\App\Http\Controllers\Finance\BankAccountController::class, 'create'])->name('create');
        Route::get('/{id}', [\App\Http\Controllers\Finance\BankAccountController::class, 'show'])->name('show');
        Route::get('/{id}/edit', [\App\Http\Controllers\Finance\BankAccountController::class, 'edit'])->name('edit');
    });

    // 계좌입출금내역 (바로빌 EaccountController 직접 사용)
    Route::get('/account-transactions', [\App\Http\Controllers\Barobill\EaccountController::class, 'index'])->name('account-transactions');

    // 자금계획일정 (실제 구현)
    Route::prefix('fund-schedules')->name('fund-schedules.')->group(function () {
        Route::get('/', [\App\Http\Controllers\Finance\FundScheduleController::class, 'index'])->name('index');
        Route::get('/create', [\App\Http\Controllers\Finance\FundScheduleController::class, 'create'])->name('create');
        Route::get('/{id}', [\App\Http\Controllers\Finance\FundScheduleController::class, 'show'])->name('show');
        Route::get('/{id}/edit', [\App\Http\Controllers\Finance\FundScheduleController::class, 'edit'])->name('edit');
    });

    // 기존 fund-schedule URL 리다이렉트 (호환성)
    Route::get('/fund-schedule', fn () => redirect()->route('finance.fund-schedules.index'))->name('fund-schedule');
    Route::get('/daily-fund', function () {
        // HTMX 요청이면 전체 페이지로 리다이렉트 (React 스크립트 로딩을 위해)
        if (request()->header('HX-Request')) {
            return response('', 200)->header('HX-Redirect', route('finance.daily-fund'));
        }

        return view('finance.daily-fund');
    })->name('daily-fund');

    // 일일자금일보 API
    Route::prefix('daily-fund')->name('daily-fund.')->group(function () {
        Route::get('/list', [\App\Http\Controllers\Finance\DailyFundController::class, 'index'])->name('list');
        Route::get('/period-report', [\App\Http\Controllers\Finance\DailyFundController::class, 'periodReport'])->name('period-report');
        Route::post('/store', [\App\Http\Controllers\Finance\DailyFundController::class, 'store'])->name('store');
        Route::put('/{id}', [\App\Http\Controllers\Finance\DailyFundController::class, 'update'])->name('update');
        Route::delete('/{id}', [\App\Http\Controllers\Finance\DailyFundController::class, 'destroy'])->name('destroy');
        Route::post('/memo', [\App\Http\Controllers\Finance\DailyFundController::class, 'saveMemo'])->name('memo');
    });

    // 일반전표입력
    Route::get('/journal-entries', function () {
        if (request()->header('HX-Request')) {
            return response('', 200)->header('HX-Redirect', route('finance.journal-entries'));
        }

        return view('finance.journal-entries');
    })->name('journal-entries');

    Route::prefix('journal-entries')->name('journal-entries.')->group(function () {
        Route::get('/list', [\App\Http\Controllers\Finance\JournalEntryController::class, 'index'])->name('list');
        Route::get('/next-entry-no', [\App\Http\Controllers\Finance\JournalEntryController::class, 'nextEntryNo'])->name('next-entry-no');
        Route::get('/account-codes', [\App\Http\Controllers\Finance\JournalEntryController::class, 'accountCodes'])->name('account-codes');
        Route::get('/account-codes/all', [\App\Http\Controllers\Finance\JournalEntryController::class, 'accountCodesAll'])->name('account-codes.all');
        Route::post('/account-codes', [\App\Http\Controllers\Finance\JournalEntryController::class, 'accountCodeStore'])->name('account-codes.store');
        Route::put('/account-codes/{id}', [\App\Http\Controllers\Finance\JournalEntryController::class, 'accountCodeUpdate'])->name('account-codes.update');
        Route::delete('/account-codes/{id}', [\App\Http\Controllers\Finance\JournalEntryController::class, 'accountCodeDestroy'])->name('account-codes.destroy');
        Route::get('/trading-partners', [\App\Http\Controllers\Finance\JournalEntryController::class, 'tradingPartners'])->name('trading-partners');

        // 은행거래 기반 분개
        Route::get('/bank-transactions', [\App\Http\Controllers\Finance\JournalEntryController::class, 'bankTransactions'])->name('bank-transactions');
        Route::post('/store-from-bank', [\App\Http\Controllers\Finance\JournalEntryController::class, 'storeFromBank'])->name('store-from-bank');
        Route::get('/bank-journals', [\App\Http\Controllers\Finance\JournalEntryController::class, 'bankJournals'])->name('bank-journals');
        Route::delete('/bank-journal/{id}', [\App\Http\Controllers\Finance\JournalEntryController::class, 'deleteBankJournal'])->name('delete-bank-journal');

        // 카드거래 기반 분개
        Route::get('/card-transactions', [\App\Http\Controllers\Finance\JournalEntryController::class, 'cardTransactions'])->name('card-transactions');
        Route::post('/store-from-card', [\App\Http\Controllers\Finance\JournalEntryController::class, 'storeFromCard'])->name('store-from-card');
        Route::get('/card-journals', [\App\Http\Controllers\Finance\JournalEntryController::class, 'cardJournals'])->name('card-journals');
        Route::delete('/card-journal/{id}', [\App\Http\Controllers\Finance\JournalEntryController::class, 'deleteCardJournal'])->name('delete-card-journal');

        Route::get('/{id}', [\App\Http\Controllers\Finance\JournalEntryController::class, 'show'])->name('show');
        Route::post('/store', [\App\Http\Controllers\Finance\JournalEntryController::class, 'store'])->name('store');
        Route::put('/{id}', [\App\Http\Controllers\Finance\JournalEntryController::class, 'update'])->name('update');
        Route::delete('/{id}', [\App\Http\Controllers\Finance\JournalEntryController::class, 'destroy'])->name('destroy');
    });

    // 카드관리
    Route::get('/corporate-cards', function () {
        if (request()->header('HX-Request')) {
            return response('', 200)->header('HX-Redirect', route('finance.corporate-cards'));
        }

        return view('finance.corporate-cards');
    })->name('corporate-cards');

    // 법인카드 API
    Route::prefix('corporate-cards')->name('corporate-cards.')->group(function () {
        Route::get('/list', [\App\Http\Controllers\Finance\CorporateCardController::class, 'index'])->name('list');
        Route::get('/summary', [\App\Http\Controllers\Finance\CorporateCardController::class, 'summary'])->name('summary');
        Route::post('/prepayment', [\App\Http\Controllers\Finance\CorporateCardController::class, 'updatePrepayment'])->name('prepayment');
        Route::post('/store', [\App\Http\Controllers\Finance\CorporateCardController::class, 'store'])->name('store');
        Route::put('/{id}', [\App\Http\Controllers\Finance\CorporateCardController::class, 'update'])->name('update');
        Route::post('/{id}/deactivate', [\App\Http\Controllers\Finance\CorporateCardController::class, 'deactivate'])->name('deactivate');
        Route::delete('/{id}', [\App\Http\Controllers\Finance\CorporateCardController::class, 'destroy'])->name('destroy');
    });

    // 카드사용내역 (바로빌 EcardController 직접 사용)
    Route::get('/card-transactions', [\App\Http\Controllers\Barobill\EcardController::class, 'index'])->name('card-transactions');

    // 법인카드 거래내역 API
    Route::prefix('card-transactions')->name('card-transactions.')->group(function () {
        Route::get('/list', [\App\Http\Controllers\Finance\CardTransactionController::class, 'index'])->name('list');
        Route::post('/store', [\App\Http\Controllers\Finance\CardTransactionController::class, 'store'])->name('store');
        Route::put('/{id}', [\App\Http\Controllers\Finance\CardTransactionController::class, 'update'])->name('update');
        Route::delete('/{id}', [\App\Http\Controllers\Finance\CardTransactionController::class, 'destroy'])->name('destroy');
    });

    // 수입/지출
    Route::get('/income', function () {
        if (request()->header('HX-Request')) {
            return response('', 200)->header('HX-Redirect', route('finance.income'));
        }

        return view('finance.income');
    })->name('income');

    // 수입 관리 API
    Route::prefix('income')->name('income.')->group(function () {
        Route::get('/list', [\App\Http\Controllers\Finance\IncomeController::class, 'index'])->name('list');
        Route::post('/store', [\App\Http\Controllers\Finance\IncomeController::class, 'store'])->name('store');
        Route::put('/{id}', [\App\Http\Controllers\Finance\IncomeController::class, 'update'])->name('update');
        Route::delete('/{id}', [\App\Http\Controllers\Finance\IncomeController::class, 'destroy'])->name('destroy');
    });

    Route::get('/expense', function () {
        if (request()->header('HX-Request')) {
            return response('', 200)->header('HX-Redirect', route('finance.expense'));
        }

        return view('finance.expense');
    })->name('expense');

    // 지출 관리 API
    Route::prefix('expense')->name('expense.')->group(function () {
        Route::get('/list', [\App\Http\Controllers\Finance\ExpenseController::class, 'index'])->name('list');
        Route::post('/store', [\App\Http\Controllers\Finance\ExpenseController::class, 'store'])->name('store');
        Route::put('/{id}', [\App\Http\Controllers\Finance\ExpenseController::class, 'update'])->name('update');
        Route::delete('/{id}', [\App\Http\Controllers\Finance\ExpenseController::class, 'destroy'])->name('destroy');
    });

    // 매출/매입
    Route::get('/sales', function () {
        if (request()->header('HX-Request')) {
            return response('', 200)->header('HX-Redirect', route('finance.sales'));
        }

        return view('finance.sales');
    })->name('sales');

    // 매출 관리 API
    Route::prefix('sales-records')->name('sales-records.')->group(function () {
        Route::get('/list', [\App\Http\Controllers\Finance\SalesRecordController::class, 'index'])->name('list');
        Route::post('/store', [\App\Http\Controllers\Finance\SalesRecordController::class, 'store'])->name('store');
        Route::put('/{id}', [\App\Http\Controllers\Finance\SalesRecordController::class, 'update'])->name('update');
        Route::delete('/{id}', [\App\Http\Controllers\Finance\SalesRecordController::class, 'destroy'])->name('destroy');
    });

    Route::get('/purchase', function () {
        if (request()->header('HX-Request')) {
            return response('', 200)->header('HX-Redirect', route('finance.purchase'));
        }

        return view('finance.purchase');
    })->name('purchase');

    // 매입 관리 API
    Route::prefix('purchases')->name('purchases.')->group(function () {
        Route::get('/list', [\App\Http\Controllers\Finance\PurchaseController::class, 'index'])->name('list');
        Route::post('/store', [\App\Http\Controllers\Finance\PurchaseController::class, 'store'])->name('store');
        Route::put('/{id}', [\App\Http\Controllers\Finance\PurchaseController::class, 'update'])->name('update');
        Route::delete('/{id}', [\App\Http\Controllers\Finance\PurchaseController::class, 'destroy'])->name('destroy');
    });

    // 통합 정산관리
    Route::get('/settlement', [\App\Http\Controllers\Finance\SettlementController::class, 'index'])->name('settlement');
    Route::get('/settlement/commission-stats', [\App\Http\Controllers\Finance\SettlementController::class, 'commissionStats'])->name('settlement.commission-stats');
    Route::get('/settlement/commission-table', [\App\Http\Controllers\Finance\SettlementController::class, 'commissionTable'])->name('settlement.commission-table');
    Route::get('/settlement/partner-summary', [\App\Http\Controllers\Finance\SettlementController::class, 'partnerSummary'])->name('settlement.partner-summary');
    Route::get('/settlement/consulting', [\App\Http\Controllers\Finance\SettlementController::class, 'consultingTab'])->name('settlement.consulting');
    Route::get('/settlement/customer', [\App\Http\Controllers\Finance\SettlementController::class, 'customerTab'])->name('settlement.customer');
    Route::get('/settlement/subscription', [\App\Http\Controllers\Finance\SettlementController::class, 'subscriptionTab'])->name('settlement.subscription');
    Route::get('/settlement/payment', [\App\Http\Controllers\Finance\SettlementController::class, 'paymentTab'])->name('settlement.payment');
    Route::get('/settlement/payment-partner-detail/{partnerId}', [\App\Http\Controllers\Finance\SettlementController::class, 'paymentPartnerDetail'])->name('settlement.payment-partner-detail');
    Route::get('/settlement/payment-stats', [\App\Http\Controllers\Finance\SettlementController::class, 'paymentStats'])->name('settlement.payment-stats');

    // 영업수수료정산 (실제 구현 - CRUD API는 그대로 유지)
    Route::prefix('sales-commissions')->name('sales-commissions.')->group(function () {
        Route::get('/', [\App\Http\Controllers\Finance\SalesCommissionController::class, 'index'])->name('index');
        Route::get('/export', [\App\Http\Controllers\Finance\SalesCommissionController::class, 'export'])->name('export');
        Route::get('/payment-form', [\App\Http\Controllers\Finance\SalesCommissionController::class, 'paymentForm'])->name('payment-form');
        Route::get('/table', [\App\Http\Controllers\Finance\SalesCommissionController::class, 'table'])->name('table');
        Route::get('/stats', [\App\Http\Controllers\Finance\SalesCommissionController::class, 'stats'])->name('stats');
        Route::post('/payment', [\App\Http\Controllers\Finance\SalesCommissionController::class, 'registerPayment'])->name('payment');
        Route::post('/bulk-approve', [\App\Http\Controllers\Finance\SalesCommissionController::class, 'bulkApprove'])->name('bulk-approve');
        Route::post('/bulk-mark-paid', [\App\Http\Controllers\Finance\SalesCommissionController::class, 'bulkMarkPaid'])->name('bulk-mark-paid');
        Route::get('/{id}', [\App\Http\Controllers\Finance\SalesCommissionController::class, 'show'])->name('show');
        Route::get('/{id}/detail', [\App\Http\Controllers\Finance\SalesCommissionController::class, 'detail'])->name('detail');
        Route::post('/{id}/approve', [\App\Http\Controllers\Finance\SalesCommissionController::class, 'approve'])->name('approve');
        Route::post('/{id}/mark-paid', [\App\Http\Controllers\Finance\SalesCommissionController::class, 'markPaid'])->name('mark-paid');
        Route::post('/{id}/unapprove', [\App\Http\Controllers\Finance\SalesCommissionController::class, 'unapprove'])->name('unapprove');
        Route::post('/{id}/cancel', [\App\Http\Controllers\Finance\SalesCommissionController::class, 'cancel'])->name('cancel');
        Route::put('/{id}/update-date', [\App\Http\Controllers\Finance\SalesCommissionController::class, 'updateCommissionDate'])->name('update-date');
    });

    // 기존 URL 리다이렉트 → 통합 정산관리
    Route::get('/sales-commission', fn () => redirect()->route('finance.settlement'))->name('sales-commission');
    Route::get('/consulting-fee', fn () => redirect()->route('finance.settlement', ['tab' => 'consulting']))->name('consulting-fee');

    // 상담수수료 API
    Route::prefix('consulting-fees')->name('consulting-fees.')->group(function () {
        Route::get('/list', [\App\Http\Controllers\Finance\ConsultingFeeController::class, 'index'])->name('list');
        Route::post('/store', [\App\Http\Controllers\Finance\ConsultingFeeController::class, 'store'])->name('store');
        Route::put('/{id}', [\App\Http\Controllers\Finance\ConsultingFeeController::class, 'update'])->name('update');
        Route::delete('/{id}', [\App\Http\Controllers\Finance\ConsultingFeeController::class, 'destroy'])->name('destroy');
    });

    Route::get('/customer-settlement', fn () => redirect()->route('finance.settlement', ['tab' => 'customer']))->name('customer-settlement');

    // 고객사별 정산 API
    Route::prefix('customer-settlements')->name('customer-settlements.')->group(function () {
        Route::get('/list', [\App\Http\Controllers\Finance\CustomerSettlementController::class, 'index'])->name('list');
        Route::post('/store', [\App\Http\Controllers\Finance\CustomerSettlementController::class, 'store'])->name('store');
        Route::put('/{id}', [\App\Http\Controllers\Finance\CustomerSettlementController::class, 'update'])->name('update');
        Route::delete('/{id}', [\App\Http\Controllers\Finance\CustomerSettlementController::class, 'destroy'])->name('destroy');
    });

    Route::get('/subscription', fn () => redirect()->route('finance.settlement', ['tab' => 'subscription']))->name('subscription');

    // 구독 관리 API
    Route::prefix('subscriptions')->name('subscriptions.')->group(function () {
        Route::get('/list', [\App\Http\Controllers\Finance\SubscriptionController::class, 'index'])->name('list');
        Route::post('/store', [\App\Http\Controllers\Finance\SubscriptionController::class, 'store'])->name('store');
        Route::put('/{id}', [\App\Http\Controllers\Finance\SubscriptionController::class, 'update'])->name('update');
        Route::delete('/{id}', [\App\Http\Controllers\Finance\SubscriptionController::class, 'destroy'])->name('destroy');
    });

    // 차량관리
    Route::get('/corporate-vehicles', [\App\Http\Controllers\Finance\CorporateVehicleController::class, 'index'])->name('corporate-vehicles');
    Route::get('/corporate-vehicles/list', [\App\Http\Controllers\Finance\CorporateVehicleController::class, 'list'])->name('corporate-vehicles.list');
    Route::post('/corporate-vehicles', [\App\Http\Controllers\Finance\CorporateVehicleController::class, 'store'])->name('corporate-vehicles.store');
    Route::put('/corporate-vehicles/{id}', [\App\Http\Controllers\Finance\CorporateVehicleController::class, 'update'])->name('corporate-vehicles.update');
    Route::delete('/corporate-vehicles/{id}', [\App\Http\Controllers\Finance\CorporateVehicleController::class, 'destroy'])->name('corporate-vehicles.destroy');

    // 차량일지 (운행기록부)
    Route::get('/vehicle-logs', [\App\Http\Controllers\Finance\VehicleLogController::class, 'index'])->name('vehicle-logs');
    Route::get('/vehicle-logs/vehicles', [\App\Http\Controllers\Finance\VehicleLogController::class, 'vehicles'])->name('vehicle-logs.vehicles');
    Route::get('/vehicle-logs/list', [\App\Http\Controllers\Finance\VehicleLogController::class, 'list'])->name('vehicle-logs.list');
    Route::get('/vehicle-logs/summary', [\App\Http\Controllers\Finance\VehicleLogController::class, 'summary'])->name('vehicle-logs.summary');
    Route::post('/vehicle-logs', [\App\Http\Controllers\Finance\VehicleLogController::class, 'store'])->name('vehicle-logs.store');
    Route::put('/vehicle-logs/{id}', [\App\Http\Controllers\Finance\VehicleLogController::class, 'update'])->name('vehicle-logs.update');
    Route::delete('/vehicle-logs/{id}', [\App\Http\Controllers\Finance\VehicleLogController::class, 'destroy'])->name('vehicle-logs.destroy');

    // 차량정비이력
    Route::get('/vehicle-maintenance', [\App\Http\Controllers\Finance\VehicleMaintenanceController::class, 'index'])->name('vehicle-maintenance');
    Route::get('/vehicle-maintenance/vehicles', [\App\Http\Controllers\Finance\VehicleMaintenanceController::class, 'vehicles'])->name('vehicle-maintenance.vehicles');
    Route::get('/vehicle-maintenance/list', [\App\Http\Controllers\Finance\VehicleMaintenanceController::class, 'list'])->name('vehicle-maintenance.list');
    Route::post('/vehicle-maintenance', [\App\Http\Controllers\Finance\VehicleMaintenanceController::class, 'store'])->name('vehicle-maintenance.store');
    Route::put('/vehicle-maintenance/{id}', [\App\Http\Controllers\Finance\VehicleMaintenanceController::class, 'update'])->name('vehicle-maintenance.update');
    Route::delete('/vehicle-maintenance/{id}', [\App\Http\Controllers\Finance\VehicleMaintenanceController::class, 'destroy'])->name('vehicle-maintenance.destroy');

    // 거래처관리
    Route::get('/customers', function () {
        if (request()->header('HX-Request')) {
            return response('', 200)->header('HX-Redirect', route('finance.customers'));
        }

        return view('finance.customers');
    })->name('customers');

    // 고객사 관리 API
    Route::prefix('customers')->name('customers.')->group(function () {
        Route::get('/list', [\App\Http\Controllers\Finance\CustomerController::class, 'index'])->name('list');
        Route::post('/store', [\App\Http\Controllers\Finance\CustomerController::class, 'store'])->name('store');
        Route::post('/ocr', [\App\Http\Controllers\Finance\CustomerController::class, 'ocr'])->middleware('throttle:10,1')->name('ocr');
        Route::put('/{id}', [\App\Http\Controllers\Finance\CustomerController::class, 'update'])->name('update');
        Route::delete('/{id}', [\App\Http\Controllers\Finance\CustomerController::class, 'destroy'])->name('destroy');
    });

    Route::get('/partners', function () {
        if (request()->header('HX-Request')) {
            return response('', 200)->header('HX-Redirect', route('finance.partners'));
        }

        return view('finance.partners');
    })->name('partners');

    // 거래처 관리 API
    Route::prefix('partners')->name('partners.')->group(function () {
        Route::get('/list', [\App\Http\Controllers\Finance\TradingPartnerController::class, 'index'])->name('list');
        Route::post('/store', [\App\Http\Controllers\Finance\TradingPartnerController::class, 'store'])->name('store');
        Route::post('/ocr', [\App\Http\Controllers\Finance\TradingPartnerController::class, 'ocr'])->name('ocr');
        Route::put('/{id}', [\App\Http\Controllers\Finance\TradingPartnerController::class, 'update'])->name('update');
        Route::delete('/{id}', [\App\Http\Controllers\Finance\TradingPartnerController::class, 'destroy'])->name('destroy');
    });

    // 채권/채무
    Route::get('/receivables', function () {
        if (request()->header('HX-Request')) {
            return response('', 200)->header('HX-Redirect', route('finance.receivables'));
        }

        return view('finance.receivables');
    })->name('receivables');

    // 미수금 관리 API
    Route::prefix('receivables')->name('receivables.')->group(function () {
        Route::get('/list', [\App\Http\Controllers\Finance\ReceivableController::class, 'index'])->name('list');
        Route::get('/ledger', [\App\Http\Controllers\Finance\ReceivableController::class, 'ledger'])->name('ledger');
        Route::get('/summary', [\App\Http\Controllers\Finance\ReceivableController::class, 'summary'])->name('summary');
        Route::post('/store', [\App\Http\Controllers\Finance\ReceivableController::class, 'store'])->name('store');
        Route::put('/{id}', [\App\Http\Controllers\Finance\ReceivableController::class, 'update'])->name('update');
        Route::post('/{id}/collect', [\App\Http\Controllers\Finance\ReceivableController::class, 'collect'])->name('collect');
        Route::delete('/{id}', [\App\Http\Controllers\Finance\ReceivableController::class, 'destroy'])->name('destroy');
    });

    Route::get('/payables', function () {
        if (request()->header('HX-Request')) {
            return response('', 200)->header('HX-Redirect', route('finance.payables'));
        }

        return view('finance.payables');
    })->name('payables');

    // 미지급금 관리 API
    Route::prefix('payables')->name('payables.')->group(function () {
        Route::get('/list', [\App\Http\Controllers\Finance\PayableController::class, 'index'])->name('list');
        Route::get('/integrated', [\App\Http\Controllers\Finance\PayableController::class, 'integrated'])->name('integrated');
        Route::get('/hometax', [\App\Http\Controllers\Finance\PayableController::class, 'hometaxPayables'])->name('hometax');
        Route::get('/journals', [\App\Http\Controllers\Finance\PayableController::class, 'journalPayables'])->name('journals');
        Route::post('/store', [\App\Http\Controllers\Finance\PayableController::class, 'store'])->name('store');
        Route::put('/{id}', [\App\Http\Controllers\Finance\PayableController::class, 'update'])->name('update');
        Route::post('/{id}/pay', [\App\Http\Controllers\Finance\PayableController::class, 'pay'])->name('pay');
        Route::delete('/{id}', [\App\Http\Controllers\Finance\PayableController::class, 'destroy'])->name('destroy');
    });

    // 기타
    Route::get('/refunds', function () {
        if (request()->header('HX-Request')) {
            return response('', 200)->header('HX-Redirect', route('finance.refunds'));
        }

        return view('finance.refunds');
    })->name('refunds');

    // 환불/해지 관리 API
    Route::prefix('refunds')->name('refunds.')->group(function () {
        Route::get('/list', [\App\Http\Controllers\Finance\RefundController::class, 'index'])->name('list');
        Route::post('/store', [\App\Http\Controllers\Finance\RefundController::class, 'store'])->name('store');
        Route::put('/{id}', [\App\Http\Controllers\Finance\RefundController::class, 'update'])->name('update');
        Route::post('/{id}/process', [\App\Http\Controllers\Finance\RefundController::class, 'process'])->name('process');
        Route::delete('/{id}', [\App\Http\Controllers\Finance\RefundController::class, 'destroy'])->name('destroy');
    });

    Route::get('/vat', function () {
        if (request()->header('HX-Request')) {
            return response('', 200)->header('HX-Redirect', route('finance.vat'));
        }

        return view('finance.vat');
    })->name('vat');

    // 부가세 관리 API
    Route::prefix('vat')->name('vat.')->group(function () {
        Route::get('/list', [\App\Http\Controllers\Finance\VatRecordController::class, 'index'])->name('list');
        Route::post('/store', [\App\Http\Controllers\Finance\VatRecordController::class, 'store'])->name('store');
        Route::put('/{id}', [\App\Http\Controllers\Finance\VatRecordController::class, 'update'])->name('update');
        Route::delete('/{id}', [\App\Http\Controllers\Finance\VatRecordController::class, 'destroy'])->name('destroy');
    });
});

/*
|--------------------------------------------------------------------------
| Sales Management Routes (영업관리)
|--------------------------------------------------------------------------
*/
Route::middleware(['auth', 'hq.member'])->prefix('sales')->name('sales.')->group(function () {
    // 영업관리 대시보드
    Route::get('salesmanagement/dashboard', [\App\Http\Controllers\Sales\SalesDashboardController::class, 'index'])->name('salesmanagement.dashboard');
    Route::get('salesmanagement/dashboard/refresh', [\App\Http\Controllers\Sales\SalesDashboardController::class, 'refresh'])->name('salesmanagement.dashboard.refresh');
    Route::get('salesmanagement/dashboard/tenants', [\App\Http\Controllers\Sales\SalesDashboardController::class, 'refreshTenantList'])->name('salesmanagement.dashboard.tenants');
    Route::get('salesmanagement/dashboard/partner-activity', [\App\Http\Controllers\Sales\SalesDashboardController::class, 'partnerActivity'])->name('salesmanagement.dashboard.partner-activity');
    Route::get('salesmanagement/dashboard/help', [\App\Http\Controllers\Sales\SalesDashboardController::class, 'helpGuide'])->name('salesmanagement.dashboard.help');
    Route::get('salesmanagement/dashboard/prospect/{id}/row', [\App\Http\Controllers\Sales\SalesDashboardController::class, 'getProspectRow'])->name('salesmanagement.dashboard.prospect-row');

    // 영업파트너 승인 (본사 관리자 전용) - resource 전에 정의해야 함
    Route::get('managers/approvals', [\App\Http\Controllers\Sales\SalesManagerController::class, 'approvals'])->name('managers.approvals');
    Route::post('managers/approvals/{id}/approve', [\App\Http\Controllers\Sales\SalesManagerController::class, 'approveFromList'])->name('managers.approvals.approve');
    Route::post('managers/approvals/{id}/reject', [\App\Http\Controllers\Sales\SalesManagerController::class, 'rejectFromList'])->name('managers.approvals.reject');

    // 개발 승인 관리 (본사 관리자 전용)
    Route::prefix('development')->name('development.')->group(function () {
        Route::get('/approvals', [\App\Http\Controllers\Sales\SalesDevelopmentApprovalController::class, 'index'])
            ->name('approvals.index');
        Route::post('/approvals/{id}/approve', [\App\Http\Controllers\Sales\SalesDevelopmentApprovalController::class, 'approve'])
            ->name('approvals.approve');
        Route::post('/approvals/{id}/reject', [\App\Http\Controllers\Sales\SalesDevelopmentApprovalController::class, 'reject'])
            ->name('approvals.reject');
        Route::post('/approvals/{id}/status', [\App\Http\Controllers\Sales\SalesDevelopmentApprovalController::class, 'updateStatus'])
            ->name('approvals.status');
        Route::post('/approvals/{id}/revert-pending', [\App\Http\Controllers\Sales\SalesDevelopmentApprovalController::class, 'revertToPending'])
            ->name('approvals.revert-pending');
        Route::get('/approvals/{id}/detail', [\App\Http\Controllers\Sales\SalesDevelopmentApprovalController::class, 'detail'])
            ->name('approvals.detail');
    });

    // 명함신청
    Route::get('business-cards', [\App\Http\Controllers\Sales\BusinessCardRequestController::class, 'index'])->name('business-cards.index');
    Route::post('business-cards', [\App\Http\Controllers\Sales\BusinessCardRequestController::class, 'store'])->name('business-cards.store');
    Route::get('business-cards/manage', [\App\Http\Controllers\Sales\BusinessCardRequestController::class, 'manage'])->name('business-cards.manage');
    Route::post('business-cards/{id}/order', [\App\Http\Controllers\Sales\BusinessCardRequestController::class, 'order'])->name('business-cards.order');
    Route::post('business-cards/{id}/process', [\App\Http\Controllers\Sales\BusinessCardRequestController::class, 'process'])->name('business-cards.process');
    Route::delete('business-cards/{id}', [\App\Http\Controllers\Sales\BusinessCardRequestController::class, 'destroy'])->name('business-cards.destroy');

    // 매니저 검색 (리소스 라우트보다 먼저 정의해야 함!)
    Route::get('managers/list', [\App\Http\Controllers\Sales\SalesDashboardController::class, 'getManagers'])->name('managers.list');
    Route::get('managers/search', [\App\Http\Controllers\Sales\SalesDashboardController::class, 'searchManagers'])->name('managers.search');

    // 영업 담당자 관리
    Route::resource('managers', \App\Http\Controllers\Sales\SalesManagerController::class);
    Route::get('managers/{id}/modal-show', [\App\Http\Controllers\Sales\SalesManagerController::class, 'modalShow'])->name('managers.modal-show');
    Route::get('managers/{id}/modal-edit', [\App\Http\Controllers\Sales\SalesManagerController::class, 'modalEdit'])->name('managers.modal-edit');
    Route::post('managers/{id}/approve', [\App\Http\Controllers\Sales\SalesManagerController::class, 'approve'])->name('managers.approve');
    Route::post('managers/{id}/reject', [\App\Http\Controllers\Sales\SalesManagerController::class, 'reject'])->name('managers.reject');
    Route::post('managers/{id}/delegate-role', [\App\Http\Controllers\Sales\SalesManagerController::class, 'delegateRole'])->name('managers.delegate-role');
    Route::post('managers/{id}/assign-role', [\App\Http\Controllers\Sales\SalesManagerController::class, 'assignRole'])->name('managers.assign-role');
    Route::post('managers/{id}/remove-role', [\App\Http\Controllers\Sales\SalesManagerController::class, 'removeRole'])->name('managers.remove-role');
    Route::get('managers/{id}/documents/{documentId}/download', [\App\Http\Controllers\Sales\SalesManagerController::class, 'downloadDocument'])->name('managers.documents.download');
    Route::delete('managers/{id}/documents/{documentId}', [\App\Http\Controllers\Sales\SalesManagerController::class, 'deleteDocument'])->name('managers.documents.delete');

    // 명함등록 (영업권) 관리
    Route::resource('prospects', \App\Http\Controllers\Sales\TenantProspectController::class);
    Route::post('prospects/{id}/convert', [\App\Http\Controllers\Sales\TenantProspectController::class, 'convert'])->name('prospects.convert');
    Route::post('prospects/check-business-number', [\App\Http\Controllers\Sales\TenantProspectController::class, 'checkBusinessNumber'])->name('prospects.check-business-number');
    Route::delete('prospects/{id}/attachment', [\App\Http\Controllers\Sales\TenantProspectController::class, 'deleteAttachment'])->name('prospects.delete-attachment');
    Route::get('prospects/{id}/modal-show', [\App\Http\Controllers\Sales\TenantProspectController::class, 'modalShow'])->name('prospects.modal-show');
    Route::get('prospects/{id}/modal-edit', [\App\Http\Controllers\Sales\TenantProspectController::class, 'modalEdit'])->name('prospects.modal-edit');

    // 관리자용 전체 고객 관리 (관리자/슈퍼관리자 전용)
    Route::get('admin-prospects', [\App\Http\Controllers\Sales\AdminProspectController::class, 'index'])->name('admin-prospects.index');
    Route::get('admin-prospects/refresh', [\App\Http\Controllers\Sales\AdminProspectController::class, 'refresh'])->name('admin-prospects.refresh');
    Route::get('admin-prospects/{id}/modal-show', [\App\Http\Controllers\Sales\AdminProspectController::class, 'modalShow'])->name('admin-prospects.modal-show');
    Route::post('admin-prospects/{id}/hq-status', [\App\Http\Controllers\Sales\AdminProspectController::class, 'updateHqStatus'])->name('admin-prospects.update-hq-status');
    Route::post('admin-prospects/{id}/contracted-date', [\App\Http\Controllers\Sales\AdminProspectController::class, 'updateContractedDate'])->name('admin-prospects.update-contracted-date');
    Route::post('admin-prospects/{id}/handover-date', [\App\Http\Controllers\Sales\AdminProspectController::class, 'updateHandoverDate'])->name('admin-prospects.update-handover-date');
    Route::post('admin-prospects/{id}/commission-date', [\App\Http\Controllers\Sales\AdminProspectController::class, 'updateCommissionDate'])->name('admin-prospects.update-commission-date');
    Route::delete('admin-prospects/{id}/commission-date', [\App\Http\Controllers\Sales\AdminProspectController::class, 'clearCommissionDate'])->name('admin-prospects.clear-commission-date');
    Route::delete('admin-prospects/{id}', [\App\Http\Controllers\Sales\AdminProspectController::class, 'destroy'])->name('admin-prospects.destroy')->middleware('super.admin');
    Route::post('admin-prospects/{id}/toggle-status', [\App\Http\Controllers\Sales\AdminProspectController::class, 'toggleStatus'])->name('admin-prospects.toggle-status');
    Route::post('admin-prospects/{id}/referrer-commission', [\App\Http\Controllers\Sales\AdminProspectController::class, 'updateReferrerCommission'])->name('admin-prospects.update-referrer-commission');

    // 영업 시나리오 관리
    Route::prefix('scenarios')->name('scenarios.')->group(function () {
        // 테넌트 기반 (기존)
        Route::get('/{tenant}/sales', [\App\Http\Controllers\Sales\SalesScenarioController::class, 'salesScenario'])->name('sales');
        Route::get('/{tenant}/manager', [\App\Http\Controllers\Sales\SalesScenarioController::class, 'managerScenario'])->name('manager');
        Route::post('/checklist/toggle', [\App\Http\Controllers\Sales\SalesScenarioController::class, 'toggleChecklist'])->name('checklist.toggle');
        Route::get('/{tenant}/{type}/progress', [\App\Http\Controllers\Sales\SalesScenarioController::class, 'getProgress'])->name('progress');

        // 가망고객 기반 (신규)
        Route::get('/prospect/{prospect}/sales', [\App\Http\Controllers\Sales\SalesScenarioController::class, 'prospectSalesScenario'])->name('prospect.sales');
        Route::get('/prospect/{prospect}/manager', [\App\Http\Controllers\Sales\SalesScenarioController::class, 'prospectManagerScenario'])->name('prospect.manager');
        Route::post('/prospect/checklist/toggle', [\App\Http\Controllers\Sales\SalesScenarioController::class, 'toggleProspectChecklist'])->name('prospect.checklist.toggle');
        Route::get('/prospect/{prospect}/{type}/progress', [\App\Http\Controllers\Sales\SalesScenarioController::class, 'getProspectProgress'])->name('prospect.progress');
    });

    // 상담 기록 관리
    Route::prefix('consultations')->name('consultations.')->group(function () {
        // 테넌트용
        Route::get('/{tenant}', [\App\Http\Controllers\Sales\ConsultationController::class, 'index'])->name('index');
        Route::post('/', [\App\Http\Controllers\Sales\ConsultationController::class, 'store'])->name('store');
        Route::delete('/{consultation}', [\App\Http\Controllers\Sales\ConsultationController::class, 'destroy'])->name('destroy');
        Route::post('/upload-audio', [\App\Http\Controllers\Sales\ConsultationController::class, 'uploadAudio'])->name('upload-audio');
        Route::post('/upload-file', [\App\Http\Controllers\Sales\ConsultationController::class, 'uploadFile'])->name('upload-file');
        Route::delete('/file/{file}', [\App\Http\Controllers\Sales\ConsultationController::class, 'deleteFile'])->name('delete-file');
        Route::get('/download-audio/{consultation}', [\App\Http\Controllers\Sales\ConsultationController::class, 'downloadAudio'])->name('download-audio');
        Route::get('/download-file/{consultation}', [\App\Http\Controllers\Sales\ConsultationController::class, 'downloadFile'])->name('download-file');

        // 가망고객(Prospect)용
        Route::get('/prospect/{prospect}', [\App\Http\Controllers\Sales\ConsultationController::class, 'prospectIndex'])->name('prospect.index');
        Route::post('/prospect', [\App\Http\Controllers\Sales\ConsultationController::class, 'prospectStore'])->name('prospect.store');
        Route::post('/prospect/upload-audio', [\App\Http\Controllers\Sales\ConsultationController::class, 'prospectUploadAudio'])->name('prospect.upload-audio');
        Route::post('/prospect/upload-file', [\App\Http\Controllers\Sales\ConsultationController::class, 'prospectUploadFile'])->name('prospect.upload-file');
    });

    // 영업 실적 관리
    Route::resource('records', \App\Http\Controllers\Sales\SalesRecordController::class);

    // 매니저 지정 변경
    Route::post('/tenants/{tenant}/assign-manager', [\App\Http\Controllers\Sales\SalesDashboardController::class, 'assignManager'])->name('tenants.assign-manager');
    Route::post('/prospects/{prospect}/assign-manager', [\App\Http\Controllers\Sales\SalesDashboardController::class, 'assignProspectManager'])->name('prospects.assign-manager');

    // 매니저 목록/검색은 리소스 라우트 앞에 정의됨 (912줄 위치)

    // 상품관리 (HQ 전용)
    Route::prefix('products')->name('products.')->group(function () {
        Route::get('/', [SalesProductController::class, 'index'])->name('index');
        Route::get('/list', [SalesProductController::class, 'productList'])->name('list');
        Route::post('/', [SalesProductController::class, 'store'])->name('store');
        Route::put('/{id}', [SalesProductController::class, 'update'])->name('update');
        Route::delete('/{id}', [SalesProductController::class, 'destroy'])->name('destroy');
        Route::post('/{id}/toggle', [SalesProductController::class, 'toggleActive'])->name('toggle');
        Route::post('/reorder', [SalesProductController::class, 'reorder'])->name('reorder');

        // 카테고리 관리
        Route::prefix('categories')->name('categories.')->group(function () {
            Route::get('/', [SalesProductController::class, 'categories'])->name('index');
            Route::post('/', [SalesProductController::class, 'storeCategory'])->name('store');
            Route::put('/{id}', [SalesProductController::class, 'updateCategory'])->name('update');
            Route::delete('/{id}', [SalesProductController::class, 'deleteCategory'])->name('destroy');
        });

        // API (영업 시나리오용)
        Route::get('/api/list', [SalesProductController::class, 'getProductsApi'])->name('api.list');
    });

    // 계약관리
    Route::prefix('contracts')->name('contracts.')->group(function () {
        Route::post('/products', [\App\Http\Controllers\Sales\SalesContractController::class, 'saveProducts'])->name('products.save');
        Route::get('/products/{tenant}', [\App\Http\Controllers\Sales\SalesContractController::class, 'getProducts'])->name('products.get');
    });

    // 인터뷰 시나리오 관리
    Route::prefix('interviews')->name('interviews.')->group(function () {
        Route::get('/', [\App\Http\Controllers\Sales\InterviewScenarioController::class, 'index'])->name('index');
        // 카테고리 API
        Route::get('/api/categories', [\App\Http\Controllers\Sales\InterviewScenarioController::class, 'categories'])->name('api.categories');
        Route::post('/api/categories', [\App\Http\Controllers\Sales\InterviewScenarioController::class, 'storeCategory'])->name('api.categories.store');
        Route::put('/api/categories/{id}', [\App\Http\Controllers\Sales\InterviewScenarioController::class, 'updateCategory'])->name('api.categories.update');
        Route::delete('/api/categories/{id}', [\App\Http\Controllers\Sales\InterviewScenarioController::class, 'destroyCategory'])->name('api.categories.destroy');
        // 트리 API
        Route::get('/api/tree', [\App\Http\Controllers\Sales\InterviewScenarioController::class, 'tree'])->name('api.tree');
        // 템플릿(항목) API
        Route::post('/api/templates', [\App\Http\Controllers\Sales\InterviewScenarioController::class, 'storeTemplate'])->name('api.templates.store');
        Route::put('/api/templates/{id}', [\App\Http\Controllers\Sales\InterviewScenarioController::class, 'updateTemplate'])->name('api.templates.update');
        Route::delete('/api/templates/{id}', [\App\Http\Controllers\Sales\InterviewScenarioController::class, 'destroyTemplate'])->name('api.templates.destroy');
        // 질문 API
        Route::post('/api/questions', [\App\Http\Controllers\Sales\InterviewScenarioController::class, 'storeQuestion'])->name('api.questions.store');
        Route::put('/api/questions/{id}', [\App\Http\Controllers\Sales\InterviewScenarioController::class, 'updateQuestion'])->name('api.questions.update');
        Route::delete('/api/questions/{id}', [\App\Http\Controllers\Sales\InterviewScenarioController::class, 'destroyQuestion'])->name('api.questions.destroy');
        // 일괄 가져오기 API
        Route::post('/api/bulk-import', [\App\Http\Controllers\Sales\InterviewScenarioController::class, 'bulkImport'])->name('api.bulk-import');
        // 세션 API
        Route::get('/api/sessions', [\App\Http\Controllers\Sales\InterviewScenarioController::class, 'sessions'])->name('api.sessions');
        Route::post('/api/sessions', [\App\Http\Controllers\Sales\InterviewScenarioController::class, 'storeSession'])->name('api.sessions.store');
        Route::get('/api/sessions/{id}', [\App\Http\Controllers\Sales\InterviewScenarioController::class, 'showSession'])->name('api.sessions.show');
        Route::post('/api/sessions/toggle-answer', [\App\Http\Controllers\Sales\InterviewScenarioController::class, 'toggleAnswer'])->name('api.sessions.toggle-answer');
        Route::post('/api/sessions/{id}/complete', [\App\Http\Controllers\Sales\InterviewScenarioController::class, 'completeSession'])->name('api.sessions.complete');
    });
});

/*
|--------------------------------------------------------------------------
| 주일기업 기획 (Juil Planning)
|--------------------------------------------------------------------------
*/
Route::middleware('auth')->prefix('juil')->name('juil.')->group(function () {
    Route::get('/estimate', [PlanningController::class, 'estimate'])->name('estimate');
    Route::get('/project', [PlanningController::class, 'project'])->name('project');

    // 공사현장 사진대지
    Route::prefix('construction-photos')->name('construction-photos.')->group(function () {
        Route::get('/', [ConstructionSitePhotoController::class, 'index'])->name('index');
        Route::get('/list', [ConstructionSitePhotoController::class, 'list'])->name('list');
        Route::post('/', [ConstructionSitePhotoController::class, 'store'])->name('store');
        Route::post('/log-stt-usage', [ConstructionSitePhotoController::class, 'logSttUsage'])->name('log-stt-usage');
        Route::get('/{id}', [ConstructionSitePhotoController::class, 'show'])->name('show');
        Route::put('/{id}', [ConstructionSitePhotoController::class, 'update'])->name('update');
        Route::delete('/{id}', [ConstructionSitePhotoController::class, 'destroy'])->name('destroy');
        // 행 관리
        Route::post('/{id}/rows', [ConstructionSitePhotoController::class, 'addRow'])->name('add-row');
        Route::delete('/{id}/rows/{rowId}', [ConstructionSitePhotoController::class, 'deleteRow'])->name('delete-row');
        // 행별 사진 관리
        Route::post('/{id}/rows/{rowId}/upload', [ConstructionSitePhotoController::class, 'uploadPhoto'])->name('upload');
        Route::delete('/{id}/rows/{rowId}/photo/{type}', [ConstructionSitePhotoController::class, 'deletePhoto'])->name('delete-photo');
        Route::get('/{id}/rows/{rowId}/download/{type}', [ConstructionSitePhotoController::class, 'downloadPhoto'])->name('download');
    });

    // 회의록 작성
    Route::prefix('meeting-minutes')->name('meeting-minutes.')->group(function () {
        Route::get('/', [MeetingMinuteController::class, 'index'])->name('index');
        Route::get('/list', [MeetingMinuteController::class, 'list'])->name('list');
        Route::post('/', [MeetingMinuteController::class, 'store'])->name('store');
        Route::post('/log-stt-usage', [MeetingMinuteController::class, 'logSttUsage'])->name('log-stt-usage');
        Route::get('/{id}', [MeetingMinuteController::class, 'show'])->name('show');
        Route::put('/{id}', [MeetingMinuteController::class, 'update'])->name('update');
        Route::delete('/{id}', [MeetingMinuteController::class, 'destroy'])->name('destroy');
        Route::post('/{id}/segments', [MeetingMinuteController::class, 'saveSegments'])->name('save-segments');
        Route::post('/{id}/upload-audio', [MeetingMinuteController::class, 'uploadAudio'])->name('upload-audio');
        Route::post('/{id}/summarize', [MeetingMinuteController::class, 'summarize'])->name('summarize');
        Route::post('/{id}/diarize', [MeetingMinuteController::class, 'diarize'])->name('diarize');
        Route::get('/{id}/download-audio', [MeetingMinuteController::class, 'downloadAudio'])->name('download-audio');
    });

});

/*
|--------------------------------------------------------------------------
| 아카데미 (Academy)
|--------------------------------------------------------------------------
*/
Route::middleware('auth')->prefix('academy')->name('academy.')->group(function () {
    Route::get('/fire-shutter', [\App\Http\Controllers\AcademyController::class, 'fireShutter'])->name('fire-shutter');
    Route::get('/it-planning', [\App\Http\Controllers\AcademyController::class, 'itPlanning'])->name('it-planning');
    Route::get('/server-knowledge', [\App\Http\Controllers\AcademyController::class, 'serverKnowledge'])->name('server-knowledge');
    Route::get('/frontend-dev', [\App\Http\Controllers\AcademyController::class, 'frontendDev'])->name('frontend-dev');
    Route::get('/docker-environment', [\App\Http\Controllers\AcademyController::class, 'dockerEnvironment'])->name('docker-environment');
    Route::get('/docker-commands', [\App\Http\Controllers\AcademyController::class, 'dockerCommands'])->name('docker-commands');
    Route::get('/web-basics', [\App\Http\Controllers\AcademyController::class, 'webBasics'])->name('web-basics');
    Route::get('/backend-dev', [\App\Http\Controllers\AcademyController::class, 'backendDev'])->name('backend-dev');
    Route::get('/env-management', [\App\Http\Controllers\AcademyController::class, 'envManagement'])->name('env-management');
    Route::get('/git-policy', [\App\Http\Controllers\AcademyController::class, 'gitPolicy'])->name('git-policy');
    Route::get('/git-tools', [\App\Http\Controllers\AcademyController::class, 'gitTools'])->name('git-tools');
    Route::get('/nginx-encyclopedia', [\App\Http\Controllers\AcademyController::class, 'nginxEncyclopedia'])->name('nginx-encyclopedia');
    Route::get('/linux-commands', [\App\Http\Controllers\AcademyController::class, 'linuxCommands'])->name('linux-commands');
    Route::get('/pm2-guide', [\App\Http\Controllers\AcademyController::class, 'pm2Guide'])->name('pm2-guide');
    Route::get('/tax-correction', [\App\Http\Controllers\AcademyController::class, 'taxCorrection'])->name('tax-correction');
});

// SAM E-Sign - 인증 필요
Route::middleware('auth')->prefix('esign')->name('esign.')->group(function () {
    // 화면 라우트
    Route::get('/', [EsignController::class, 'dashboard'])->name('dashboard');
    Route::get('/create', [EsignController::class, 'create'])->name('create');
    Route::get('/docs', [EsignController::class, 'docs'])->name('docs');
    Route::get('/templates', [EsignController::class, 'templates'])->name('templates');
    Route::get('/templates/{templateId}/fields', [EsignController::class, 'templateFields'])->whereNumber('templateId')->name('template-fields');
    Route::get('/{id}', [EsignController::class, 'detail'])->whereNumber('id')->name('detail');
    Route::get('/{id}/fields', [EsignController::class, 'fields'])->whereNumber('id')->name('fields');
    Route::get('/{id}/send', [EsignController::class, 'send'])->whereNumber('id')->name('send');

    // 내부 API 라우트 (Finance 패턴과 동일 - 직접 DB 접근)
    Route::prefix('contracts')->name('contracts.')->group(function () {
        Route::get('/stamp', [EsignApiController::class, 'getStamp'])->name('stamp.get');
        Route::post('/stamp', [EsignApiController::class, 'uploadStamp'])->name('stamp.upload');
        Route::delete('/stamp', [EsignApiController::class, 'deleteStamp'])->name('stamp.delete');
        Route::get('/stamp/image/{tenant}', [EsignApiController::class, 'serveStampImage'])->name('stamp.image');
        Route::get('/search-partners', [EsignApiController::class, 'searchPartners'])->name('search-partners');
        Route::get('/search-tenants', [EsignApiController::class, 'searchTenants'])->name('search-tenants');
        Route::get('/generate-contract-number', [EsignApiController::class, 'generateContractNumber'])->name('generate-contract-number');
        Route::get('/alimtalk-templates', [EsignApiController::class, 'getAlimtalkTemplates'])->name('alimtalk-templates');

        Route::get('/stats', [EsignApiController::class, 'stats'])->name('stats');
        Route::get('/list', [EsignApiController::class, 'index'])->name('list');
        Route::post('/store', [EsignApiController::class, 'store'])->name('store');
        Route::get('/{id}', [EsignApiController::class, 'show'])->whereNumber('id')->name('show');
        Route::post('/{id}/cancel', [EsignApiController::class, 'cancel'])->whereNumber('id')->name('cancel');
        Route::delete('/destroy', [EsignApiController::class, 'destroy'])->name('destroy');
        Route::get('/trashed', [EsignApiController::class, 'trashed'])->name('trashed');
        Route::post('/restore', [EsignApiController::class, 'restore'])->name('restore');
        Route::delete('/force-destroy', [EsignApiController::class, 'forceDestroy'])->name('force-destroy');
        Route::post('/{id}/fields', [EsignApiController::class, 'configureFields'])->whereNumber('id')->name('fields');
        Route::post('/{id}/send', [EsignApiController::class, 'send'])->whereNumber('id')->name('send');
        Route::post('/{id}/remind', [EsignApiController::class, 'remind'])->whereNumber('id')->name('remind');
        Route::get('/{id}/download', [EsignApiController::class, 'download'])->whereNumber('id')->name('download');
        Route::post('/{id}/upload-pdf', [EsignApiController::class, 'uploadPdf'])->whereNumber('id')->name('upload-pdf');

        // 필드 템플릿
        Route::get('/templates', [EsignApiController::class, 'indexTemplates'])->name('templates.index');
        Route::post('/templates', [EsignApiController::class, 'storeTemplate'])->name('templates.store');
        Route::get('/templates/{templateId}', [EsignApiController::class, 'showTemplate'])->whereNumber('templateId')->name('templates.show');
        Route::put('/templates/{templateId}', [EsignApiController::class, 'updateTemplate'])->whereNumber('templateId')->name('templates.update');
        Route::post('/templates/{templateId}/duplicate', [EsignApiController::class, 'duplicateTemplate'])->whereNumber('templateId')->name('templates.duplicate');
        Route::delete('/templates/{templateId}', [EsignApiController::class, 'destroyTemplate'])->whereNumber('templateId')->name('templates.destroy');
        Route::post('/templates/{templateId}/upload-pdf', [EsignApiController::class, 'uploadTemplatePdf'])->whereNumber('templateId')->name('templates.upload-pdf');
        Route::delete('/templates/{templateId}/remove-pdf', [EsignApiController::class, 'removeTemplatePdf'])->whereNumber('templateId')->name('templates.remove-pdf');
        Route::delete('/templates/{templateId}/items/{itemId}', [EsignApiController::class, 'destroyTemplateItem'])->whereNumber('templateId')->whereNumber('itemId')->name('templates.items.destroy');
        Route::put('/templates/{templateId}/items', [EsignApiController::class, 'updateTemplateItems'])->whereNumber('templateId')->name('templates.items.update');
        Route::get('/templates/{templateId}/download', [EsignApiController::class, 'downloadTemplatePdf'])->whereNumber('templateId')->name('templates.download');

        // 템플릿 적용 / 필드 복사
        Route::post('/{id}/apply-template', [EsignApiController::class, 'applyTemplate'])->whereNumber('id')->name('apply-template');
        Route::post('/{id}/copy-fields/{sourceId}', [EsignApiController::class, 'copyFieldsFromContract'])->whereNumber('id')->whereNumber('sourceId')->name('copy-fields');
    });
});

/*
|--------------------------------------------------------------------------
| YouTube Shorts AI Generator (Veo 3.1)
|--------------------------------------------------------------------------
*/
Route::prefix('video/veo3')->name('video.veo3.')->middleware('auth')->group(function () {
    Route::get('/', [\App\Http\Controllers\Video\Veo3Controller::class, 'index'])->name('index');
    Route::get('/trending', [\App\Http\Controllers\Video\Veo3Controller::class, 'fetchTrending'])->name('trending');
    Route::post('/titles', [\App\Http\Controllers\Video\Veo3Controller::class, 'generateTitles'])->name('titles');
    Route::post('/scenario', [\App\Http\Controllers\Video\Veo3Controller::class, 'generateScenario'])->name('scenario');
    Route::post('/generate', [\App\Http\Controllers\Video\Veo3Controller::class, 'generate'])->name('generate');
    Route::get('/status/{id}', [\App\Http\Controllers\Video\Veo3Controller::class, 'status'])->name('status');
    Route::get('/download/{id}', [\App\Http\Controllers\Video\Veo3Controller::class, 'download'])->name('download');
    Route::get('/preview/{id}', [\App\Http\Controllers\Video\Veo3Controller::class, 'preview'])->name('preview');
    Route::get('/detail/{id}', [\App\Http\Controllers\Video\Veo3Controller::class, 'show'])->name('show');
    Route::get('/history', [\App\Http\Controllers\Video\Veo3Controller::class, 'history'])->name('history');
    Route::delete('/history', [\App\Http\Controllers\Video\Veo3Controller::class, 'destroy'])->name('destroy');
});

/*
|--------------------------------------------------------------------------
| Tutorial Video Generator (사용자 매뉴얼 영상 자동 생성)
|--------------------------------------------------------------------------
*/
Route::prefix('video/tutorial')->name('video.tutorial.')->middleware('auth')->group(function () {
    Route::get('/', [\App\Http\Controllers\Video\TutorialVideoController::class, 'index'])->name('index');
    Route::post('/upload', [\App\Http\Controllers\Video\TutorialVideoController::class, 'upload'])->name('upload');
    Route::post('/analyze', [\App\Http\Controllers\Video\TutorialVideoController::class, 'analyze'])->name('analyze');
    Route::post('/generate', [\App\Http\Controllers\Video\TutorialVideoController::class, 'generate'])->name('generate');
    Route::get('/status/{id}', [\App\Http\Controllers\Video\TutorialVideoController::class, 'status'])->name('status');
    Route::get('/download/{id}', [\App\Http\Controllers\Video\TutorialVideoController::class, 'download'])->name('download');
    Route::get('/preview/{id}', [\App\Http\Controllers\Video\TutorialVideoController::class, 'preview'])->name('preview');
    Route::get('/history', [\App\Http\Controllers\Video\TutorialVideoController::class, 'history'])->name('history');
    Route::get('/detail/{id}', [\App\Http\Controllers\Video\TutorialVideoController::class, 'detail'])->name('detail');
    Route::delete('/history', [\App\Http\Controllers\Video\TutorialVideoController::class, 'destroy'])->name('destroy');
});

/*
|--------------------------------------------------------------------------
| 설비관리 (Equipment Management)
|--------------------------------------------------------------------------
*/
Route::prefix('equipment')->name('equipment.')->middleware(['auth', 'hq.member', 'password.changed'])->group(function () {
    Route::get('/', [\App\Http\Controllers\EquipmentController::class, 'dashboard'])->name('dashboard');
    Route::get('/registry', [\App\Http\Controllers\EquipmentController::class, 'index'])->name('index');
    Route::get('/registry/create', [\App\Http\Controllers\EquipmentController::class, 'create'])->name('create');
    Route::get('/registry/{id}', [\App\Http\Controllers\EquipmentController::class, 'show'])->whereNumber('id')->name('show');
    Route::get('/registry/{id}/edit', [\App\Http\Controllers\EquipmentController::class, 'edit'])->whereNumber('id')->name('edit');
    Route::get('/inspections', [\App\Http\Controllers\EquipmentController::class, 'inspections'])->name('inspections');
    Route::get('/repairs', [\App\Http\Controllers\EquipmentController::class, 'repairs'])->name('repairs');
    Route::get('/repairs/create', [\App\Http\Controllers\EquipmentController::class, 'repairCreate'])->name('repairs.create');
    Route::get('/import', [\App\Http\Controllers\EquipmentController::class, 'import'])->name('import');
    Route::get('/guide', [\App\Http\Controllers\EquipmentController::class, 'guide'])->name('guide');
});

/*
|--------------------------------------------------------------------------
| SAM E-Sign Public Routes (인증 불필요 - 서명자용)
|--------------------------------------------------------------------------
*/
// 전자계약 전화번호 확인 (공개, 비인증)
Route::post('/esign/verify-phone', [EsignPublicController::class, 'verifyPhone'])->name('esign.verify-phone');

Route::prefix('esign/sign')->group(function () {
    // 화면 라우트
    Route::get('/{token}', [EsignPublicController::class, 'auth'])->name('esign.sign.auth');
    Route::get('/{token}/sign', [EsignPublicController::class, 'sign'])->name('esign.sign.do');
    Route::get('/{token}/done', [EsignPublicController::class, 'done'])->name('esign.sign.done');

    // 서명 API (토큰 기반, 비인증)
    Route::get('/{token}/api/contract', [EsignPublicController::class, 'getContract'])->name('esign.sign.api.contract');
    Route::post('/{token}/api/otp/send', [EsignPublicController::class, 'sendOtp'])->name('esign.sign.api.otp.send');
    Route::post('/{token}/api/otp/verify', [EsignPublicController::class, 'verifyOtp'])->name('esign.sign.api.otp.verify');
    Route::post('/{token}/api/submit', [EsignPublicController::class, 'submitSignature'])->name('esign.sign.api.submit');
    Route::post('/{token}/api/reject', [EsignPublicController::class, 'rejectContract'])->name('esign.sign.api.reject');
    Route::get('/{token}/api/document', [EsignPublicController::class, 'downloadDocument'])->name('esign.sign.api.document');
});
