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

/**
 * Get app logo URL
 */
function get_app_logo($dark = false) {
    $CI =& get_instance();
    $CI->load->model('Setting_model');
    
    $key = $dark ? 'app_logo_dark' : 'app_logo';
    $logo = $CI->Setting_model->get($key);
    
    if ($logo && file_exists(FCPATH . 'uploads/branding/' . $logo)) {
        return base_url('uploads/branding/' . $logo);
    }
    
    return base_url('assets/img/logo.png');
}

/**
 * Get app favicon URL
 */
function get_app_favicon() {
    $CI =& get_instance();
    $CI->load->model('Setting_model');
    
    $favicon = $CI->Setting_model->get('app_favicon');
    
    if ($favicon && file_exists(FCPATH . 'uploads/branding/' . $favicon)) {
        return base_url('uploads/branding/' . $favicon);
    }
    
    return base_url('assets/img/favicon.png');
}

/**
 * Get app name
 */
function get_app_name() {
    $CI =& get_instance();
    $CI->load->model('Setting_model');
    return $CI->Setting_model->get('app_name', '');
}

/**
 * Get support email
 */
function get_support_email() {
    $CI =& get_instance();
    $CI->load->model('Setting_model');
    return $CI->Setting_model->get('support_email', '');
}

/**
 * Get user avatar URL
 */
function get_user_avatar($user, $size = 'w36') {
    if (!empty($user->profile_photo)) {
        return base_url('uploads/avatars/' . $user->profile_photo);
    }
    // Default avatar based on user id to give variety
    $avatar_num = (($user->id ?? 1) % 10) + 1;
    return base_url('assets/img/sample/avatar/avatar' . $avatar_num . '.jpg');
}

/**
 * Get header HTML
 */
function get_header($title = 'MeridianBank', $user = null, $show_back = false, $transparent = false) {
    $CI =& get_instance();
    $unread = $user ? $CI->Notification_model->count_unread($user->id) : 0;
    
    $header_class = $transparent ? 'appHeader no-border transparent position-absolute' : 'appHeader bg-primary text-light';
    
    $html = '
    <div class="' . $header_class . '">
        <div class="left">';
    
    if ($show_back) {
        $html .= '
            <a href="javascript:history.back()" class="headerButton">
                <ion-icon name="chevron-back-outline"></ion-icon>
            </a>';
    } else {
        $html .= '
            <a href="#" class="headerButton" data-bs-toggle="modal" data-bs-target="#sidebarPanel">
                <ion-icon name="menu-outline"></ion-icon>
            </a>';
    }
    
    $html .= '
        </div>
        <div class="pageTitle">' . ($show_back ? $title : '<img src="' . get_app_logo() . '" alt="logo" class="logo">') . '</div>
        <div class="right">';
    
    if ($user) {
        $html .= '
            <a href="' . base_url('dashboard/notifications') . '" class="headerButton">
                <ion-icon class="icon" name="notifications-outline"></ion-icon>
                ' . ($unread > 0 ? '<span class="badge badge-danger">' . $unread . '</span>' : '') . '
            </a>
            <a href="' . base_url('dashboard/profile') . '" class="headerButton">
                <img src="' . base_url('assets/img/sample/avatar/avatar1.jpg') . '" alt="image" class="imaged w32">
            </a>';
    }
    
    $html .= '
        </div>
    </div>';
    
    return $html;
}

/**
 * Get bottom menu HTML
 */
function get_bottom_menu($active = 'dashboard') {
    $menu_items = [
        'dashboard' => ['url' => 'dashboard', 'icon' => 'pie-chart-outline', 'label' => 'Home'],
        'accounts' => ['url' => 'accounts', 'icon' => 'wallet-outline', 'label' => 'Accounts'],
        'transfers' => ['url' => 'transfers', 'icon' => 'swap-horizontal-outline', 'label' => 'Transfer'],
        'cards' => ['url' => 'cards', 'icon' => 'card-outline', 'label' => 'Cards'],
        'settings' => ['url' => 'dashboard/settings', 'icon' => 'settings-outline', 'label' => 'Settings']
    ];
    
    $html = '<div class="appBottomMenu">';
    
    foreach ($menu_items as $key => $item) {
        $active_class = ($key === $active) ? ' active' : '';
        $html .= '
        <a href="' . base_url($item['url']) . '" class="item' . $active_class . '">
            <div class="col">
                <ion-icon name="' . $item['icon'] . '"></ion-icon>
                <strong>' . $item['label'] . '</strong>
            </div>
        </a>';
    }
    
    $html .= '</div>';
    
    return $html;
}

/**
 * Get sidebar HTML
 */
