<?php
/**
 * RUHONDO BEACH RESORT HMS + POS System
 * Notification Functions
 * Developer: JOACHIM'S INNOTECH
 */

// Prevent direct access
if (!defined('BASE_PATH')) {
    exit('Direct access not permitted');
}

/**
 * Get unread notification count for a user
 * 
 * @param int $user_id
 * @return int
 */
if (!function_exists('get_unread_notification_count')) {
    function get_unread_notification_count($user_id) {
        if (!is_numeric($user_id) || $user_id <= 0) {
            return 0;
        }
        
        try {
            if (function_exists('db_fetch_value')) {
                return (int)db_fetch_value(
                    "SELECT COUNT(*) FROM notifications WHERE user_id = ? AND is_read = 0",
                    [$user_id]
                ) ?: 0;
            }
        } catch (Exception $e) {
            error_log("Error in get_unread_notification_count: " . $e->getMessage());
        }
        
        return 0;
    }
}

/**
 * Get user notifications
 * 
 * @param int $user_id
 * @param int $limit
 * @return array
 */
if (!function_exists('get_user_notifications')) {
    function get_user_notifications($user_id, $limit = 10) {
        if (!is_numeric($user_id) || $user_id <= 0) {
            return [];
        }
        
        try {
            if (function_exists('db_fetch_all')) {
                return db_fetch_all(
                    "SELECT * FROM notifications WHERE user_id = ? ORDER BY created_at DESC LIMIT ?",
                    [$user_id, $limit]
                ) ?: [];
            }
        } catch (Exception $e) {
            error_log("Error in get_user_notifications: " . $e->getMessage());
        }
        
        return [];
    }
}

/**
 * Mark notification as read
 * 
 * @param int $notification_id
 * @param int $user_id
 * @return bool
 */
if (!function_exists('mark_notification_read')) {
    function mark_notification_read($notification_id, $user_id = null) {
        try {
            if (function_exists('db_update')) {
                $where = "notification_id = ?";
                $params = [$notification_id];
                
                if ($user_id) {
                    $where .= " AND user_id = ?";
                    $params[] = $user_id;
                }
                
                return db_update('notifications', ['is_read' => 1, 'read_at' => date('Y-m-d H:i:s')], $where, $params) > 0;
            }
        } catch (Exception $e) {
            error_log("Error in mark_notification_read: " . $e->getMessage());
        }
        
        return false;
    }
}

/**
 * Mark all notifications as read for a user
 * 
 * @param int $user_id
 * @return bool
 */
if (!function_exists('mark_all_notifications_read')) {
    function mark_all_notifications_read($user_id) {
        try {
            if (function_exists('db_update')) {
                return db_update(
                    'notifications', 
                    ['is_read' => 1, 'read_at' => date('Y-m-d H:i:s')], 
                    'user_id = ? AND is_read = 0', 
                    [$user_id]
                ) > 0;
            }
        } catch (Exception $e) {
            error_log("Error in mark_all_notifications_read: " . $e->getMessage());
        }
        
        return false;
    }
}

/**
 * Create a notification
 * 
 * @param int $user_id
 * @param string $type
 * @param string $title
 * @param string $message
 * @param string $link
 * @return int|false
 */
if (!function_exists('create_notification')) {
    function create_notification($user_id, $type, $title, $message, $link = null) {
        try {
            if (function_exists('db_insert')) {
                $data = [
                    'user_id' => $user_id,
                    'type' => $type,
                    'title' => $title,
                    'message' => $message,
                    'link' => $link,
                    'is_read' => 0,
                    'created_at' => date('Y-m-d H:i:s')
                ];
                
                return db_insert('notifications', $data);
            }
        } catch (Exception $e) {
            error_log("Error in create_notification: " . $e->getMessage());
        }
        
        return false;
    }
}

/**
 * Delete old notifications
 * 
 * @param int $days_old
 * @return int
 */
if (!function_exists('cleanup_old_notifications')) {
    function cleanup_old_notifications($days_old = 30) {
        try {
            if (function_exists('db_delete')) {
                $cutoff_date = date('Y-m-d H:i:s', strtotime("-{$days_old} days"));
                return db_delete('notifications', "created_at < ? AND is_read = 1", [$cutoff_date]);
            }
        } catch (Exception $e) {
            error_log("Error in cleanup_old_notifications: " . $e->getMessage());
        }
        
        return 0;
    }
}