<?php

namespace Database\Seeders;

use Illuminate\Database\Seeder;

class FolderSeeder extends Seeder
{
    /**
     * Run the database seeds.
     *
     * Usage:
     * php artisan db:seed --class=FolderSeeder
     * or from code: (new FolderSeeder)->run(tenantId: 1)
     */
    public function run(?int $tenantId = null): void
    {
        // 테넌트 ID가 지정되지 않으면 모든 테넌트에 대해 실행
        $tenants = $tenantId
            ? [\App\Models\Tenants\Tenant::findOrFail($tenantId)]
            : \App\Models\Tenants\Tenant::all();

        foreach ($tenants as $tenant) {
            // 이미 폴더가 있는지 확인
            if (\App\Models\Folder::where('tenant_id', $tenant->id)->exists()) {
                $this->command->info("Tenant {$tenant->id} already has folders. Skipping...");

                continue;
            }

            // 기본 폴더 생성
            $defaultFolders = [
                [
                    'folder_key' => 'product',
                    'folder_name' => '생산관리',
                    'description' => '생산 관련 문서 및 도면',
                    'icon' => 'icon-production',
                    'color' => '#3B82F6',
                    'display_order' => 1,
                ],
                [
                    'folder_key' => 'quality',
                    'folder_name' => '품질관리',
                    'description' => '품질 검사 및 인증 문서',
                    'icon' => 'icon-quality',
                    'color' => '#10B981',
                    'display_order' => 2,
                ],
                [
                    'folder_key' => 'accounting',
                    'folder_name' => '회계',
                    'description' => '회계 관련 증빙 서류',
                    'icon' => 'icon-accounting',
                    'color' => '#F59E0B',
                    'display_order' => 3,
                ],
                [
                    'folder_key' => 'hr',
                    'folder_name' => '인사',
                    'description' => '인사 관련 문서',
                    'icon' => 'icon-hr',
                    'color' => '#8B5CF6',
                    'display_order' => 4,
                ],
                [
                    'folder_key' => 'general',
                    'folder_name' => '일반',
                    'description' => '기타 문서',
                    'icon' => 'icon-general',
                    'color' => '#6B7280',
                    'display_order' => 5,
                ],
            ];

            foreach ($defaultFolders as $folder) {
                \App\Models\Folder::create([
                    'tenant_id' => $tenant->id,
                    'folder_key' => $folder['folder_key'],
                    'folder_name' => $folder['folder_name'],
                    'description' => $folder['description'],
                    'icon' => $folder['icon'],
                    'color' => $folder['color'],
                    'display_order' => $folder['display_order'],
                    'is_active' => true,
                ]);
            }

            $this->command->info("Created default folders for tenant {$tenant->id}");
        }
    }
}