function get_sidebar($user, $total_balance = 0) {
    $avatar_url = get_user_avatar($user, 'w36');
    $html = '
    <div class="modal fade panelbox panelbox-left" id="sidebarPanel" tabindex="-1" role="dialog">
        <div class="modal-dialog" role="document">
            <div class="modal-content">
                <div class="modal-body p-0">
                    <div class="profileBox pt-2 pb-2">
                        <div class="image-wrapper">
                            <img src="' . $avatar_url . '" alt="image" class="imaged w36 rounded">
                        </div>
                        <div class="in">
                            <strong>' . htmlspecialchars($user->first_name . ' ' . $user->last_name) . '</strong>
                            <div class="text-muted">' . htmlspecialchars($user->email) . '</div>
                        </div>
                        <a href="#" class="btn btn-link btn-icon sidebar-close" data-bs-dismiss="modal">
                            <ion-icon name="close-outline"></ion-icon>
                        </a>
                    </div>
                    
                    <div class="sidebar-balance">
                        <div class="listview-title">Balance</div>
                        <div class="in">
                            <h1 class="amount">$ ' . number_format($total_balance, 2) . '</h1>
                        </div>
                    </div>
                    
                    <div class="action-group">
                        <a href="' . base_url('transfers') . '" class="action-button">
                            <div class="in">
                                <div class="iconbox"><ion-icon name="arrow-forward-outline"></ion-icon></div>
                                Transfer
                            </div>
                        </a>
                        <a href="' . base_url('bills') . '" class="action-button">
                            <div class="in">
                                <div class="iconbox"><ion-icon name="receipt-outline"></ion-icon></div>
                                Bills
                            </div>
                        </a>
                        <a href="' . base_url('cards') . '" class="action-button">
                            <div class="in">
                                <div class="iconbox"><ion-icon name="card-outline"></ion-icon></div>
                                Cards
                            </div>
                        </a>
                    </div>
                    
                    <div class="listview-title mt-1">Menu</div>
                    <ul class="listview flush transparent no-line image-listview">
                        <li>
                            <a href="' . base_url('dashboard') . '" class="item">
                                <div class="icon-box bg-primary"><ion-icon name="pie-chart-outline"></ion-icon></div>
                                <div class="in">Dashboard</div>
                            </a>
                        </li>
                        <li>
                            <a href="' . base_url('accounts') . '" class="item">
                                <div class="icon-box bg-primary"><ion-icon name="wallet-outline"></ion-icon></div>
                                <div class="in">Accounts</div>
                            </a>
                        </li>
                        <li>
                            <a href="' . base_url('transactions') . '" class="item">
                                <div class="icon-box bg-primary"><ion-icon name="list-outline"></ion-icon></div>
                                <div class="in">Transactions</div>
                            </a>
                        </li>
                        <li>
                            <a href="' . base_url('bills') . '" class="item">
                                <div class="icon-box bg-primary"><ion-icon name="receipt-outline"></ion-icon></div>
                                <div class="in">Pay Bills</div>
                            </a>
                        </li>
                    </ul>
                    
                    <div class="listview-title mt-1">Others</div>
                    <ul class="listview flush transparent no-line image-listview">';
    
    // Add Admin Panel link for admin users
    if (isset($user->user_type) && $user->user_type === 'admin') {
        $html .= '
                        <li>
                            <a href="' . base_url('admin/dashboard') . '" class="item">
                                <div class="icon-box bg-danger"><ion-icon name="shield-checkmark-outline"></ion-icon></div>
                                <div class="in">Admin Panel</div>
                            </a>
                        </li>';
    }
    
    $html .= '
                        <li>
                            <a href="' . base_url('dashboard/settings') . '" class="item">
                                <div class="icon-box bg-primary"><ion-icon name="settings-outline"></ion-icon></div>
                                <div class="in">Settings</div>
                            </a>
                        </li>
                        <li>
                            <a href="' . base_url('dashboard/profile') . '" class="item">
                                <div class="icon-box bg-primary"><ion-icon name="person-outline"></ion-icon></div>
                                <div class="in">Profile</div>
                            </a>
                        </li>
                        <li>
                            <a href="' . base_url('auth/logout') . '" class="item">
                                <div class="icon-box bg-primary"><ion-icon name="log-out-outline"></ion-icon></div>
                                <div class="in">Log out</div>
                            </a>
                        </li>
                    </ul>
                </div>
            </div>
        </div>
    </div>';
    
    return $html;
}

/**
 * Format money
 */
function format_money($amount, $currency = 'USD') {
    $symbols = [
        'USD' => '$',
        'GBP' => '£',
        'EUR' => '€'
    ];
    
    $symbol = isset($symbols[$currency]) ? $symbols[$currency] : $currency . ' ';
    return $symbol . number_format($amount, 2);
}

/**
 * Time ago
 */
function time_ago($datetime) {
    $time = strtotime($datetime);
    $diff = time() - $time;
    
    if ($diff < 60) return 'Just now';
    if ($diff < 3600) return floor($diff / 60) . ' min ago';
    if ($diff < 86400) return floor($diff / 3600) . ' hours ago';
    if ($diff < 604800) return floor($diff / 86400) . ' days ago';
    
    return date('M d, Y', $time);
}
