<?php

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

/**
 * @since  2.3.3
 * Get available staff permissions, modules can use the filter too to hook permissions
 * @param  array  $data additional data passed from view role.php and member.php
 * @return array
 */
function get_available_staff_permissions($data = [])
{
    $viewGlobalName = _l('permission_view') . '(' . _l('permission_global') . ')';

    $allPermissionsArray = [
        'view_own' => _l('permission_view_own'),
        'view'     => $viewGlobalName,
        'create'   => _l('permission_create'),
        'edit'     => _l('permission_edit'),
        'delete'   => _l('permission_delete'),
    ];

    $withoutViewOwnPermissionsArray = [
        'view'   => $viewGlobalName,
        'create' => _l('permission_create'),
        'edit'   => _l('permission_edit'),
        'delete' => _l('permission_delete'),
    ];

    $withNotApplicableViewOwn = array_merge(['view_own' => ['not_applicable' => true, 'name' => _l('permission_view_own')]], $withoutViewOwnPermissionsArray);

    $corePermissions = [
        'bulk_pdf_exporter' => [
            'name'         => _l('bulk_pdf_exporter'),
            'capabilities' => [
                'view' => $viewGlobalName,
            ],
        ],
        'contracts' => [
            'name'         => _l('contracts'),
            'capabilities' => array_merge($allPermissionsArray, [
                'view_all_templates' => _l('permission_view_all_templates'),
            ]),
        ],
        'credit_notes' => [
            'name'         => _l('credit_notes'),
            'capabilities' => $allPermissionsArray,
        ],
        'customers' => [
            'name'         => _l('clients'),
            'capabilities' => $withNotApplicableViewOwn,
            'help'         => [
                'view_own' => _l('permission_customers_based_on_admins'),
            ],
        ],
        //  'staff_declaration' => [
        //     'name'         => _l('staff_declaration'),
        //     'capabilities' => $allPermissionsArray,
        // ],

      'tickets' => [
    'name' => _l('support_tickets'),
    'capabilities' => [
        'view'     => _l('permission_view_global'),
        'view_own' => _l('permission_view_own'),
        'create'   => _l('permission_create'),
        'edit'     => _l('permission_edit_global'),
        'edit_own' => _l('permission_edit_own'),
    ],
],





        'email_templates' => [
            'name'         => _l('email_templates'),
            'capabilities' => [
                'view' => $viewGlobalName,
                'edit' => _l('permission_edit'),
            ],
        ],
        'estimates' => [
            'name'         => _l('estimates'),
            'capabilities' => $allPermissionsArray,
        ],
        'expenses' => [
            'name'         => _l('expenses'),
            'capabilities' => $allPermissionsArray,
        ],
        'invoices' => [
            'name'         => _l('invoices'),
            'capabilities' => $allPermissionsArray,
        ],
        'items' => [
            'name'         => _l('items'),
            'capabilities' => $withoutViewOwnPermissionsArray,
        ],
        'knowledge_base' => [
            'name'         => _l('knowledge_base'),
            'capabilities' => $withoutViewOwnPermissionsArray,
        ],
        'payments' => [
            'name'         => _l('payments'),
            'capabilities' => $withNotApplicableViewOwn,
            'help'         => [
                'view_own' => _l('permission_payments_based_on_invoices'),
            ],
        ],
        'projects' => [
            'name'         => _l('projects'),
            'capabilities' => array_merge($withNotApplicableViewOwn, [ 'create_milestones' => _l('permission_create_timesheets'),
                'edit_milestones'                                                          => _l('permission_edit_milestones'), 'delete_milestones' => _l('permission_delete_milestones'), ]),
            'help' => [
                'view'     => _l('help_project_permissions'),
                'view_own' => _l('permission_projects_based_on_assignee'),
            ],
        ],
        'proposals' => [
            'name'         => _l('proposals'),
            'capabilities' => array_merge($allPermissionsArray, [
                'view_all_templates' => _l('permission_view_all_templates'),
            ]),
        ],
        'reports' => [
            'name'         => _l('reports'),
            'capabilities' => [
                'view'            => $viewGlobalName,
                'view-timesheets' => _l('permission_view_timesheet_report'),
            ],
        ],
        'roles' => [
            'name'         => _l('roles'),
            'capabilities' => $withoutViewOwnPermissionsArray,
        ],
        'settings' => [
            'name'         => _l('settings'),
            'capabilities' => [
                'view' => $viewGlobalName,
                'edit' => _l('permission_edit'),
            ],
        ],
        'staff' => [
            'name'         => _l('staff'),
            'capabilities' => $withoutViewOwnPermissionsArray,
        ],
        'subscriptions' => [
            'name'         => _l('subscriptions'),
            'capabilities' => $allPermissionsArray,
        ],
        'tasks' => [
            'name'         => _l('tasks'),
            'capabilities' => array_merge($withNotApplicableViewOwn, [
                'edit_timesheet'       => _l('permission_edit_timesheets'),
                'edit_own_timesheet'   => _l('permission_edit_own_timesheets'),
                'delete_timesheet'     => _l('permission_delete_timesheets'),
                'delete_own_timesheet' => _l('permission_delete_own_timesheets'),
            ]),
             'help' => [
                'view'     => _l('help_tasks_permissions'),
                'view_own' => _l('permission_tasks_based_on_assignee'),
            ],
        ],
        'checklist_templates' => [
            'name'         => _l('checklist_templates'),
            'capabilities' => [
                'create' => _l('permission_create'),
                'delete' => _l('permission_delete'),
            ],
        ],
        'estimate_request' => [
            'name'         => _l('estimate_request'),
            'capabilities' => $allPermissionsArray,
        ],
    ];

    $addLeadsPermission = true;
    if (isset($data['staff_id']) && $data['staff_id']) {
        $is_staff_member = is_staff_member($data['staff_id']);
        if (!$is_staff_member) {
            $addLeadsPermission = false;
        }
    }

    if ($addLeadsPermission) {
        $corePermissions['leads'] = [
            'name'         => _l('leads'),
            'capabilities' => [
                'view'   => $viewGlobalName,
                'delete' => _l('permission_delete'),
            ],
            'help' => [
                'view' => _l('help_leads_permission_view'),
            ],
        ];
    }

    return hooks()->apply_filters('staff_permissions', $corePermissions, $data);
}
/**
 * Get staff by ID or current logged in staff
 * @param  mixed $id staff id
 * @return mixed
 */
