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

class User_model extends CI_Model {

    public function __construct() {
        parent::__construct();
    }

    /**
     * Get user by username
     */
    public function get_by_username($username) {
        return $this->db->get_where('users', ['username' => $username])->row();
    }

    /**
     * Get user by email
     */
    public function get_by_email($email) {
        return $this->db->get_where('users', ['email' => $email])->row();
    }

    /**
     * Get user by ID
     */
    public function get_by_id($id) {
        return $this->db->get_where('users', ['id' => $id])->row();
    }

    /**
     * Get all users with pagination
     */
    public function get_all_users($limit = 10, $offset = 0, $role = null, $status = null) {
        $this->db->select('id, username, email, role, status, created_at');
        
        if ($role) {
            $this->db->where('role', $role);
        }
        
        if ($status) {
            $this->db->where('status', $status);
        }
        
        $this->db->limit($limit, $offset);
        $this->db->order_by('created_at', 'DESC');
        
        return $this->db->get('users')->result();
    }

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

    /**
     * Create new user
     */
    public function create_user($data) {
        $insert_data = [
            'username' => $data['username'],
            'email' => $data['email'],
            'password' => password_hash($data['password'], PASSWORD_BCRYPT),
            'role' => $data['role'],
            'status' => isset($data['status']) ? $data['status'] : 'active'
        ];
        
        return $this->db->insert('users', $insert_data);
    }

    /**
     * Update user
     */
    public function update_user($id, $data) {
        $update_data = [];
        
        if (isset($data['username'])) {
            $update_data['username'] = $data['username'];
        }
        
        if (isset($data['email'])) {
            $update_data['email'] = $data['email'];
        }
        
        if (isset($data['password']) && !empty($data['password'])) {
            $update_data['password'] = password_hash($data['password'], PASSWORD_BCRYPT);
        }
        
        if (isset($data['role'])) {
            $update_data['role'] = $data['role'];
        }
        
        if (isset($data['status'])) {
            $update_data['status'] = $data['status'];
        }
        
        $this->db->where('id', $id);
        return $this->db->update('users', $update_data);
    }

    /**
     * Delete user
     */
    public function delete_user($id) {
        return $this->db->delete('users', ['id' => $id]);
    }

    /**
     * Toggle user status
     */
    public function toggle_status($id) {
        $user = $this->get_by_id($id);
        if ($user) {
            $new_status = ($user->status == 'active') ? 'inactive' : 'active';
            return $this->update_user($id, ['status' => $new_status]);
        }
        return false;
    }

    /**
     * Verify user password
     */
    public function verify_password($username, $password) {
        $user = $this->get_by_username($username);
        
        if ($user && password_verify($password, $user->password)) {
            return $user;
        }
        
        return false;
    }

    /**
     * Get users assigned to campaign
     */
    public function get_campaign_users($campaign_id) {
        $this->db->select('users.id, users.username, users.email');
        $this->db->from('users');
        $this->db->join('campaign_users', 'campaign_users.user_id = users.id');
        $this->db->where('campaign_users.campaign_id', $campaign_id);
        
        return $this->db->get()->result();
    }

    /**
     * Get unassigned users for a campaign
     */
    public function get_unassigned_users($campaign_id) {
        $this->db->select('id, username, email');
        $this->db->from('users');
        $this->db->where('role', 'user');
        $this->db->where('status', 'active');
        $this->db->where("id NOT IN (SELECT user_id FROM campaign_users WHERE campaign_id = $campaign_id)", NULL, FALSE);
        
        return $this->db->get()->result();
    }
}
