<?php

namespace Database\Seeders;

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

class UserSeeder extends Seeder
{
    public function run(): void
    {
        $users = [
            [
                'name' => 'علی محمدی',
                'email' => 'admin@pm.com',
                'password' => Hash::make('password'),
                'phone' => '09121111111',
                'job_title' => 'مدیر سیستم',
                'department' => 'فناوری اطلاعات',
                'status' => 'active',
                'avatar' => 'avatars/default.png',
                'email_verified_at' => now(),
                'created_at' => now(),
                'updated_at' => now(),
            ],
            [
                'name' => 'سارا احمدی',
                'email' => 'manager@pm.com',
                'password' => Hash::make('password'),
                'phone' => '09122222222',
                'job_title' => 'مدیر پروژه',
                'department' => 'مدیریت پروژه',
                'status' => 'active',
                'avatar' => 'avatars/default.png',
                'email_verified_at' => now(),
                'created_at' => now(),
                'updated_at' => now(),
            ],
            [
                'name' => 'رضا کریمی',
                'email' => 'member@pm.com',
                'password' => Hash::make('password'),
                'phone' => '09123333333',
                'job_title' => 'توسعه‌دهنده',
                'department' => 'فناوری اطلاعات',
                'status' => 'active',
                'avatar' => 'avatars/default.png',
                'email_verified_at' => now(),
                'created_at' => now(),
                'updated_at' => now(),
            ],
            [
                'name' => 'مریم حسینی',
                'email' => 'observer@pm.com',
                'password' => Hash::make('password'),
                'phone' => '09124444444',
                'job_title' => 'کارشناس',
                'department' => 'کیفیت',
                'status' => 'active',
                'avatar' => 'avatars/default.png',
                'email_verified_at' => now(),
                'created_at' => now(),
                'updated_at' => now(),
            ],
            [
                'name' => 'دکتر امیر رضایی',
                'email' => 'ceo@pm.com',
                'password' => Hash::make('password'),
                'phone' => '09125555555',
                'job_title' => 'مدیرعامل',
                'department' => 'مدیریت ارشد',
                'status' => 'active',
                'avatar' => 'avatars/default.png',
                'email_verified_at' => now(),
                'created_at' => now(),
                'updated_at' => now(),
            ],
        ];

        foreach ($users as $user) {
            $createdUser = User::create($user);
            $role = match ($createdUser->email) {
                'admin@pm.com' => 'admin',
                'manager@pm.com' => 'project_manager',
                'member@pm.com' => 'specialist',
                'observer@pm.com' => 'observer',
                'ceo@pm.com' => 'ceo',
                default => 'specialist',
            };
            $roleId = DB::table('roles')->where('name', $role)->value('id');
            DB::table('role_user')->insert([
                'role_id' => $roleId,
                'user_id' => $createdUser->id,
                'created_at' => now(),
                'updated_at' => now(),
            ]);
        }
    }
}
