<?php

namespace Database\Seeders;

use App\Models\SystemFieldDefinition;
use Illuminate\Database\Seeder;

class SystemFieldDefinitionSeeder extends Seeder
{
    /**
     * Run the database seeds.
     */
    public function run(): void
    {
        $definitions = [
            // ========================================
            // items 테이블 시스템 필드
            // ========================================
            ...array_map(fn ($field) => array_merge($field, [
                'source_table' => 'items',
                'source_table_label' => '품목',
            ]), [
                ['field_key' => 'code', 'field_name' => '품목코드', 'field_type' => 'textbox', 'is_required' => true, 'is_seed_default' => true, 'order_no' => 1],
                ['field_key' => 'name', 'field_name' => '품목명', 'field_type' => 'textbox', 'is_required' => true, 'is_seed_default' => true, 'order_no' => 2],
                ['field_key' => 'item_type', 'field_name' => '품목유형', 'field_type' => 'dropdown', 'is_required' => true, 'is_seed_default' => true, 'order_no' => 3, 'options' => json_encode([
                    ['label' => '제품', 'value' => 'product'],
                    ['label' => '자재', 'value' => 'material'],
                    ['label' => '반제품', 'value' => 'semi'],
                ])],
                ['field_key' => 'unit', 'field_name' => '단위', 'field_type' => 'dropdown', 'is_required' => true, 'is_seed_default' => true, 'order_no' => 4],
                ['field_key' => 'category_id', 'field_name' => '카테고리', 'field_type' => 'dropdown', 'is_required' => false, 'is_seed_default' => true, 'order_no' => 5],
                ['field_key' => 'description', 'field_name' => '설명', 'field_type' => 'textarea', 'is_required' => false, 'is_seed_default' => false, 'order_no' => 6],
                ['field_key' => 'is_active', 'field_name' => '활성화', 'field_type' => 'checkbox', 'is_required' => false, 'is_seed_default' => true, 'order_no' => 7, 'default_value' => 'true'],
            ]),

            // ========================================
            // tenants 테이블 시스템 필드
            // ========================================
            ...array_map(fn ($field) => array_merge($field, [
                'source_table' => 'tenants',
                'source_table_label' => '테넌트',
            ]), [
                ['field_key' => 'company_name', 'field_name' => '회사명', 'field_type' => 'textbox', 'is_required' => true, 'is_seed_default' => true, 'order_no' => 1],
                ['field_key' => 'code', 'field_name' => '회사코드', 'field_type' => 'textbox', 'is_required' => true, 'is_seed_default' => true, 'order_no' => 2],
                ['field_key' => 'email', 'field_name' => '이메일', 'field_type' => 'textbox', 'is_required' => false, 'is_seed_default' => true, 'order_no' => 3],
                ['field_key' => 'phone', 'field_name' => '전화번호', 'field_type' => 'textbox', 'is_required' => false, 'is_seed_default' => true, 'order_no' => 4],
                ['field_key' => 'address', 'field_name' => '주소', 'field_type' => 'textarea', 'is_required' => false, 'is_seed_default' => false, 'order_no' => 5],
                ['field_key' => 'business_num', 'field_name' => '사업자번호', 'field_type' => 'textbox', 'is_required' => false, 'is_seed_default' => true, 'order_no' => 6],
                ['field_key' => 'corp_reg_no', 'field_name' => '법인등록번호', 'field_type' => 'textbox', 'is_required' => false, 'is_seed_default' => false, 'order_no' => 7],
                ['field_key' => 'ceo_name', 'field_name' => '대표자명', 'field_type' => 'textbox', 'is_required' => false, 'is_seed_default' => true, 'order_no' => 8],
                ['field_key' => 'homepage', 'field_name' => '홈페이지', 'field_type' => 'textbox', 'is_required' => false, 'is_seed_default' => false, 'order_no' => 9],
                ['field_key' => 'fax', 'field_name' => '팩스', 'field_type' => 'textbox', 'is_required' => false, 'is_seed_default' => false, 'order_no' => 10],
                ['field_key' => 'tenant_type', 'field_name' => '테넌트유형', 'field_type' => 'dropdown', 'is_required' => false, 'is_seed_default' => false, 'order_no' => 11],
                ['field_key' => 'max_users', 'field_name' => '최대사용자수', 'field_type' => 'number', 'is_required' => false, 'is_seed_default' => false, 'order_no' => 12],
            ]),

            // ========================================
            // users 테이블 시스템 필드
            // ========================================
            ...array_map(fn ($field) => array_merge($field, [
                'source_table' => 'users',
                'source_table_label' => '사용자',
            ]), [
                ['field_key' => 'name', 'field_name' => '이름', 'field_type' => 'textbox', 'is_required' => true, 'is_seed_default' => true, 'order_no' => 1],
                ['field_key' => 'email', 'field_name' => '이메일', 'field_type' => 'textbox', 'is_required' => true, 'is_seed_default' => true, 'order_no' => 2],
                ['field_key' => 'phone', 'field_name' => '전화번호', 'field_type' => 'textbox', 'is_required' => false, 'is_seed_default' => true, 'order_no' => 3],
                ['field_key' => 'role', 'field_name' => '역할', 'field_type' => 'dropdown', 'is_required' => false, 'is_seed_default' => true, 'order_no' => 4],
                ['field_key' => 'is_active', 'field_name' => '활성화', 'field_type' => 'checkbox', 'is_required' => false, 'is_seed_default' => true, 'order_no' => 5, 'default_value' => 'true'],
                ['field_key' => 'is_super_admin', 'field_name' => '슈퍼관리자', 'field_type' => 'checkbox', 'is_required' => false, 'is_seed_default' => false, 'order_no' => 6, 'default_value' => 'false'],
            ]),
        ];

        foreach ($definitions as $definition) {
            SystemFieldDefinition::updateOrCreate(
                [
                    'source_table' => $definition['source_table'],
                    'field_key' => $definition['field_key'],
                ],
                array_merge($definition, ['is_active' => true])
            );
        }

        $this->command->info('SystemFieldDefinition 시딩 완료: '.count($definitions).'개');
    }
}
