<?php

namespace App\Database\Seeds;

use CodeIgniter\Database\Seeder;

class AdminSeeder extends Seeder
{
    public function run()
    {
        $data = [
            'full_name'     => 'Administrador',
            'email'         => env('AUTH_ADMIN_EMAIL', 'admin@simprofire.local'),
            'password_hash' => password_hash((string) env('AUTH_ADMIN_PASSWORD', 'Admin12345'), PASSWORD_DEFAULT),
            'is_active'     => 1,
            'created_at'    => date('Y-m-d H:i:s'),
            'updated_at'    => date('Y-m-d H:i:s'),
        ];

        $existing = $this->db->table('users')->where('email', $data['email'])->get()->getFirstRow('array');

        if ($existing !== null) {
            $this->db->table('users')->where('id', $existing['id'])->update($data);

            return;
        }

        $this->db->table('users')->insert($data);
    }
}
