<?php

namespace Database\Seeders;

use Illuminate\Database\Seeder;
use App\Models\User;
use App\Models\Department;
use App\Models\NotificationSetting;
use Illuminate\Support\Facades\Hash;

class UserSeeder extends Seeder
{
    public function run(): void
    {
        $departments = Department::all();

        // Super Admin
        $superAdmin = User::create([
            'name' => 'Super Administrator',
            'email' => 'superadmin@company.com',
            'password' => Hash::make('password'),
            'role' => 'super_admin',
            'department_id' => $departments->where('code', 'IT')->first()->id,
            'phone' => '081234567890',
            'telegram_chat_id' => null,
            'is_active' => true,
        ]);

        NotificationSetting::create([
            'user_id' => $superAdmin->id,
            'email_enabled' => true,
            'telegram_enabled' => false,
            'notify_for_departments' => [],
            'reminder_days_before' => [30, 7, 1],
        ]);

        // Admin HR
        $adminHR = User::create([
            'name' => 'HR Administrator',
            'email' => 'hr@company.com',
            'password' => Hash::make('password'),
            'role' => 'admin_hr',
            'department_id' => $departments->where('code', 'HR')->first()->id,
            'phone' => '081234567891',
            'is_active' => true,
        ]);

        NotificationSetting::create([
            'user_id' => $adminHR->id,
            'email_enabled' => true,
            'telegram_enabled' => false,
            'notify_for_departments' => [],
            'reminder_days_before' => [30, 7, 1],
        ]);

        // IT Security
        $itSecurity = User::create([
            'name' => 'IT Security Officer',
            'email' => 'security@company.com',
            'password' => Hash::make('password'),
            'role' => 'it_security',
            'department_id' => $departments->where('code', 'IT')->first()->id,
            'phone' => '081234567892',
            'is_active' => true,
        ]);

        NotificationSetting::create([
            'user_id' => $itSecurity->id,
            'email_enabled' => true,
            'telegram_enabled' => false,
            'notify_for_departments' => [],
            'reminder_days_before' => [30, 7, 1],
        ]);

        // Manager (beberapa department)
        $managerRoles = [
            ['name' => 'IT Manager', 'email' => 'it.manager@company.com', 'dept' => 'IT'],
            ['name' => 'HR Manager', 'email' => 'hr.manager@company.com', 'dept' => 'HR'],
            ['name' => 'Finance Manager', 'email' => 'finance.manager@company.com', 'dept' => 'FIN'],
        ];

        foreach ($managerRoles as $manager) {
            $user = User::create([
                'name' => $manager['name'],
                'email' => $manager['email'],
                'password' => Hash::make('password'),
                'role' => 'manager',
                'department_id' => $departments->where('code', $manager['dept'])->first()->id,
                'is_active' => true,
            ]);

            NotificationSetting::create([
                'user_id' => $user->id,
                'email_enabled' => true,
                'telegram_enabled' => false,
                'notify_for_departments' => [$user->department_id],
                'reminder_days_before' => [30, 7, 1],
            ]);
        }

        $this->command->info('✅ Users seeded successfully!');
        $this->command->info('   - superadmin@company.com / password');
        $this->command->info('   - hr@company.com / password');
        $this->command->info('   - security@company.com / password');
    }
}