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

/**
 * RBAC helpers for report access (view / export).
 */

if (!function_exists('rbac_is_superadmin')) {
    function rbac_is_superadmin()
    {
        $CI =& get_instance();
        if ($CI->session->userdata('is_superadmin')) {
            return true;
        }
        // Legacy Admin usertype remains full access until migrated
        return $CI->session->userdata('usertype') === 'Admin'
            && !$CI->session->userdata('role_id');
    }
}

if (!function_exists('rbac_can')) {
    /**
     * @param string $module_key
     * @param string $action view|export
     */
    function rbac_can($module_key, $action = 'view')
    {
        if (rbac_is_superadmin()) {
            return true;
        }

        $CI =& get_instance();
        $perms = $CI->session->userdata('permissions');
        if (!is_array($perms) || !isset($perms[$module_key])) {
            return false;
        }

        $row = $perms[$module_key];
        if ($action === 'export') {
            return !empty($row['can_export']);
        }
        return !empty($row['can_view']);
    }
}

if (!function_exists('rbac_can_view')) {
    function rbac_can_view($module_key)
    {
        return rbac_can($module_key, 'view');
    }
}

if (!function_exists('rbac_can_export')) {
    function rbac_can_export($module_key)
    {
        return rbac_can($module_key, 'export');
    }
}

if (!function_exists('rbac_first_allowed_report')) {
    function rbac_first_allowed_report()
    {
        $order = array(
            'overdue_report_details',
            'issue_report',
            'current_checkout',
            'return_report',
            'circulation_report',
            'reservation_report',
            'due_clearance',
            'accession_register',
            'search_catalogue_report',
            'new_arrivals',
            'subject_wise_report',
            'book_report',
            'patent_report',
            'institutional_repository',
            'standards_data',
            'ereport_books',
            'application_notes_report',
            'reports_book_report',
            'journals_book_report',
            'ebook_report',
            'top_books_report',
            'single_book_report',
            'overall_material_statistics_report',
            'publisher_year_wise_report',
            'accession_year_wise_report',
            'accession_date_wise_report',
            'barcode_range_report',
            'price_range_report',
            'weed_out_report',
            'publisher_person_wise_report',
            'top_users_report',
            'single_user_report',
            'user_report',
            'search_user_report',
            'so_report',
            'crv_report',
            'lac_new_titles_report',
            'budget_expenditure_report',
        );

        if (rbac_is_superadmin()) {
            return 'report/overdue_report_details';
        }

        foreach ($order as $key) {
            if (rbac_can_view($key)) {
                return 'report/' . $key;
            }
        }

        if (rbac_can_view('manage_users')) {
            return 'users';
        }
        if (rbac_can_view('manage_roles')) {
            return 'roles';
        }
        if (rbac_can_view('manage_permissions')) {
            return 'permissions';
        }
        if (rbac_can_view('manage_font_settings')) {
            return 'settings';
        }

        return null;
    }
}