function get_staff($id = null)
{
    if (empty($id) && isset($GLOBALS['current_user'])) {
        return $GLOBALS['current_user'];
    }

    // Staff not logged in
    if (empty($id)) {
        return null;
    }

    if (!class_exists('staff_model', false)) {
        get_instance()->load->model('staff_model');
    }

    return get_instance()->staff_model->get($id);
}

/**
 * Return staff profile image url
 * @param  mixed $staff_id
 * @param  string $type
 * @return string
 */
function staff_profile_image_url($staff_id, $type = 'small')
{
    $url = base_url('assets/images/user-placeholder.jpg');

    if ((string) $staff_id === (string) get_staff_user_id() && isset($GLOBALS['current_user'])) {
        $staff = $GLOBALS['current_user'];
    } else {
        $CI = & get_instance();
        $CI->db->select('profile_image')
        ->where('staffid', $staff_id);

        $staff = $CI->db->get(db_prefix() . 'staff')->row();
    }

    if ($staff) {
        if (!empty($staff->profile_image)) {
            $profileImagePath = 'uploads/staff_profile_images/' . $staff_id . '/' . $type . '_' . $staff->profile_image;
            if (file_exists($profileImagePath)) {
                $url = base_url($profileImagePath);
            }
        }
    }

    return $url;
}

/**
 * Staff profile image with href
 * @param  boolean $id        staff id
 * @param  array   $classes   image classes
 * @param  string  $type
 * @param  array   $img_attrs additional <img /> attributes
 * @return string
 */
