<?php

namespace App\Database\Seeds;

use CodeIgniter\Database\Seeder;
use CodeIgniter\Shield\Entities\User;

class AdminSeeder extends Seeder
{
    public function run(): void
    {
        $users = auth()->getProvider();

        // Super Admin
        $superAdmin = new User([
            'username' => 'superadmin',
            'email'    => 'superadmin@sttelshadday.ac.id',
            'password' => 'Admin@12345',
            'active'   => true,
        ]);

        $users->save($superAdmin);
        $superAdmin = $users->findById($users->getInsertID());
        $superAdmin->addGroup('super_admin');

        // Create user_profiles entry for super admin
        $this->db->table('user_profiles')->insert([
            'user_id'         => $superAdmin->id,
            'name'            => 'Super Administrator',
            'identity_number' => null,
            'role'            => 'super_admin',
            'status'          => 'active',
            'created_at'      => date('Y-m-d H:i:s'),
            'updated_at'      => date('Y-m-d H:i:s'),
        ]);

        // Admin
        $admin = new User([
            'username' => 'admin',
            'email'    => 'admin@sttelshadday.ac.id',
            'password' => 'Admin@12345',
            'active'   => true,
        ]);

        $users->save($admin);
        $admin = $users->findById($users->getInsertID());
        $admin->addGroup('admin');

        $this->db->table('user_profiles')->insert([
            'user_id'         => $admin->id,
            'name'            => 'Administrator',
            'identity_number' => null,
            'role'            => 'admin',
            'status'          => 'active',
            'created_at'      => date('Y-m-d H:i:s'),
            'updated_at'      => date('Y-m-d H:i:s'),
        ]);

        echo "Super Admin created: superadmin@sttelshadday.ac.id / Admin@12345\n";
        echo "Admin created:       admin@sttelshadday.ac.id / Admin@12345\n";
        echo "IMPORTANT: Change passwords immediately after first login!\n";
    }
}
