<?php

namespace Database\Seeders;

use App\Models\Commons\GlobalMenu;
use Illuminate\Database\Seeder;
use Illuminate\Support\Facades\DB;

class ReactMenuSeeder extends Seeder
{
    /**
     * React 라우트 기반 글로벌 메뉴 시딩
     * docs/projects/api-integration/phase-2-menu-extraction/menu-list.md 참조
     *
     * 대상: 결재관리, 게시판, 품목관리, 판매관리, 생산관리, 회계관리, 인사관리, 리포트, 고객센터, 설정
     */
    public function run(): void
    {
        DB::transaction(function () {
            // React 메뉴 구조 정의 (menu-list.md JSON 기반)
            $menus = [
                [
                    'name' => '대시보드',
                    'url' => '/dashboard',
                    'icon' => 'layout-dashboard',
                    'sort_order' => 1,
                    'children' => [],
                ],
                [
                    'name' => '결재관리',
                    'url' => '/approval',
                    'icon' => 'file-check',
                    'sort_order' => 2,
                    'children' => [
                        ['name' => '기안함', 'url' => '/approval/draft', 'icon' => 'file-edit'],
                        ['name' => '결재함', 'url' => '/approval/inbox', 'icon' => 'inbox'],
                        ['name' => '참조함', 'url' => '/approval/reference', 'icon' => 'eye'],
                    ],
                ],
                [
                    'name' => '게시판',
                    'url' => '/board',
                    'icon' => 'layout-list',
                    'sort_order' => 3,
                    'children' => [
                        ['name' => '게시판 관리', 'url' => '/board/board-management', 'icon' => 'settings'],
                    ],
                ],
                [
                    'name' => '품목관리',
                    'url' => '/items',
                    'icon' => 'package',
                    'sort_order' => 4,
                    'children' => [
                        ['name' => '품목기준관리', 'url' => '/master-data/item-master-data-management', 'icon' => 'database'],
                    ],
                ],
                [
                    'name' => '판매관리',
                    'url' => '/sales',
                    'icon' => 'briefcase',
                    'sort_order' => 5,
                    'children' => [
                        ['name' => '거래처관리', 'url' => '/sales/client-management-sales-admin', 'icon' => 'building-2'],
                        ['name' => '견적관리', 'url' => '/sales/quote-management', 'icon' => 'calculator'],
                        ['name' => '단가관리', 'url' => '/sales/pricing-management', 'icon' => 'dollar-sign'],
                    ],
                ],
                [
                    'name' => '생산관리',
                    'url' => '/production',
                    'icon' => 'factory',
                    'sort_order' => 6,
                    'children' => [
                        ['name' => '스크린 생산', 'url' => '/production/screen-production', 'icon' => 'package'],
                    ],
                ],
                [
                    'name' => '회계관리',
                    'url' => '/accounting',
                    'icon' => 'calculator',
                    'sort_order' => 7,
                    'children' => [
                        ['name' => '거래처관리', 'url' => '/accounting/vendors', 'icon' => 'building-2'],
                        ['name' => '거래처원장', 'url' => '/accounting/vendor-ledger', 'icon' => 'file-text'],
                        ['name' => '매출관리', 'url' => '/accounting/sales', 'icon' => 'trending-up'],
                        ['name' => '매입관리', 'url' => '/accounting/purchase', 'icon' => 'trending-down'],
                        ['name' => '세금계산서', 'url' => '/accounting/bills', 'icon' => 'receipt'],
                        ['name' => '입금관리', 'url' => '/accounting/deposits', 'icon' => 'arrow-down-circle'],
                        ['name' => '출금관리', 'url' => '/accounting/withdrawals', 'icon' => 'arrow-up-circle'],
                        ['name' => '은행거래', 'url' => '/accounting/bank-transactions', 'icon' => 'landmark'],
                        ['name' => '카드거래', 'url' => '/accounting/card-transactions', 'icon' => 'credit-card'],
                        ['name' => '미수금현황', 'url' => '/accounting/receivables-status', 'icon' => 'alert-circle'],
                        ['name' => '예상비용', 'url' => '/accounting/expected-expenses', 'icon' => 'clock'],
                        ['name' => '대손채권회수', 'url' => '/accounting/bad-debt-collection', 'icon' => 'shield'],
                        ['name' => '일일리포트', 'url' => '/accounting/daily-report', 'icon' => 'bar-chart-3'],
                        ['name' => '결제내역', 'url' => '/payment-history', 'icon' => 'history'],
                    ],
                ],
                [
                    'name' => '인사관리',
                    'url' => '/hr',
                    'icon' => 'users',
                    'sort_order' => 8,
                    'children' => [
                        ['name' => '직원관리', 'url' => '/hr/employee-management', 'icon' => 'user'],
                        ['name' => '부서관리', 'url' => '/hr/department-management', 'icon' => 'building'],
                        ['name' => '카드관리', 'url' => '/hr/card-management', 'icon' => 'credit-card'],
                        ['name' => '근태현황', 'url' => '/hr/attendance', 'icon' => 'clock'],
                        ['name' => '근태관리', 'url' => '/hr/attendance-management', 'icon' => 'calendar-check'],
                        ['name' => '급여관리', 'url' => '/hr/salary-management', 'icon' => 'dollar-sign'],
                        ['name' => '휴가관리', 'url' => '/hr/vacation-management', 'icon' => 'calendar'],
                    ],
                ],
                [
                    'name' => '리포트',
                    'url' => '/reports',
                    'icon' => 'bar-chart-3',
                    'sort_order' => 9,
                    'children' => [
                        ['name' => '종합분석', 'url' => '/reports/comprehensive-analysis', 'icon' => 'pie-chart'],
                    ],
                ],
                [
                    'name' => '고객센터',
                    'url' => '/customer-center',
                    'icon' => 'headphones',
                    'sort_order' => 10,
                    'children' => [
                        ['name' => '공지사항', 'url' => '/customer-center/notices', 'icon' => 'megaphone'],
                        ['name' => 'FAQ', 'url' => '/customer-center/faq', 'icon' => 'help-circle'],
                        ['name' => '문의하기', 'url' => '/customer-center/inquiries', 'icon' => 'message-circle'],
                        ['name' => '이벤트', 'url' => '/customer-center/events', 'icon' => 'calendar'],
                    ],
                ],
                [
                    'name' => '설정',
                    'url' => '/settings',
                    'icon' => 'settings',
                    'sort_order' => 11,
                    'children' => [
                        ['name' => '계정정보', 'url' => '/settings/account-info', 'icon' => 'user'],
                        ['name' => '계정관리', 'url' => '/settings/accounts', 'icon' => 'users'],
                        ['name' => '권한관리', 'url' => '/settings/permissions', 'icon' => 'shield'],
                        ['name' => '직급관리', 'url' => '/settings/ranks', 'icon' => 'award'],
                        ['name' => '직책관리', 'url' => '/settings/titles', 'icon' => 'award'],
                        ['name' => '근태설정', 'url' => '/settings/attendance-settings', 'icon' => 'clock'],
                        ['name' => '휴가정책', 'url' => '/settings/leave-policy', 'icon' => 'calendar'],
                        ['name' => '근무일정', 'url' => '/settings/work-schedule', 'icon' => 'calendar-days'],
                        ['name' => '알림설정', 'url' => '/settings/notification-settings', 'icon' => 'bell'],
                        ['name' => '팝업관리', 'url' => '/settings/popup-management', 'icon' => 'layout'],
                        ['name' => '회사정보', 'url' => '/company-info', 'icon' => 'building'],
                        ['name' => '구독관리', 'url' => '/subscription', 'icon' => 'credit-card'],
                    ],
                ],
            ];

            // 메뉴 생성 (global_menus 테이블에 저장)
            foreach ($menus as $menuData) {
                $children = $menuData['children'] ?? [];
                unset($menuData['children']);

                // 최상위 메뉴 생성
                $parentMenu = GlobalMenu::create([
                    'parent_id' => null,
                    'name' => $menuData['name'],
                    'url' => $menuData['url'],
                    'icon' => $menuData['icon'],
                    'sort_order' => $menuData['sort_order'],
                    'is_active' => true,
                    'hidden' => false,
                    'is_external' => false,
                    'external_url' => null,
                ]);

                // 하위 메뉴 생성
                if (count($children) > 0) {
                    foreach ($children as $index => $childData) {
                        GlobalMenu::create([
                            'parent_id' => $parentMenu->id,
                            'name' => $childData['name'],
                            'url' => $childData['url'],
                            'icon' => $childData['icon'],
                            'sort_order' => $index + 1,
                            'is_active' => true,
                            'hidden' => false,
                            'is_external' => false,
                            'external_url' => null,
                        ]);
                    }
                }
            }
        });

        $this->command->info('✅ React 메뉴 생성 완료 (global_menus 테이블, 11개 대메뉴, 54개 중메뉴)');
    }
}
