<?php

namespace Database\Seeders\Dummy;

use App\Models\Orders\Client;
use App\Models\Orders\ClientGroup;
use Database\Seeders\DummyDataSeeder;
use Illuminate\Database\Seeder;

class DummyClientSeeder extends Seeder
{
    public function run(): void
    {
        $tenantId = DummyDataSeeder::TENANT_ID;
        $userId = DummyDataSeeder::USER_ID;

        // 기존 데이터 있으면 스킵
        $existing = Client::where('tenant_id', $tenantId)->count();
        if ($existing > 0) {
            $this->command->info('   ⚠ clients: 이미 '.$existing.'개 존재 (스킵)');

            return;
        }

        // 그룹 ID 조회
        $groups = ClientGroup::where('tenant_id', $tenantId)
            ->pluck('id', 'group_code')
            ->toArray();

        // 매출처 (SALES) - 10개
        $salesClients = [
            ['code' => 'S001', 'name' => '삼성전자',     'business_no' => '124-81-00998', 'group' => 'VIP',    'contact' => '김철수', 'phone' => '02-1234-5678', 'email' => 'kim@samsung.com'],
            ['code' => 'S002', 'name' => 'LG전자',       'business_no' => '107-86-14075', 'group' => 'VIP',    'contact' => '이영희', 'phone' => '02-2345-6789', 'email' => 'lee@lg.com'],
            ['code' => 'S003', 'name' => 'SK하이닉스',   'business_no' => '204-81-17169', 'group' => 'GOLD',   'contact' => '박민수', 'phone' => '031-123-4567', 'email' => 'park@skhynix.com'],
            ['code' => 'S004', 'name' => '현대자동차',   'business_no' => '101-81-05765', 'group' => 'GOLD',   'contact' => '정은지', 'phone' => '02-3456-7890', 'email' => 'jung@hyundai.com'],
            ['code' => 'S005', 'name' => '네이버',       'business_no' => '220-81-62517', 'group' => 'GOLD',   'contact' => '최준호', 'phone' => '031-234-5678', 'email' => 'choi@naver.com'],
            ['code' => 'S006', 'name' => '카카오',       'business_no' => '120-87-65763', 'group' => 'SILVER', 'contact' => '강미래', 'phone' => '02-4567-8901', 'email' => 'kang@kakao.com'],
            ['code' => 'S007', 'name' => '쿠팡',         'business_no' => '120-88-00767', 'group' => 'SILVER', 'contact' => '임도현', 'phone' => '02-5678-9012', 'email' => 'lim@coupang.com'],
            ['code' => 'S008', 'name' => '토스',         'business_no' => '120-87-83139', 'group' => 'NORMAL', 'contact' => '윤서연', 'phone' => '02-6789-0123', 'email' => 'yoon@toss.im'],
            ['code' => 'S009', 'name' => '배달의민족',   'business_no' => '220-87-93847', 'group' => 'NORMAL', 'contact' => '한지민', 'phone' => '02-7890-1234', 'email' => 'han@woowahan.com'],
            ['code' => 'S010', 'name' => '당근마켓',     'business_no' => '815-87-01234', 'group' => 'NEW',    'contact' => '오태양', 'phone' => '02-8901-2345', 'email' => 'oh@daangn.com'],
        ];

        // 매입처 (PURCHASE) - 7개
        $purchaseClients = [
            ['code' => 'P001', 'name' => '한화솔루션',   'business_no' => '138-81-00610', 'group' => null, 'contact' => '김재원', 'phone' => '02-1111-2222', 'email' => 'kim@hanwha.com'],
            ['code' => 'P002', 'name' => '포스코',       'business_no' => '506-81-08754', 'group' => null, 'contact' => '이현석', 'phone' => '054-111-2222', 'email' => 'lee@posco.com'],
            ['code' => 'P003', 'name' => '롯데케미칼',   'business_no' => '301-81-07123', 'group' => null, 'contact' => '박서준', 'phone' => '02-2222-3333', 'email' => 'park@lottechem.com'],
            ['code' => 'P004', 'name' => 'GS칼텍스',     'business_no' => '104-81-23858', 'group' => null, 'contact' => '정해인', 'phone' => '02-3333-4444', 'email' => 'jung@gscaltex.com'],
            ['code' => 'P005', 'name' => '대한항공',     'business_no' => '110-81-14794', 'group' => null, 'contact' => '송민호', 'phone' => '02-4444-5555', 'email' => 'song@koreanair.com'],
            ['code' => 'P006', 'name' => '현대제철',     'business_no' => '130-81-12345', 'group' => null, 'contact' => '강동원', 'phone' => '032-555-6666', 'email' => 'kang@hyundaisteel.com'],
            ['code' => 'P007', 'name' => 'SK이노베이션', 'business_no' => '110-81-67890', 'group' => null, 'contact' => '유재석', 'phone' => '02-6666-7777', 'email' => 'yoo@skinnovation.com'],
        ];

        // 매출매입처 (BOTH) - 3개
        $bothClients = [
            ['code' => 'B001', 'name' => '두산에너빌리티', 'business_no' => '124-81-08628', 'group' => 'GOLD',   'contact' => '조인성', 'phone' => '02-7777-8888', 'email' => 'cho@doosan.com'],
            ['code' => 'B002', 'name' => 'CJ대한통운',    'business_no' => '104-81-39849', 'group' => 'SILVER', 'contact' => '공유',   'phone' => '02-8888-9999', 'email' => 'gong@cjlogistics.com'],
            ['code' => 'B003', 'name' => '삼성SDS',       'business_no' => '124-81-34567', 'group' => 'VIP',    'contact' => '이정재', 'phone' => '02-9999-0000', 'email' => 'lee@samsungsds.com'],
        ];

        $count = 0;

        // 매출처 생성
        foreach ($salesClients as $client) {
            $this->createClient($client, 'SALES', $tenantId, $userId, $groups);
            $count++;
        }

        // 매입처 생성
        foreach ($purchaseClients as $client) {
            $this->createClient($client, 'PURCHASE', $tenantId, $userId, $groups);
            $count++;
        }

        // 매출매입처 생성
        foreach ($bothClients as $client) {
            $this->createClient($client, 'BOTH', $tenantId, $userId, $groups);
            $count++;
        }

        $this->command->info('   ✓ clients: '.$count.'건 생성');
    }

    private function createClient(array $data, string $type, int $tenantId, int $userId, array $groups): void
    {
        Client::create([
            'tenant_id' => $tenantId,
            'client_group_id' => $data['group'] ? ($groups[$data['group']] ?? null) : null,
            'client_code' => $data['code'],
            'name' => $data['name'],
            'client_type' => $type,
            'contact_person' => $data['contact'],
            'phone' => $data['phone'],
            'email' => $data['email'],
            'business_no' => $data['business_no'],
            'business_type' => '제조업',
            'business_item' => '전자제품',
            'is_active' => true,
        ]);
    }
}
