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

class User_model extends CI_Model {

    public function __construct() {
        parent::__construct();
        $this->load->database();
    }

    /**
     * Get all users with pagination
     */
    public function get_users($limit = 100, $offset = 0) {
        $this->db->select('id, name, email, role, municipality, division, ward, status, created_at, last_login');
        $this->db->from('users');
        $this->db->where('status', 'active');
        $this->db->limit($limit, $offset);
        $this->db->order_by('created_at', 'DESC');
        $query = $this->db->get();
        return $query->result_array();
    }

    /**
     * Get user by ID
     */
    public function get_user_by_id($user_id) {
        $this->db->select('id, name, email, role, municipality, division, ward, status, created_at, last_login');
        $this->db->from('users');
        $this->db->where('id', $user_id);
        $query = $this->db->get();
        return $query->row_array();
    }

    /**
     * Get user by email
     */
    public function get_user_by_email($email) {
        $this->db->select('id, name, email, password, role, municipality, division, ward, status, last_login');
        $this->db->from('users');
        $this->db->where('email', $email);
        $query = $this->db->get();
        return $query->row_array();
    }

    /**
     * Create new user
     */
    public function create_user($data) {
        // Hash password before insertion
        if (isset($data['password'])) {
            $data['password'] = password_hash($data['password'], PASSWORD_BCRYPT);
        }
        
        return $this->db->insert('users', $data);
    }

    /**
     * Update user
     */
    public function update_user($user_id, $data) {
        // Hash password if it's being updated
        if (isset($data['password']) && !empty($data['password'])) {
            $data['password'] = password_hash($data['password'], PASSWORD_BCRYPT);
        } else {
            // Remove password field if empty
            unset($data['password']);
        }
        
        $data['updated_at'] = date('Y-m-d H:i:s');
        $this->db->where('id', $user_id);
        return $this->db->update('users', $data);
    }

    /**
     * Delete user (soft delete by setting status to inactive)
     */
    public function delete_user($user_id) {
        $data = array(
            'status' => 'inactive',
            'updated_at' => date('Y-m-d H:i:s')
        );
        $this->db->where('id', $user_id);
        return $this->db->update('users', $data);
    }

    /**
     * Permanently delete user
     */
    public function permanent_delete_user($user_id) {
        $this->db->where('id', $user_id);
        return $this->db->delete('users');
    }

    /**
     * Verify user credentials
     */
    public function verify_credentials($email, $password) {
        $user = $this->get_user_by_email($email);
        
        if ($user && password_verify($password, $user['password'])) {
            // Update last login
            $this->db->where('id', $user['id']);
            $this->db->update('users', array('last_login' => date('Y-m-d H:i:s')));
            
            // Remove password from returned data
            unset($user['password']);
            return $user;
        }
        
        return false;
    }

    /**
     * Count total users
     */
    public function count_users() {
        $this->db->where('status', 'active');
        return $this->db->count_all_results('users');
    }

    /**
     * Get users by role
     */
    public function get_users_by_role($role) {
        $this->db->select('id, name, email, role, municipality, division, ward, status, created_at, last_login');
        $this->db->from('users');
        $this->db->where('role', $role);
        $this->db->where('status', 'active');
        $this->db->order_by('created_at', 'DESC');
        $query = $this->db->get();
        return $query->result_array();
    }
}
