<?php

namespace Database\Seeders;

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

class UserRoleDemoSeeder extends Seeder
{
    public function run(): void
    {
        $admin = User::whereNotNull('business_id')->orderByDesc('is_admin')->firstOrFail();
        $roles = Role::orderBy('id')->get();
        $teams = ['Sales', 'Marketing', 'Support', 'Operations', 'Management'];
        for ($i = 1; $i <= 50; $i++) {
            $user = User::updateOrCreate(
                ['email' => sprintf('qa.user.%03d.20260803@example.test', $i)],
                [
                    'business_id' => $admin->business_id,
                    'name' => sprintf('QA User 50-%03d', $i),
                    'password' => Hash::make('LocalTest@2026'),
                    'parent_user_id' => $admin->id,
                    'team_name' => $teams[($i - 1) % count($teams)],
                    'reporting_manager_id' => $admin->id,
                ]
            );
            $user->forceFill(['email_verified_at' => $i % 5 === 0 ? null : now()])->save();
            if ($roles->isNotEmpty()) $user->syncRoles([$roles[($i - 1) % $roles->count()]->name]);
        }
    }
}