function staff_profile_image($id, $classes = ['staff-profile-image'], $type = 'small', $img_attrs = [])
{
    $url = base_url('assets/images/user-placeholder.jpg');

    $id = trim($id);

    $_attributes = '';
    foreach ($img_attrs as $key => $val) {
        $_attributes .= $key . '=' . '"' . e($val) . '" ';
    }

    $blankImageFormatted = '<img src="' . $url . '" ' . $_attributes . ' class="' . implode(' ', $classes) . '" />';

    if ((string) $id === (string) get_staff_user_id() && isset($GLOBALS['current_user'])) {
        $result = $GLOBALS['current_user'];
    } else {
        $CI     = & get_instance();
        $result = $CI->app_object_cache->get('staff-profile-image-data-' . $id);

        if (!$result) {
            $CI->db->select('profile_image,firstname,lastname');
            $CI->db->where('staffid', $id);
            $result = $CI->db->get(db_prefix() . 'staff')->row();
            $CI->app_object_cache->add('staff-profile-image-data-' . $id, $result);
        }
    }

    if (!$result) {
        return $blankImageFormatted;
    }

    if ($result && $result->profile_image !== null) {
        $profileImagePath = 'uploads/staff_profile_images/' . $id . '/' . $type . '_' . $result->profile_image;
        if (file_exists($profileImagePath)) {
            $profile_image = '<img ' . $_attributes . ' src="' . base_url($profileImagePath) . '" class="' . implode(' ', $classes) . '" />';
        } else {
            return $blankImageFormatted;
        }
    } else {
        $profile_image = '<img src="' . $url . '" ' . $_attributes . ' class="' . implode(' ', $classes) . '" />';
    }

    return $profile_image;
}

/**
 * Get staff full name
 * @param  string $userid Optional
 * @return string Firstname and Lastname
 */
function get_staff_full_name($userid = '')
{
    $tmpStaffUserId = get_staff_user_id();
    if ($userid == '' || $userid == $tmpStaffUserId) {
        if (isset($GLOBALS['current_user'])) {
            return $GLOBALS['current_user']->firstname . ' ' . $GLOBALS['current_user']->lastname;
        }
        $userid = $tmpStaffUserId;
    }

    $CI = & get_instance();

    $staff = $CI->app_object_cache->get('staff-full-name-data-' . $userid);

    if (!$staff) {
        $CI->db->where('staffid', $userid);
        $staff = $CI->db->select('firstname,lastname')->from(db_prefix() . 'staff')->get()->row();
        $CI->app_object_cache->add('staff-full-name-data-' . $userid, $staff);
    }

    return $staff ? $staff->firstname . ' ' . $staff->lastname : '';
}
if (!function_exists('get_staff_inceif_staff_id')) {
    /**
     * Return INCEIF staff number (tblstaff.inceif_staff_id) for a staff user.
     * If $userid is empty, uses current logged in staff.
     */
    function get_staff_inceif_staff_id($userid = '')
    {
        $tmpStaffUserId = get_staff_user_id();

        if ($userid == '' || $userid == $tmpStaffUserId) {
            // if current user already loaded, try use it (optional)
            if (isset($GLOBALS['current_user']) && isset($GLOBALS['current_user']->inceif_staff_id)) {
                return (string) $GLOBALS['current_user']->inceif_staff_id;
            }
            $userid = $tmpStaffUserId;
        }

        $CI = &get_instance();

        // cache key
        $cacheKey = 'staff-inceif-staff-id-' . (int)$userid;

        $row = $CI->app_object_cache->get($cacheKey);

        if (!$row) {
            $CI->db->where('staffid', (int)$userid);
            $row = $CI->db->select('inceif_staff_id')
                ->from(db_prefix() . 'staff')
                ->get()
                ->row();

            $CI->app_object_cache->add($cacheKey, $row);
        }

        return $row ? (string) $row->inceif_staff_id : '';
    }
}

if (!function_exists('get_staff_position')) {
    /**
     * Return staff position (tblstaff.position) for a staff user.
     * If $userid is empty, uses current logged in staff.
     */
    function get_staff_position($userid = '')
    {
        $tmpStaffUserId = get_staff_user_id();

        if ($userid == '' || $userid == $tmpStaffUserId) {
            // If current user already loaded, use it (if available)
            if (isset($GLOBALS['current_user']) && isset($GLOBALS['current_user']->position)) {
                return (string) $GLOBALS['current_user']->position;
            }
            $userid = $tmpStaffUserId;
        }

        $CI = &get_instance();

        // cache key
        $cacheKey = 'staff-position-' . (int) $userid;

        $row = $CI->app_object_cache->get($cacheKey);

        if (!$row) {
            $CI->db->where('staffid', (int) $userid);
            $row = $CI->db->select('position')
                ->from(db_prefix() . 'staff')
                ->get()
                ->row();

            $CI->app_object_cache->add($cacheKey, $row);
        }

        return $row ? (string) $row->position : '';
    }
}
if (!function_exists('ip_is_hod')) {
    function ip_is_hod($hod_staff_id = '')
    {
        $hod_staff_id = $hod_staff_id ?: get_staff_user_id();

        $CI = &get_instance();
        // A HOD is anyone that has at least 1 staff assigned to them
        return (bool) $CI->db->where('hod', (int)$hod_staff_id)
            ->where('active', 1)
            ->count_all_results(db_prefix().'staff');
    }
}

