<?php

namespace Database\Seeders\Dummy;

use App\Models\Members\User;
use Database\Seeders\DummyDataSeeder;
use Illuminate\Database\Seeder;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Hash;

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

        // 더미 직원 데이터 (15명)
        $employees = [
            ['user_id' => 'EMP001', 'name' => '김철수', 'email' => 'chulsoo.kim@test.com', 'phone' => '010-1234-5678'],
            ['user_id' => 'EMP002', 'name' => '이영희', 'email' => 'younghee.lee@test.com', 'phone' => '010-2345-6789'],
            ['user_id' => 'EMP003', 'name' => '박민수', 'email' => 'minsoo.park@test.com', 'phone' => '010-3456-7890'],
            ['user_id' => 'EMP004', 'name' => '정은지', 'email' => 'eunji.jung@test.com', 'phone' => '010-4567-8901'],
            ['user_id' => 'EMP005', 'name' => '최준호', 'email' => 'junho.choi@test.com', 'phone' => '010-5678-9012'],
            ['user_id' => 'EMP006', 'name' => '강미래', 'email' => 'mirae.kang@test.com', 'phone' => '010-6789-0123'],
            ['user_id' => 'EMP007', 'name' => '임도현', 'email' => 'dohyun.lim@test.com', 'phone' => '010-7890-1234'],
            ['user_id' => 'EMP008', 'name' => '윤서연', 'email' => 'seoyeon.yoon@test.com', 'phone' => '010-8901-2345'],
            ['user_id' => 'EMP009', 'name' => '한지민', 'email' => 'jimin.han@test.com', 'phone' => '010-9012-3456'],
            ['user_id' => 'EMP010', 'name' => '오태양', 'email' => 'taeyang.oh@test.com', 'phone' => '010-0123-4567'],
            ['user_id' => 'EMP011', 'name' => '신동욱', 'email' => 'dongwook.shin@test.com', 'phone' => '010-1111-2222'],
            ['user_id' => 'EMP012', 'name' => '권나래', 'email' => 'narae.kwon@test.com', 'phone' => '010-2222-3333'],
            ['user_id' => 'EMP013', 'name' => '조성민', 'email' => 'sungmin.cho@test.com', 'phone' => '010-3333-4444'],
            ['user_id' => 'EMP014', 'name' => '백지훈', 'email' => 'jihun.baek@test.com', 'phone' => '010-4444-5555'],
            ['user_id' => 'EMP015', 'name' => '송하늘', 'email' => 'haneul.song@test.com', 'phone' => '010-5555-6666'],
        ];

        $count = 0;

        foreach ($employees as $employee) {
            // 이미 존재하는지 확인
            $existing = User::where('email', $employee['email'])->first();
            if ($existing) {
                // 이미 tenant에 연결되어 있는지 확인
                $linked = DB::table('user_tenants')
                    ->where('user_id', $existing->id)
                    ->where('tenant_id', $tenantId)
                    ->exists();

                if (! $linked) {
                    DB::table('user_tenants')->insert([
                        'user_id' => $existing->id,
                        'tenant_id' => $tenantId,
                        'is_active' => true,
                        'is_default' => false,
                        'joined_at' => now(),
                        'created_at' => now(),
                        'updated_at' => now(),
                    ]);
                }

                continue;
            }

            $user = User::create([
                'user_id' => $employee['user_id'],
                'name' => $employee['name'],
                'email' => $employee['email'],
                'phone' => $employee['phone'],
                'password' => Hash::make('password123'),
                'is_active' => true,
                'created_by' => $userId,
            ]);

            // 테넌트에 연결
            DB::table('user_tenants')->insert([
                'user_id' => $user->id,
                'tenant_id' => $tenantId,
                'is_active' => true,
                'is_default' => true,
                'joined_at' => now(),
                'created_at' => now(),
                'updated_at' => now(),
            ]);

            $count++;
        }

        $this->command->info('   ✓ users: '.$count.'명 생성 (테넌트 연결 완료)');
    }
}
