<?php

namespace Database\Seeders;

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

/**
 * UserSeeder
 *
 * Creates the default SSHIA system users — one per role — with real
 * organisational email addresses on the sshia.org domain. All users
 * are active and ready for immediate use after a fresh installation.
 *
 * Credentials are documented in docs/credentials/user-credentials.md
 *
 * Password for all seeded users: Sshia@2026!
 *
 * This seeder is idempotent: existing users are skipped on re-run.
 * Depends on: RolePermissionSeeder (roles must exist before assignment)
 */
class UserSeeder extends Seeder
{
    /**
     * The default password used for all seeded users.
     * Change this immediately after first login in production.
     */
    private const DEFAULT_PASSWORD = 'Sshia@2026!';

    public function run(): void
    {
        $users = [
            [
                'name'      => 'SSHIA Administrator',
                'email'     => 'admin@sshia.org',
                'username'  => 'admin@sshia.org',
                'role'      => 'Super Manager',
                'is_active' => true,
            ],
            [
                'name'      => 'Operations Manager',
                'email'     => 'manager@sshia.org',
                'username'  => 'manager@sshia.org',
                'role'      => 'Manager',
                'is_active' => true,
            ],
            [
                'name'      => 'SSHIA Member',
                'email'     => 'member@sshia.org',
                'username'  => 'member@sshia.org',
                'role'      => 'Members',
                'is_active' => true,
            ],
            [
                'name'      => 'Health Trainer',
                'email'     => 'trainer@sshia.org',
                'username'  => 'trainer@sshia.org',
                'role'      => 'Trainers',
                'is_active' => true,
            ],
            [
                'name'      => 'Health Trainee',
                'email'     => 'trainee@sshia.org',
                'username'  => 'trainee@sshia.org',
                'role'      => 'Trainees',
                'is_active' => true,
            ],
            [
                'name'      => 'Senior Mentor',
                'email'     => 'mentor@sshia.org',
                'username'  => 'mentor@sshia.org',
                'role'      => 'Mentor',
                'is_active' => true,
            ],
            [
                'name'      => 'Mentorship Mentee',
                'email'     => 'mentee@sshia.org',
                'username'  => 'mentee@sshia.org',
                'role'      => 'Mentee',
                'is_active' => true,
            ],
            [
                'name'      => 'Health Partner',
                'email'     => 'partner@sshia.org',
                'username'  => 'partner@sshia.org',
                'role'      => 'Partners',
                'is_active' => true,
            ],
            [
                'name'      => 'Research Contributor',
                'email'     => 'contributor@sshia.org',
                'username'  => 'contributor@sshia.org',
                'role'      => 'Contributor',
                'is_active' => true,
            ],
            [
                'name'      => 'Health Marketer',
                'email'     => 'healthmarketor@sshia.org',
                'username'  => 'healthmarketor@sshia.org',
                'role'      => 'Health Marketor',
                'is_active' => true,
            ],
        ];

        foreach ($users as $userData) {
            // Idempotency: skip if user already exists
            if (User::where('email', $userData['email'])->exists()) {
                $this->command->info("Skipping existing user: {$userData['email']}");
                continue;
            }

            // Verify the role exists before creating the user
            $role = Role::where('name', $userData['role'])->first();
            if (! $role) {
                $this->command->error("Role '{$userData['role']}' not found. Run RolePermissionSeeder first.");
                continue;
            }

            $user = User::create([
                'name'      => $userData['name'],
                'email'     => $userData['email'],
                'username'  => $userData['username'],
                'password'  => Hash::make(self::DEFAULT_PASSWORD),
                'is_active' => $userData['is_active'],
            ]);

            $user->assignRole($userData['role']);

            $this->command->info("Created user: {$userData['email']} [{$userData['role']}]");
        }

        $this->command->info('');
        $this->command->info('UserSeeder completed. Default password: ' . self::DEFAULT_PASSWORD);
        $this->command->warn('IMPORTANT: Change all passwords immediately after first login in production.');
    }
}
