<?php

namespace Database\Seeders;

use Illuminate\Database\Seeder;
use App\Models\User;
use App\Models\Department;
use Illuminate\Support\Facades\Hash;
use Spatie\Permission\Models\Role;

class UserSeeder extends Seeder
{
    public function run(): void
    {
        $eng = Department::where('name', 'Engineering')->first();
        $hr  = Department::where('name', 'HR')->first();
        $mkt = Department::where('name', 'Marketing')->first();

        // Super Admin
        $superAdmin = User::updateOrCreate(
            ['email' => 'super_admin@company.com'],
            [
                'name' => 'Super Admin',
                'password' => Hash::make('password'),
                'role' => 'super_admin',
                'department_id' => $eng->id,
                'designation' => 'Chief Technology Officer',
                'is_active' => true,
            ]
        );
        $superAdmin->assignRole('super_admin');

        // Admin
        $admin = User::updateOrCreate(
            ['email' => 'admin@company.com'],
            [
                'name' => 'Admin HR',
                'password' => Hash::make('password'),
                'role' => 'admin',
                'department_id' => $hr->id,
                'designation' => 'HR Manager',
                'is_active' => true,
            ]
        );
        $admin->assignRole('admin');

        // Manager
        $manager = User::updateOrCreate(
            ['email' => 'manager@company.com'],
            [
                'name' => 'Manager Eng',
                'password' => Hash::make('password'),
                'role' => 'manager',
                'department_id' => $eng->id,
                'designation' => 'Engineering Lead',
                'is_active' => true,
            ]
        );
        $manager->assignRole('manager');

        // Employees
        $employee1 = User::updateOrCreate(
            ['email' => 'employee1@company.com'],
            [
                'name' => 'Employee 1',
                'password' => Hash::make('password'),
                'role' => 'employee',
                'department_id' => $eng->id,
                'designation' => 'Senior Developer',
                'is_active' => true,
            ]
        );
        $employee1->assignRole('employee');

        $employee2 = User::updateOrCreate(
            ['email' => 'employee2@company.com'],
            [
                'name' => 'Employee 2',
                'password' => Hash::make('password'),
                'role' => 'employee',
                'department_id' => $mkt->id,
                'designation' => 'Marketing Specialist',
                'is_active' => true,
            ]
        );
        $employee2->assignRole('employee');

        $employee3 = User::updateOrCreate(
            ['email' => 'employee3@company.com'],
            [
                'name' => 'Employee 3',
                'password' => Hash::make('password'),
                'role' => 'employee',
                'department_id' => $eng->id,
                'designation' => 'Junior Developer',
                'is_active' => true,
            ]
        );
        $employee3->assignRole('employee');
        
        // Link manager to department
        $eng->update(['manager_id' => $manager->id]);
        $hr->update(['manager_id' => $admin->id]);
    }
}
