<?php

function pre($str) {
   echo "<pre>";
      print_r($str);
   echo "</pre>";
}

function get_company_name_without_space($id) {
	$CI = & get_instance();
	
	$result = $CI->db->query("select * from company where id='$id'")->result_array();
	
	return str_replace(' ', '_', $result[0]['company_name']);
}
function get_authority_name_without_space($id) {
	$CI = & get_instance();
	
	$result = $CI->db->query("select * from authority where id='$id'")->result_array();
	
	return str_replace(' ', '_', $result[0]['authority_name']);
}

function get_company_documents($id) {
	$CI = & get_instance();
	
	$result = $CI->db->query("SELECT  f.*,  a.authority_name,  t.type_name, t.frequency  FROM  company_files f LEFT JOIN  authority a ON a.id = f.authority_id::integer
							LEFT JOIN  sub_type t ON t.id = f.type_id::integer WHERE  f.company_id ='$id'")->result_array();
	
	return $result;
}
function get_all_company() {

    log_message('debug', 'get_all_company() called');
    $CI = &get_instance();
    $login_id = $CI->session->userdata('id');
    $role_id = $CI->session->userdata('role_id');

    // Super Admin (role 1) sees all companies
    if ($role_id == '1') {
        return $CI->db->select('*')
            ->from('company')
            ->where('status', '1')
            ->order_by('company_name', 'ASC')
            ->get()
            ->result_array();
    }
    
    // Admin (role 2) and User (role 3) see only assigned companies
    // First try the new user_companies normalized table
    $company_ids = $CI->db->select('company_id')
        ->from('user_companies')
        ->where('user_id', $login_id)
        ->get()
        ->result_array();
    
    if (!empty($company_ids)) {
        $ids = array_column($company_ids, 'company_id');
        return $CI->db->select('*')
            ->from('company')
            ->where('status', '1')
            ->where_in('id', $ids)
            ->order_by('company_name', 'ASC')
            ->get()
            ->result_array();
    }
    
    // Fallback: Check old users.user_company CSV field for backward compatibility
    $user = $CI->db->select('user_company')
        ->from('users')
        ->where('id', $login_id)
        ->get()
        ->row();
    
    if ($user && !empty($user->user_company)) {
        $ids = array_filter(explode(',', $user->user_company));
        if (!empty($ids)) {
            return $CI->db->select('*')
                ->from('company')
                ->where('status', '1')
                ->where_in('id', $ids)
                ->order_by('company_name', 'ASC')
                ->get()
                ->result_array();
        }
    }
    
    return []; // No companies assigned

}
function get_all_user() {
	$CI = &get_instance();
	$CI->db->select('u.id,u.first_name, u.last_name')
           ->from('users u')
           ->where('u.role_id <>', '1');
    $result = $CI->db->get()->result_array();
	return $result;
}

function get_company_name($id) {
    $CI = & get_instance();
    $result = $CI->db->get_where('company', ['id' => $id])->row();
    return $result ? $result->company_name : '';
}
?>
