<?php

namespace App\Database\Seeds;

use CodeIgniter\Database\Seeder;

class UserSeeder extends Seeder
{
    public function run()
    {
        $this->createUserIfMissing([
            'name'     => 'Administrator',
            'email'    => 'admin@app.test',
            'password' => 'admin123',
            'role'     => 'administrator',
        ]);
    }

    private function createUserIfMissing(array $data): void
    {
        $user = $this->db->table('user')
            ->where('email', $data['email'])
            ->get()
            ->getRowArray();

        if ($user) {
            return;
        }

        $this->db->table('user')->insert([
            'name'       => $data['name'],
            'email'      => $data['email'],
            'password'   => password_hash($data['password'], PASSWORD_DEFAULT),
            'role'       => $data['role'],
            'is_active'  => 1,
            'created_at' => date('Y-m-d H:i:s'),
            'updated_at' => date('Y-m-d H:i:s'),
        ]);
    }
}
