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

class Tamu_model extends CI_Model {

    public function get_all($limit = null, $offset = 0) {
        $this->db->select('tamu.*, u.nama_lengkap as dicatat_oleh_nama');
        $this->db->from('tamu');
        $this->db->join('users u', 'u.id = tamu.dicatat_oleh', 'left');
        $this->db->order_by('tamu.checkin_at', 'DESC');
        if ($limit) { $this->db->limit($limit, $offset); }
        return $this->db->get()->result();
    }

    public function get_today() {
        $this->db->select('tamu.*');
        $this->db->from('tamu');
        $this->db->where('DATE(tamu.checkin_at)', app_now('Y-m-d'));
        $this->db->order_by('tamu.checkin_at', 'DESC');
        return $this->db->get()->result();
    }

    public function get_by_id($id) {
        $this->db->select('tamu.*, u.nama_lengkap as dicatat_oleh_nama');
        $this->db->from('tamu');
        $this->db->join('users u', 'u.id = tamu.dicatat_oleh', 'left');
        $this->db->where('tamu.id', $id);
        return $this->db->get()->row();
    }

    public function get_masih_checkin() {
        $this->db->where('status', 'checkin');
        $this->db->order_by('checkin_at', 'ASC');
        return $this->db->get('tamu')->result();
    }

    public function get_menginap_aktif() {
        $this->db->where('status', 'checkin');
        $this->db->where('tipe_kunjungan', 'menginap');
        $this->db->order_by('checkin_at', 'ASC');
        return $this->db->get('tamu')->result();
    }

    public function count_today() {
        $this->db->where('DATE(checkin_at)', app_now('Y-m-d'));
        return $this->db->count_all_results('tamu');
    }

    public function count_checkin_sekarang() {
        $this->db->where('status', 'checkin');
        return $this->db->count_all_results('tamu');
    }

    public function count_menginap() {
        $this->db->where('status', 'checkin');
        $this->db->where('tipe_kunjungan', 'menginap');
        return $this->db->count_all_results('tamu');
    }

    public function get_filter($params = []) {
        $this->db->select('tamu.*, u.nama_lengkap as dicatat_oleh_nama');
        $this->db->from('tamu');
        $this->db->join('users u', 'u.id = tamu.dicatat_oleh', 'left');

        if (!empty($params['status']))  { $this->db->where('tamu.status', $params['status']); }
        if (!empty($params['tipe']))    { $this->db->where('tamu.tipe_kunjungan', $params['tipe']); }
        if (!empty($params['tanggal'])) { $this->db->where('DATE(tamu.checkin_at)', $params['tanggal']); }
        if (!empty($params['keyword'])) {
            $kw = $params['keyword'];
            $this->db->group_start();
                $this->db->like('tamu.nama_tamu', $kw);
                $this->db->or_like('tamu.tujuan_blok', $kw);
                $this->db->or_like('tamu.nama_tuan_rumah', $kw);
                $this->db->or_like('tamu.no_kendaraan', $kw);
                $this->db->or_like('tamu.kode_tamu', $kw);
            $this->db->group_end();
        }

        $this->db->order_by('tamu.checkin_at', 'DESC');
        return $this->db->get()->result();
    }

    public function insert($data) {
        $kode = 'TM-' . app_now('Ymd') . '-' . strtoupper(substr(uniqid(), -4));
        $data['kode_tamu']  = $kode;
        $data['created_at'] = app_now();
        $data['updated_at'] = app_now();
        $this->db->insert('tamu', $data);
        return $this->db->insert_id();
    }

    public function checkout($id) {
        $now = app_now();
        $this->db->where('id', $id);
        return $this->db->update('tamu', [
            'status'      => 'checkout',
            'checkout_at' => $now,
            'updated_at'  => $now,
        ]);
    }

    public function update($id, $data) {
        $data['updated_at'] = app_now();
        $this->db->where('id', $id);
        return $this->db->update('tamu', $data);
    }

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

    public function get_statistik() {
        return [
            'total_hari_ini' => $this->count_today(),
            'sedang_dalam'   => $this->count_checkin_sekarang(),
            'menginap_aktif' => $this->count_menginap(),
            'total_semua'    => $this->db->count_all('tamu'),
        ];
    }
}