/**
 * NotificationContentDao.java
 * 
 * Data Access Object for NotificationContentEntity operations
 * Provides efficient queries and operations for notification content management
 * 
 * @author Matthew Raymer
 * @version 1.0.0
 * @since 2025-10-20
 */

package org.timesafari.dailynotification.dao;

import androidx.room.*;
import org.timesafari.dailynotification.entities.NotificationContentEntity;

import java.util.List;

/**
 * Data Access Object for notification content operations
 * 
 * Provides efficient database operations for:
 * - CRUD operations on notification content
 * - Plugin-specific queries and filtering
 * - Performance-optimized bulk operations
 * - Analytics and reporting queries
 */
@Dao
public interface NotificationContentDao {
    
    // ===== BASIC CRUD OPERATIONS =====
    
    /**
     * Insert a new notification content entity
     */
    @Insert(onConflict = OnConflictStrategy.REPLACE)
    void insertNotification(NotificationContentEntity notification);
    
    /**
     * Insert multiple notification content entities
     */
    @Insert(onConflict = OnConflictStrategy.REPLACE)
    void insertNotifications(List<NotificationContentEntity> notifications);
    
    /**
     * Update an existing notification content entity
     */
    @Update
    void updateNotification(NotificationContentEntity notification);
    
    /**
     * Delete a notification content entity by ID
     */
    @Query("DELETE FROM notification_content WHERE id = :id")
    void deleteNotification(String id);
    
    /**
     * Delete multiple notification content entities by IDs
     */
    @Query("DELETE FROM notification_content WHERE id IN (:ids)")
    void deleteNotifications(List<String> ids);
    
    // ===== QUERY OPERATIONS =====
    
    /**
     * Get notification content by ID
     */
    @Query("SELECT * FROM notification_content WHERE id = :id")
    NotificationContentEntity getNotificationById(String id);
    
    /**
     * Get all notification content entities
     */
    @Query("SELECT * FROM notification_content ORDER BY scheduled_time ASC")
    List<NotificationContentEntity> getAllNotifications();
    
    /**
     * Get notifications by TimeSafari DID
     */
    @Query("SELECT * FROM notification_content WHERE timesafari_did = :timesafariDid ORDER BY scheduled_time ASC")
    List<NotificationContentEntity> getNotificationsByTimeSafariDid(String timesafariDid);
    
    /**
     * Get notifications by plugin version
     */
    @Query("SELECT * FROM notification_content WHERE plugin_version = :pluginVersion ORDER BY created_at DESC")
    List<NotificationContentEntity> getNotificationsByPluginVersion(String pluginVersion);
    
    /**
     * Get notifications by type
     */
    @Query("SELECT * FROM notification_content WHERE notification_type = :notificationType ORDER BY scheduled_time ASC")
    List<NotificationContentEntity> getNotificationsByType(String notificationType);
    
    /**
     * Get notifications ready for delivery
     */
    @Query("SELECT * FROM notification_content WHERE scheduled_time <= :currentTime AND delivery_status != 'delivered' ORDER BY scheduled_time ASC")
    List<NotificationContentEntity> getNotificationsReadyForDelivery(long currentTime);
    
    /**
     * Get expired notifications
     */
    @Query("SELECT * FROM notification_content WHERE (created_at + (ttl_seconds * 1000)) < :currentTime")
    List<NotificationContentEntity> getExpiredNotifications(long currentTime);
    
    // ===== PLUGIN-SPECIFIC QUERIES =====
    
    /**
     * Get notifications scheduled for a specific time range
     */
    @Query("SELECT * FROM notification_content WHERE scheduled_time BETWEEN :startTime AND :endTime ORDER BY scheduled_time ASC")
    List<NotificationContentEntity> getNotificationsInTimeRange(long startTime, long endTime);
    
    /**
     * Get notifications by delivery status
     */
    @Query("SELECT * FROM notification_content WHERE delivery_status = :deliveryStatus ORDER BY scheduled_time ASC")
    List<NotificationContentEntity> getNotificationsByDeliveryStatus(String deliveryStatus);
    
    /**
     * Get notifications with user interactions
     */
    @Query("SELECT * FROM notification_content WHERE user_interaction_count > 0 ORDER BY last_user_interaction DESC")
    List<NotificationContentEntity> getNotificationsWithUserInteractions();
    
