<?php

namespace App\Database\Seeds;

use CodeIgniter\Database\Seeder;

class AuthSeeder extends Seeder
{
    public function run()
    {
        // Roles
        $rolesData = [
            [
                'name' => 'SUPER ADMIN',
                'slug' => 'superadmin',
                'description' => 'Akses penuh ke sistem'
            ],
            [
                'name' => 'ADMINISTRATOR',
                'slug' => 'admin',
                'description' => 'Kelola operasional sistem'
            ],
            [
                'name' => 'VERIFIKATOR',
                'slug' => 'verifikator',
                'description' => 'Verifikasi pengajuan pangkat'
            ],
            [
                'name' => 'USER (ASN)',
                'slug' => 'user',
                'description' => 'Akses untuk user ASN'
            ],
        ];

        foreach ($rolesData as $role) {
            $this->db->table('roles')->insert($role);
        }

        // Default Admin User
        $userData = [
            'username'   => 'admin',
            'name'       => 'Super Administrator',
            'email'      => 'admin@simkp.test',
            'password'   => password_hash('admin123', PASSWORD_DEFAULT),
            'is_active'  => 1,
            'created_at' => date('Y-m-d H:i:s'),
        ];
        $this->db->table('users')->insert($userData);
        $userId = $this->db->insertID();

        // Assign Super Admin Role
        $roleId = $this->db->table('roles')->where('slug', 'superadmin')->get()->getRow()->id;
        $this->db->table('user_roles')->insert([
            'user_id' => $userId,
            'role_id' => $roleId
        ]);

        // Default Settings
        $settings = [
            ['key' => 'app_name', 'value' => 'SIMKP ASN', 'group' => 'general'],
            ['key' => 'wa_api_url', 'value' => 'https://api.fonnte.com', 'group' => 'whatsapp'],
            ['key' => 'wa_api_key', 'value' => '', 'group' => 'whatsapp'],
            ['key' => 'wa_device_id', 'value' => '', 'group' => 'whatsapp'],
        ];

        foreach ($settings as $setting) {
            $this->db->table('settings')->insert($setting);
        }
    }
}
