<?php
defined('BASEPATH') or exit('No direct script access allowed');

class Petugas_model extends CI_Model
{

	public function get_petugas_aktif($search = '')
	{
		$this->db->select('petugas_id, nip, nama_lengkap, jabatan.nama_jabatan');
		$this->db->from('tb_petugas');
		$this->db->join('tb_jabatan as jabatan', 'tb_petugas.id_jabatan = jabatan.jabatan_id', 'left');
		$this->db->where('tb_petugas.status_aktif', 1);

		if (!empty($search)) {
			$this->db->group_start();
			$this->db->like('tb_petugas.nama_lengkap', $search);
			$this->db->or_like('tb_petugas.nip', $search);
			$this->db->or_like('jabatan.nama_jabatan', $search);
			$this->db->group_end();
		}

		$this->db->order_by('tb_petugas.nama_lengkap', 'ASC');
		return $this->db->get()->result();
	}

	private $t_petugas = 'tb_petugas';
	private $t_user    = 'tb_user';
	private $t_jabatan = 'tb_jabatan';
	private $t_role    = 'tb_hak_akses';

	// ===== LIST =====
	public function get_all()
	{
		// join jabatan pakai KODE (p.id_jabatan = j.kode_jabatan)
		$this->db->select('p.*, j.nama_jabatan, u.username, r.nama_hak_akses, u.id_hak_akses, u.user_id');
		$this->db->from($this->t_petugas . ' p');
		$this->db->join($this->t_jabatan . ' j', 'p.id_jabatan = j.kode_jabatan', 'left');
		$this->db->join($this->t_user . ' u', 'p.id_user = u.user_id', 'left');
		$this->db->join($this->t_role . ' r', 'u.id_hak_akses = r.hak_akses_id', 'left');
		$this->db->order_by('p.created_at', 'DESC');
		return $this->db->get()->result();
	}

	// ===== REF DATA =====
	public function get_jabatan_aktif()
	{
		// pakai semua jabatan aktif
		$this->db->where('status_aktif', 1);
		return $this->db->get($this->t_jabatan)->result();
	}

	public function get_hak_akses_aktif()
	{
		$this->db->where('status_aktif', 1);
		return $this->db->get($this->t_role)->result();
	}

	public function get_users_aktif()
	{
		$this->db->where('status_aktif', 1);
		return $this->db->get($this->t_user)->result();
	}

	// ===== PETUGAS CRUD =====
	public function insert_petugas($data)
	{
		$data['petugas_id'] = $this->gen_uuid();
		return $this->db->insert($this->t_petugas, $data);
	}

	public function update_petugas($id, $data)
	{
		$this->db->where('petugas_id', $id);
		return $this->db->update($this->t_petugas, $data);
	}

	public function delete_petugas($id)
	{
		$this->db->where('petugas_id', $id);
		return $this->db->delete($this->t_petugas);
	}

	public function find($id)
	{
		$this->db->where('petugas_id', $id);
		return $this->db->get($this->t_petugas)->row();
	}

	// ===== USER MGMT =====
	public function is_username_exist($username)
	{
		$this->db->where('username', $username);
		return $this->db->get($this->t_user)->num_rows() > 0;
	}

	public function create_user($payload)
	{
		// payload: username, password_md5, nama_lengkap, email, id_hak_akses, no_telp
		$data = [
			'user_id'       => $this->gen_user_id(),
			'username'      => $payload['username'],
			'password'      => $payload['password_md5'], // MD5 sesuai request
			'nama_lengkap'  => $payload['nama_lengkap'],
			'email'         => $payload['email'],
			'no_telp'       => isset($payload['no_telp']) ? $payload['no_telp'] : null,
			'id_hak_akses'  => $payload['id_hak_akses'],
			'status_aktif'  => 1
		];
		$this->db->insert($this->t_user, $data);
		return $data['user_id'];
	}

	// ===== HELPERS =====
	private function gen_uuid()
	{
		// simple UUID v4-ish
		return sprintf(
			'%04x%04x-%04x-%04x-%04x-%04x%04x%04x',
			mt_rand(0, 0xffff),
			mt_rand(0, 0xffff),
			mt_rand(0, 0xffff),
			mt_rand(0, 0x0fff) | 0x4000,
			mt_rand(0, 0x3fff) | 0x8000,
			mt_rand(0, 0xffff),
			mt_rand(0, 0xffff),
			mt_rand(0, 0xffff)
		);
	}

	private function gen_user_id()
	{
		return 'u-' . substr(md5(uniqid('', true)), 0, 8);
	}
}