if (!function_exists('ip_can_view_declaration')) {
    function ip_can_view_declaration($decl)
    {
        $me = (int) get_staff_user_id();

        // ✅ Allow admins
        if (is_admin()) return true;

        // ✅ Allow global module view permission
        if (staff_can('view', 'integrity_pact')) return true;

        // $decl can be array/object
        $staff_id = (int) (is_array($decl) ? ($decl['staff_id'] ?? 0) : ($decl->staff_id ?? 0));
        if ($staff_id === $me) return true;

        // ✅ Allow HOD witness assigned in the declaration (your module uses hod_id)
        $hod_id = (int) (is_array($decl) ? ($decl['hod_id'] ?? 0) : ($decl->hod_id ?? 0));
        if ($hod_id === $me) return true;

        // ✅ Allow HOD based on staff table relationship (tblstaff.hod)
        $CI = &get_instance();
        $isSubordinate = (bool) $CI->db->where('staffid', $staff_id)
            ->where('hod', $me)
            ->count_all_results(db_prefix().'staff');

        return $isSubordinate;
    }
}

/**
 * Get staff default language
 * @param  mixed $staffid
 * @return mixed
 */
function get_staff_default_language($staffid = '')
{
    if (!is_numeric($staffid)) {
        // checking for current user if is admin
        if (isset($GLOBALS['current_user'])) {
            return $GLOBALS['current_user']->default_language;
        }

        $staffid = get_staff_user_id();
    }
    $CI = & get_instance();
    $CI->db->select('default_language');
    $CI->db->from(db_prefix() . 'staff');
    $CI->db->where('staffid', $staffid);
    $staff = $CI->db->get()->row();
    if ($staff) {
        return $staff->default_language;
    }

    return '';
}

function get_staff_recent_search_history($staff_id = null)
{
    $recentSearches = get_staff_meta($staff_id ? $staff_id : get_staff_user_id(), 'recent_searches');

    if ($recentSearches == '') {
        $recentSearches = [];
    } else {
        $recentSearches = json_decode($recentSearches);
    }

    return $recentSearches;
}

function update_staff_recent_search_history($history, $staff_id = null)
{
    $totalRecentSearches = hooks()->apply_filters('total_recent_searches', 5);
    $history             = array_reverse($history);
    $history             = array_unique($history);
    $history             = array_splice($history, 0, $totalRecentSearches);

    update_staff_meta($staff_id ? $staff_id : get_staff_user_id(), 'recent_searches', json_encode($history));

    return $history;
}


/**
 * Check if user is staff member
 * In the staff profile there is option to check IS NOT STAFF MEMBER eq like contractor
 * Some features are disabled when user is not staff member
 * @param  string  $staff_id staff id
 * @return boolean
 */
function is_staff_member($staff_id = '')
{
    $CI = & get_instance();
    if ($staff_id == '') {
        if (isset($GLOBALS['current_user'])) {
            return $GLOBALS['current_user']->is_not_staff === '0';
        }
        $staff_id = get_staff_user_id();
    }

    $CI->db->where('staffid', $staff_id)
    ->where('is_not_staff', 0);

    return $CI->db->count_all_results(db_prefix() . 'staff') > 0 ? true : false;
}

if (!function_exists('hc_submission_is_open')) {
    /**
     * Submission window: Jan 1 -> Jan 31 (inclusive) for a given year.
     * Uses server time.
     */
    function hc_submission_is_open($year = null)
    {
        $year = $year ?: (int)date('Y');

        $start = strtotime($year . '-01-01 00:00:00');
        $end   = strtotime($year . '-01-31 23:59:59');

        $now = time();
        return ($now >= $start && $now <= $end);
    }
}

if (!function_exists('hc_enforce_submission_window')) {
    /**
     * Deny submit when window is closed.
     * Call this inside store/update actions.
     */
    function hc_enforce_submission_window($year = null, $message = null)
    {
        if (!hc_submission_is_open($year)) {
            $msg = $message ?: 'Submission is closed after 31 January.';
            set_alert('danger', $msg);
            access_denied('hc_compliance'); // or just redirect back if you prefer
        }
    }
}
