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

class Pegawai_model extends CI_Model {

    // === PAGINATION ===
    public function get_all_paginated($search = null, $limit = 25, $start = 0) {
        if (!empty($search)) {
            $this->db->group_start()
                ->like('nama', $search, 'both')
                ->or_like('nip', $search, 'both')
                ->or_like('jabatan', $search, 'both')
            ->group_end();
        }
        return $this->db->limit($limit, $start)
                        ->order_by('nama', 'ASC')
                        ->get('pegawai')
                        ->result();
    }

    public function get_by_unor_ids_paginated($unor_ids, $search = null, $limit = 25, $start = 0) {
        if (empty($unor_ids)) {
            return [];
        }
        $this->db->where_in('unor_id', $unor_ids);
        if (!empty($search)) {
            $this->db->group_start()
                ->like('nama', $search, 'both')
                ->or_like('nip', $search, 'both')
                ->or_like('jabatan', $search, 'both')
            ->group_end();
        }
        return $this->db->limit($limit, $start)
                        ->order_by('nama', 'ASC')
                        ->get('pegawai')
                        ->result();
    }

    // === COUNT ===
    public function count_by_unor_ids($unor_ids, $search = null) {
        if (empty($unor_ids)) {
            return 0;
        }
        $this->db->where_in('unor_id', $unor_ids);
        if (!empty($search)) {
            $this->db->group_start()
                ->like('nama', $search, 'both')
                ->or_like('nip', $search, 'both')
                ->or_like('jabatan', $search, 'both')
            ->group_end();
        }
        return $this->db->count_all_results('pegawai');
    }

    // === UPDATE ===
    public function update($id_peg, $data) {
        $this->db->where('id_peg', $id_peg);
        return $this->db->update('pegawai', $data);
    }

    // === FUNGSI LAINNYA ===
    public function get_all($search = null) {
        if (!empty($search)) {
            $this->db->group_start()
                ->like('nama', $search, 'both')
                ->or_like('nip', $search, 'both')
                ->or_like('jabatan', $search, 'both')
            ->group_end();
        }
        return $this->db->order_by('nama', 'ASC')->get('pegawai')->result();
    }

    public function get_by_unor_ids($unor_ids) {
        if (empty($unor_ids)) {
            return [];
        }
        $this->db->where_in('unor_id', $unor_ids);
        return $this->db->order_by('nama', 'ASC')->get('pegawai')->result();
    }

    public function count_all($search = null) {
        if (!empty($search)) {
            $this->db->group_start()
                ->like('nama', $search, 'both')
                ->or_like('nip', $search, 'both')
                ->or_like('jabatan', $search, 'both')
            ->group_end();
        }
        return $this->db->count_all_results('pegawai');
    }

    public function get_all_child_unor_ids($parent_id) {
        if (empty($parent_id)) {
            return [];
        }
        $ids = [$parent_id];
        $children = $this->db->select('unor_id')
                             ->where('unor_atasan_id', $parent_id)
                             ->get('unor')
                             ->result();
        foreach ($children as $child) {
            $child_ids = $this->get_all_child_unor_ids($child->unor_id);
            $ids = array_merge($ids, $child_ids);
        }
        return array_unique($ids);
    }

    public function get_unor_id_by_name($nama_unor) {
        if (empty($nama_unor)) {
            return null;
        }
        $this->db->select('unor_id');
        $this->db->from('unor');
        $this->db->where('nama', $nama_unor);
        $row = $this->db->get()->row();
        return $row ? $row->unor_id : null;
    }

    public function get_unor_list() {
        $this->db->select('unor_id, nama', FALSE);
        $this->db->from('unor');
        $this->db->where('nama IS NOT NULL');
        $this->db->distinct();
        $this->db->order_by('nama', 'ASC');
        return $this->db->get()->result();
    }

    public function get_by_id_peg($id_peg) {
        if (empty($id_peg)) {
            return null;
        }
        return $this->db->get_where('pegawai', ['id_peg' => $id_peg])->row();
    }

    public function get_by_nip($nip) {
        if (empty($nip)) {
            return null;
        }
        return $this->db->get_where('pegawai', ['nip' => $nip])->row();
    }
}