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

function get_current_shift() {
    $CI = &get_instance();

    // Model load karen agar pehle load nahi hua
    if (!isset($CI->shifts)) {
        $CI->load->model('Shifts_model', 'shifts');
    }

    $shifts = $CI->shifts->get_all();

    $current_time = date('H:i:s');
    $selected_shift = null;

    foreach ($shifts as $shift) {
        if (strtotime($current_time) >= strtotime($shift->start_time) &&
            strtotime($current_time) <= strtotime($shift->end_time)) {
            $selected_shift = $shift;
            break;
        }
    }

    if (!$selected_shift && !empty($shifts)) {
        $selected_shift = $shifts[0];
    }

    return $selected_shift; // poora shift object return karega
}


if (!function_exists('get_next_token_number')) {
    function get_next_token_number($shift_id) {
        $CI = &get_instance();

        // Token_model guard / load karo (master layout har page pe ye function call karta hai)
        if (!isset($CI->token)) {
            $CI->load->model('Token_model', 'token');
        }

        $date = date('Y-m-d');
        $last_token = $CI->token->get_shift_tokens($shift_id, $date);

        return $last_token + 1;
    }
}

// Naya function: last token number wapis kare
function get_last_token_number($shift_id) {
    $CI = &get_instance();

    if (!isset($CI->token)) {
        $CI->load->model('Token_model', 'token');
    }

    $date = date('Y-m-d');
    $last_token = $CI->token->get_shift_tokens($shift_id, $date);

    return $last_token;
}

if (!function_exists('get_cash_in_hand')) {
    /**
     * Get cash in hand for current user & optional shift.
     * If shift object or id provided, returns shift-wise cash.
     * Otherwise, returns total cash for user today.
     *
     * @param int|null $user_id
     * @param int|object|null $shift Shift id or shift object (optional)
     * @return float
     */
    function get_cash_in_hand($user_id = null, $shift = null) {
        $CI = &get_instance();

        if (!$user_id) {
            $user_id = $CI->session->userdata('qurbani_user_id');
        }
        if (!$user_id) {
            return 0;
        }

        if (is_object($shift)) {
            $shift_id = $shift->id;
        } else {
            $shift_id = $shift;
        }

        if (!isset($CI->payment)) {
            $CI->load->model('Payment_model', 'payment');
        }

        if ($shift_id) {
            // Agar shift id di hai to shift-wise cash
            $cash = $CI->payment->get_user_cash_in_hand($user_id, $shift_id);
        } else {
            // Warna user ki aaj ki total cash
            $cash = $CI->payment->get_user_cash_in_hand($user_id);
        }

        return $cash ?? 0;
    }
}

// New function to update session cash_in_hand automatically
if (!function_exists('update_cash_in_hand_session')) {
    /**
     * Calculate cash in hand for current user and current shift,
     * update session variable, and return value.
     *
     * Use this function anywhere (header, controller, view) to auto-update session cash_in_hand.
     *
     * @return float updated cash_in_hand
     */
    // function update_cash_in_hand_session() {
    //     $CI = &get_instance();

    //     $user_id = $CI->session->userdata('qurbani_user_id');
    //     if (!$user_id) {
    //         // user not logged in
    //         $CI->session->set_userdata('cash_in_hand', 0);
    //         return 0;
    //     }

    //     $current_shift = get_current_shift();
    //     $cash_in_hand = get_cash_in_hand($user_id, $current_shift);

    //     $CI->session->set_userdata('cash_in_hand', $cash_in_hand);

    //     return $cash_in_hand;
    // }

function update_cash_in_hand_session() {
    $CI = &get_instance();

    if (!isset($CI->payment)) {
        $CI->load->model('Payment_model', 'payment');
    }

    if (!isset($CI->shifts)) {
        $CI->load->model('Shifts_model', 'shifts');
    }

    $user_id = $CI->session->userdata('qurbani_user_id');

    if (!$user_id) {
        $CI->session->set_userdata('cash_in_hand', 0);
        return 0;
    }

    $current_shift = get_current_shift();
    $cash_in_hand  = get_cash_in_hand($user_id, $current_shift);

    $CI->session->set_userdata('cash_in_hand', $cash_in_hand);

    return $cash_in_hand;
}

    
}
