<?php

namespace Database\Seeders;

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

/**
 * SelfPacedTraineeSeeder
 *
 * Creates 5 sample Trainee users for testing the Self-Paced Learning Portal.
 * These users are assigned the "Trainees" Spatie role and have realistic
 * South Sudan-appropriate names and email addresses.
 *
 * Password for all seeded trainees: Trainee@2026!
 *
 * This seeder is idempotent: existing users are skipped on re-run.
 * Depends on: RolePermissionSeeder (Trainees role must exist)
 *
 * Requirements: 15.7
 */
class SelfPacedTraineeSeeder extends Seeder
{
    private const DEFAULT_PASSWORD = 'Trainee@2026!';

    public function run(): void
    {
        $traineesRole = Role::findByName('Trainees');

        if (!$traineesRole) {
            $this->command->warn('SelfPacedTraineeSeeder: "Trainees" role not found. Run RolePermissionSeeder first.');
            return;
        }

        $trainees = [
            [
                'name'     => 'Akuol Deng Majok',
                'email'    => 'akuol.deng@sshia-learner.org',
                'username' => 'akuol.deng@sshia-learner.org',
                'phone'    => '+256700100001',
            ],
            [
                'name'     => 'James Wani Ladu',
                'email'    => 'james.wani@sshia-learner.org',
                'username' => 'james.wani@sshia-learner.org',
                'phone'    => '+256700100002',
            ],
            [
                'name'     => 'Nyakim Gatwech Tut',
                'email'    => 'nyakim.gatwech@sshia-learner.org',
                'username' => 'nyakim.gatwech@sshia-learner.org',
                'phone'    => '+256700100003',
            ],
            [
                'name'     => 'Emmanuel Lado Kenyi',
                'email'    => 'emmanuel.lado@sshia-learner.org',
                'username' => 'emmanuel.lado@sshia-learner.org',
                'phone'    => '+256700100004',
            ],
            [
                'name'     => 'Achol Atem Dut',
                'email'    => 'achol.atem@sshia-learner.org',
                'username' => 'achol.atem@sshia-learner.org',
                'phone'    => '+256700100005',
            ],
        ];

        foreach ($trainees as $data) {
            $user = User::firstOrCreate(
                ['email' => $data['email']],
                [
                    'name'      => $data['name'],
                    'username'  => $data['username'],
                    'phone'     => $data['phone'],
                    'password'  => Hash::make(self::DEFAULT_PASSWORD),
                    'is_active' => true,
                ]
            );

            // Assign Trainees role if not already assigned
            if (!$user->hasRole('Trainees')) {
                $user->assignRole($traineesRole);
            }
        }

        $this->command->info('SelfPacedTraineeSeeder: 5 trainee users seeded.');
        $this->command->line('  Password for all trainees: ' . self::DEFAULT_PASSWORD);
    }
}
