<?php
/**
 * PWA Helper Functions - Utility functions for PWA features
 * 
 * Include this file in your controllers/pages to access PWA functionality
 */

/**
 * Get PWA Manager instance (singleton)
 */
function getPWAManager()
{
    static $pwaManager = null;
    
    if ($pwaManager === null) {
        $pwaManager = new PWAManager();
    }
    
    return $pwaManager;
}

/**
 * Send a notification to a user
 * 
 * @param int $userId User ID
 * @param string $title Notification title
 * @param string $message Notification message
 * @param string $type Type: info, alert, success, error
 * @param array $options Additional options (push, tag, data, icon)
 */
function send_notification($userId, $title, $message, $type = 'info', $options = [])
{
    return getPWAManager()->sendNotification($userId, $title, $message, $type, $options);
}

/**
 * Send a push notification (requires push enabled)
 * 
 * @param int $userId User ID
 * @param string $title Push notification title
 * @param string $message Push notification message
 * @param array $options Additional options
 */
function send_push_notification($userId, $title, $message, $options = [])
{
    $options['push'] = true;
    return getPWAManager()->sendNotification($userId, $title, $message, 'info', $options);
}

/**
 * Notify a role (all users with a specific role)
 * 
 * @param string $role Role name (admin, counter, checker, controller)
 * @param string $title Notification title
 * @param string $message Notification message
 * @param string $type Type: info, alert, success, error
 */
function notify_role($role, $title, $message, $type = 'info')
{
    return getPWAManager()->notifyRole($role, $title, $message, $type);
}

/**
 * Broadcast notification to multiple users
 * 
 * @param array $userIds Array of user IDs
 * @param string $title Notification title
 * @param string $message Notification message
 * @param string $type Type: info, alert, success, error
 */
function notify_users($userIds, $title, $message, $type = 'info')
{
    return getPWAManager()->broadcastNotification($userIds, $title, $message, $type);
}

/**
 * Log a sync operation for tracking
 * 
 * @param int $userId User ID
 * @param string $operation Operation type (insert, update, delete)
 * @param string $tableName Table name
 * @param int $recordId Record ID
 * @param string $status Status (pending, synced, failed)
 * @param string $error Error message if failed
 */
function log_sync($userId, $operation, $tableName, $recordId = null, $status = 'pending', $error = null)
{
    return getPWAManager()->logSync($userId, $operation, $tableName, $recordId, $status, $error);
}

/**
 * Get sync status for a user
 * 
 * @param int $userId User ID
 * @return array Sync status (pending, synced, failed, last_sync)
 */
function get_sync_status($userId)
{
    return getPWAManager()->getSyncStatus($userId);
}

/**
 * Check if user has push notifications enabled
 * 
 * @param int $userId User ID
 * @return bool
 */
function has_push_enabled($userId)
{
    return getPWAManager()->hasPushEnabled($userId);
}

/**
 * Get user's offline settings
 * 
 * @param int $userId User ID
 * @return array Offline settings
 */
function get_offline_settings($userId)
{
    return getPWAManager()->getOfflineSettings($userId);
}

/**
 * Update user's offline settings
 * 
 * @param int $userId User ID
 * @param array $settings Settings to update
 */
function update_offline_settings($userId, $settings)
{
    return getPWAManager()->updateOfflineSettings($userId, $settings);
}

/**
 * Check if PWA is properly configured
 * 
 * @return bool
 */
function is_pwa_configured()
{
    return getPWAManager()->isConfigured();
}

/**
 * Get PWA configuration status
 * 
 * @return array Status info
 */
function get_pwa_status()
{
    return getPWAManager()->getStatus();
}

/**
 * Get PWA statistics
 * 
 * @return array Statistics
 */
function get_pwa_stats()
{
    return getPWAManager()->getStats();
}

/**
 * Clean up old sync logs (older than X days)
 * 
 * @param int $daysOld Number of days to keep
 */
function cleanup_sync_logs($daysOld = 30)
{
    return getPWAManager()->cleanupSyncLogs($daysOld);
}

/**
 * Clean up old read notifications
 * 
 * @param int $daysOld Number of days to keep
 */
function cleanup_notifications($daysOld = 30)
{
    return getPWAManager()->cleanupNotifications($daysOld);
}

/**
 * Send test notification (for debugging)
 * 
 * @param int $userId User ID
 */
function send_test_notification($userId)
{
    return getPWAManager()->sendTestNotification($userId);
}

/**
 * Examples of using PWA functions
 * 
 * Place these in your application where appropriate:
 * 
 * 1. When a user completes an assignment:
 *    ---
 *    send_notification(
 *        $userId,
 *        'Assignment Complete',
 *        'You have completed your assignment: Widget Count',
 *        'success',
 *        ['push' => true, 'tag' => 'assignment-complete']
 *    );
 *    
 *    2. When a report is generated:
 *    ---
 *    notify_role(
 *        ROLE_ADMIN,
 *        'Report Generated',
 *        'Daily count report is ready for download',
 *        'info'
 *    );
 *    
 *    3. Log sync operations:
 *    ---
 *    log_sync(
 *        $userId,
 *        'insert',
 *        'counts',
 *        $count_id,
 *        'pending'
 *    );
 *    
 *    4. Notify specific users:
 *    ---
 *    notify_users(
 *        [1, 2, 3],
 *        'Urgent Update',
 *        'Please re-sync your data',
 *        'alert'
 *    );
 *    
 *    5. Check sync status:
 *    ---
 *    $status = get_sync_status($userId);
 *    echo "Pending syncs: " . $status['pending'];
 *    echo "Last sync: " . $status['last_sync'];
 * /

// Load PWAManager class if not already loaded
if (!class_exists('PWAManager')) {
    $libraryPath = dirname(__FILE__) . '/PWAManager.php';
    if (file_exists($libraryPath)) {
        require_once $libraryPath;
    }
}
