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

class Attendance_model extends CI_Model {

    public function get_all($limit = 100, $offset = 0) {
        return $this->db
            ->order_by('timestamp', 'DESC')
            ->get('attendance_logs', $limit, $offset)
            ->result();
    }

    // public function get_by_user($user_id) {
    //     return $this->db
    //         ->where('user_id', $user_id)
    //         ->order_by('timestamp', 'DESC')
    //         ->get('attendance_logs')
    //         ->result();
    // }

    // ✅ distinct users list
    public function get_users() {
        $this->db->distinct();
        $this->db->select('user_id');
        $this->db->order_by('user_id','ASC');
        return $this->db->get('attendance_logs')->result();
    }

    // ✅ count distinct users
    public function count_users() {
        $this->db->distinct();
        $this->db->select('user_id');
        $query = $this->db->get('attendance_logs');
        return $query->num_rows();
    }


        public function get_all_with_user($limit = 100, $offset = 0) {
        $this->db->select('a.*, m.name as member_name, m.fingerprint_id, m.card_no');
        $this->db->from('attendance_logs a');
        $this->db->join('members m', 'm.id = a.user_id', 'left');
        $this->db->order_by('a.timestamp', 'DESC');
        $this->db->limit($limit, $offset);
        return $this->db->get()->result();
    }

    public function get_by_user($user_id) {
        $this->db->select('a.*, m.name as member_name, m.fingerprint_id, m.card_no');
        $this->db->from('attendance_logs a');
        $this->db->join('members m', 'm.id = a.user_id', 'left');
        $this->db->where('a.user_id', $user_id);
        $this->db->order_by('a.timestamp', 'DESC');
        return $this->db->get()->result();
    }

    
}
