<?php

namespace Database\Seeders;

use App\Models\User;
use Illuminate\Database\Seeder;

class UserSeeder extends Seeder
{
    public function run(): void
    {
        $divisions = \App\Models\Division::all();

        // super_admin (tanpa divisi)
        User::factory()->createOne([
            'name'     => 'Super Admin',
            'email'    => 'admin@admin.com',
            'password' => bcrypt('admin'),
            'phone'    => '08123456789',
            'status'   => 'aktif',
        ])->assignRole('super_admin');

        // ketua, bendahara, auditor — tanpa divisi spesifik
        foreach (['ketua', 'bendahara', 'auditor'] as $role) {
            User::factory(2)->create()->each(fn ($u) => $u->assignRole($role));
        }

        // 1 editor
        User::factory()->createOne([
            'name'     => 'Editor Konten',
            'email'    => 'editor@persegi.test',
            'password' => bcrypt('password'),
            'status'   => 'aktif',
        ])->assignRole('editor');

        // Setiap divisi: 1 pengurus (jadi leader) + 3–8 anggota
        foreach ($divisions as $division) {
            $pengurus = User::factory()->create(['division_id' => $division->id]);
            $pengurus->assignRole('pengurus');

            $division->update(['leader_id' => $pengurus->id]);

            $count = rand(3, 8);
            User::factory($count)->create(['division_id' => $division->id])
                ->each(fn ($u) => $u->assignRole('anggota'));
        }
    }
}