    /**
     * Get notifications by priority
     */
    @Query("SELECT * FROM notification_content WHERE priority = :priority ORDER BY scheduled_time ASC")
    List<NotificationContentEntity> getNotificationsByPriority(int priority);
    
    // ===== ANALYTICS QUERIES =====
    
    /**
     * Get notification count by type
     */
    @Query("SELECT COUNT(*) FROM notification_content WHERE notification_type = :notificationType")
    int getNotificationCountByType(String notificationType);
    
    /**
     * Get notification count by TimeSafari DID
     */
    @Query("SELECT COUNT(*) FROM notification_content WHERE timesafari_did = :timesafariDid")
    int getNotificationCountByTimeSafariDid(String timesafariDid);
    
    /**
     * Get total notification count
     */
    @Query("SELECT COUNT(*) FROM notification_content")
    int getTotalNotificationCount();
    
    /**
     * Get average user interaction count
     */
    @Query("SELECT AVG(user_interaction_count) FROM notification_content WHERE user_interaction_count > 0")
    double getAverageUserInteractionCount();
    
    /**
     * Get notifications with high interaction rates
     */
    @Query("SELECT * FROM notification_content WHERE user_interaction_count > :minInteractions ORDER BY user_interaction_count DESC")
    List<NotificationContentEntity> getHighInteractionNotifications(int minInteractions);
    
    // ===== CLEANUP OPERATIONS =====
    
    /**
     * Delete expired notifications
     */
    @Query("DELETE FROM notification_content WHERE (created_at + (ttl_seconds * 1000)) < :currentTime")
    int deleteExpiredNotifications(long currentTime);
    
    /**
     * Delete notifications older than specified time
     */
    @Query("DELETE FROM notification_content WHERE created_at < :cutoffTime")
    int deleteOldNotifications(long cutoffTime);
    
    /**
     * Delete notifications by plugin version
     */
    @Query("DELETE FROM notification_content WHERE plugin_version < :minVersion")
    int deleteNotificationsByPluginVersion(String minVersion);
    
    /**
     * Delete notifications by TimeSafari DID
     */
    @Query("DELETE FROM notification_content WHERE timesafari_did = :timesafariDid")
    int deleteNotificationsByTimeSafariDid(String timesafariDid);
    
    // ===== BULK OPERATIONS =====
    
    /**
     * Update delivery status for multiple notifications
     */
    @Query("UPDATE notification_content SET delivery_status = :deliveryStatus, updated_at = :updatedAt WHERE id IN (:ids)")
    void updateDeliveryStatusForNotifications(List<String> ids, String deliveryStatus, long updatedAt);
    
    /**
     * Increment delivery attempts for multiple notifications
     */
    @Query("UPDATE notification_content SET delivery_attempts = delivery_attempts + 1, last_delivery_attempt = :currentTime, updated_at = :currentTime WHERE id IN (:ids)")
    void incrementDeliveryAttemptsForNotifications(List<String> ids, long currentTime);
    
    /**
     * Update user interaction count for multiple notifications
     */
    @Query("UPDATE notification_content SET user_interaction_count = user_interaction_count + 1, last_user_interaction = :currentTime, updated_at = :currentTime WHERE id IN (:ids)")
    void incrementUserInteractionsForNotifications(List<String> ids, long currentTime);
    
    // ===== PERFORMANCE QUERIES =====
    
    /**
     * Get notification IDs only (for lightweight operations)
     */
    @Query("SELECT id FROM notification_content WHERE scheduled_time <= :currentTime AND delivery_status != 'delivered'")
    List<String> getNotificationIdsReadyForDelivery(long currentTime);
    
    /**
     * Get notification count by delivery status
     */
    @Query("SELECT delivery_status AS deliveryStatus, COUNT(*) AS count FROM notification_content GROUP BY delivery_status")
    List<NotificationCountByStatus> getNotificationCountByDeliveryStatus();
    
    /**
     * Data class for delivery status counts
     */
    class NotificationCountByStatus {
        public String deliveryStatus;
        public int count;
        
        public NotificationCountByStatus(String deliveryStatus, int count) {
            this.deliveryStatus = deliveryStatus;
            this.count = count;
        }
    }
}
