/**
 * NotificationConfigDao.java
 * 
 * Data Access Object for NotificationConfigEntity operations
 * Provides efficient queries for configuration management and user preferences
 * 
 * @author Matthew Raymer
 * @version 1.0.0
 * @since 2025-10-20
 */

package org.timesafari.dailynotification.dao;

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

import java.util.List;

/**
 * Data Access Object for notification configuration operations
 * 
 * Provides efficient database operations for:
 * - Configuration management and user preferences
 * - Plugin settings and state persistence
 * - TimeSafari integration configuration
 * - Performance tuning and behavior settings
 */
@Dao
public interface NotificationConfigDao {
    
    // ===== BASIC CRUD OPERATIONS =====
    
    /**
     * Insert a new configuration entity
     */
    @Insert(onConflict = OnConflictStrategy.REPLACE)
    void insertConfig(NotificationConfigEntity config);
    
    /**
     * Insert multiple configuration entities
     */
    @Insert(onConflict = OnConflictStrategy.REPLACE)
    void insertConfigs(List<NotificationConfigEntity> configs);
    
    /**
     * Update an existing configuration entity
     */
    @Update
    void updateConfig(NotificationConfigEntity config);
    
    /**
     * Delete a configuration entity by ID
     */
    @Query("DELETE FROM notification_config WHERE id = :id")
    void deleteConfig(String id);
    
    /**
     * Delete configurations by key
     */
    @Query("DELETE FROM notification_config WHERE config_key = :configKey")
    void deleteConfigsByKey(String configKey);
    
    // ===== QUERY OPERATIONS =====
    
    /**
     * Get configuration by ID
     */
    @Query("SELECT * FROM notification_config WHERE id = :id")
    NotificationConfigEntity getConfigById(String id);
    
    /**
     * Get configuration by key
     */
    @Query("SELECT * FROM notification_config WHERE config_key = :configKey")
    NotificationConfigEntity getConfigByKey(String configKey);
    
    /**
     * Get configuration by key and TimeSafari DID
     */
    @Query("SELECT * FROM notification_config WHERE config_key = :configKey AND timesafari_did = :timesafariDid")
    NotificationConfigEntity getConfigByKeyAndDid(String configKey, String timesafariDid);
    
    /**
     * Get all configuration entities
     */
    @Query("SELECT * FROM notification_config ORDER BY updated_at DESC")
    List<NotificationConfigEntity> getAllConfigs();
    
    /**
     * Get configurations by TimeSafari DID
     */
    @Query("SELECT * FROM notification_config WHERE timesafari_did = :timesafariDid ORDER BY updated_at DESC")
    List<NotificationConfigEntity> getConfigsByTimeSafariDid(String timesafariDid);
    
    /**
     * Get configurations by type
     */
    @Query("SELECT * FROM notification_config WHERE config_type = :configType ORDER BY updated_at DESC")
    List<NotificationConfigEntity> getConfigsByType(String configType);
    
    /**
     * Get active configurations
     */
    @Query("SELECT * FROM notification_config WHERE is_active = 1 ORDER BY updated_at DESC")
    List<NotificationConfigEntity> getActiveConfigs();
    
    /**
     * Get encrypted configurations
     */
    @Query("SELECT * FROM notification_config WHERE is_encrypted = 1 ORDER BY updated_at DESC")
    List<NotificationConfigEntity> getEncryptedConfigs();
    
    // ===== CONFIGURATION-SPECIFIC QUERIES =====
    
    /**
     * Get user preferences
     */
    @Query("SELECT * FROM notification_config WHERE config_type = 'user_preference' AND timesafari_did = :timesafariDid ORDER BY updated_at DESC")
    List<NotificationConfigEntity> getUserPreferences(String timesafariDid);
    
    /**
     * Get plugin settings
     */
    @Query("SELECT * FROM notification_config WHERE config_type = 'plugin_setting' ORDER BY updated_at DESC")
    List<NotificationConfigEntity> getPluginSettings();
    
    /**
     * Get TimeSafari integration settings
     */
    @Query("SELECT * FROM notification_config WHERE config_type = 'timesafari_integration' AND timesafari_did = :timesafariDid ORDER BY updated_at DESC")
    List<NotificationConfigEntity> getTimeSafariIntegrationSettings(String timesafariDid);
    
    /**
     * Get performance settings
     */
    @Query("SELECT * FROM notification_config WHERE config_type = 'performance_setting' ORDER BY updated_at DESC")
    List<NotificationConfigEntity> getPerformanceSettings();
    
    /**
     * Get notification preferences
     */
    @Query("SELECT * FROM notification_config WHERE config_type = 'notification_preference' AND timesafari_did = :timesafariDid ORDER BY updated_at DESC")
    List<NotificationConfigEntity> getNotificationPreferences(String timesafariDid);
    
    // ===== VALUE-BASED QUERIES =====
    
    /**
     * Get configurations by data type
     */
    @Query("SELECT * FROM notification_config WHERE config_data_type = :dataType ORDER BY updated_at DESC")
    List<NotificationConfigEntity> getConfigsByDataType(String dataType);
    
    /**
     * Get boolean configurations
     */
    @Query("SELECT * FROM notification_config WHERE config_data_type = 'boolean' ORDER BY updated_at DESC")
    List<NotificationConfigEntity> getBooleanConfigs();
    
    /**
     * Get integer configurations
     */
    @Query("SELECT * FROM notification_config WHERE config_data_type = 'integer' ORDER BY updated_at DESC")
    List<NotificationConfigEntity> getIntegerConfigs();
    
    /**
     * Get string configurations
     */
    @Query("SELECT * FROM notification_config WHERE config_data_type = 'string' ORDER BY updated_at DESC")
    List<NotificationConfigEntity> getStringConfigs();
    
    /**
     * Get JSON configurations
     */
    @Query("SELECT * FROM notification_config WHERE config_data_type = 'json' ORDER BY updated_at DESC")
    List<NotificationConfigEntity> getJsonConfigs();
    
    // ===== ANALYTICS QUERIES =====
    
    /**
     * Get configuration count by type
     */
    @Query("SELECT COUNT(*) FROM notification_config WHERE config_type = :configType")
    int getConfigCountByType(String configType);
    
    /**
     * Get configuration count by TimeSafari DID
     */
    @Query("SELECT COUNT(*) FROM notification_config WHERE timesafari_did = :timesafariDid")
    int getConfigCountByTimeSafariDid(String timesafariDid);
    
    /**
     * Get total configuration count
     */
    @Query("SELECT COUNT(*) FROM notification_config")
    int getTotalConfigCount();
    
    /**
     * Get active configuration count
     */
    @Query("SELECT COUNT(*) FROM notification_config WHERE is_active = 1")
    int getActiveConfigCount();
    
    /**
     * Get encrypted configuration count
     */
    @Query("SELECT COUNT(*) FROM notification_config WHERE is_encrypted = 1")
    int getEncryptedConfigCount();
    
    // ===== CLEANUP OPERATIONS =====
    
    /**
     * Delete expired configurations
     */
    @Query("DELETE FROM notification_config WHERE (created_at + (ttl_seconds * 1000)) < :currentTime")
    int deleteExpiredConfigs(long currentTime);
    
    /**
     * Delete old configurations
     */
    @Query("DELETE FROM notification_config WHERE created_at < :cutoffTime")
    int deleteOldConfigs(long cutoffTime);
    
    /**
     * Delete configurations by TimeSafari DID
     */
    @Query("DELETE FROM notification_config WHERE timesafari_did = :timesafariDid")
    int deleteConfigsByTimeSafariDid(String timesafariDid);
    
    /**
     * Delete inactive configurations
     */
    @Query("DELETE FROM notification_config WHERE is_active = 0")
    int deleteInactiveConfigs();
    
    /**
     * Delete configurations by type
     */
    @Query("DELETE FROM notification_config WHERE config_type = :configType")
    int deleteConfigsByType(String configType);
    
    // ===== BULK OPERATIONS =====
    
    /**
     * Update configuration values for multiple configs
     */
    @Query("UPDATE notification_config SET config_value = :newValue, updated_at = :updatedAt WHERE id IN (:ids)")
    void updateConfigValuesForConfigs(List<String> ids, String newValue, long updatedAt);
    
    /**
     * Activate/deactivate multiple configurations
     */
    @Query("UPDATE notification_config SET is_active = :isActive, updated_at = :updatedAt WHERE id IN (:ids)")
    void updateActiveStatusForConfigs(List<String> ids, boolean isActive, long updatedAt);
    
    /**
     * Mark configurations as encrypted
     */
    @Query("UPDATE notification_config SET is_encrypted = 1, encryption_key_id = :keyId, updated_at = :updatedAt WHERE id IN (:ids)")
    void markConfigsAsEncrypted(List<String> ids, String keyId, long updatedAt);
    
    // ===== UTILITY QUERIES =====
    
    /**
     * Check if configuration exists by key
     */
    @Query("SELECT COUNT(*) > 0 FROM notification_config WHERE config_key = :configKey")
    boolean configExistsByKey(String configKey);
    
    /**
     * Check if configuration exists by key and TimeSafari DID
     */
    @Query("SELECT COUNT(*) > 0 FROM notification_config WHERE config_key = :configKey AND timesafari_did = :timesafariDid")
    boolean configExistsByKeyAndDid(String configKey, String timesafariDid);
    
    /**
     * Get configuration keys by type
     */
    @Query("SELECT config_key FROM notification_config WHERE config_type = :configType ORDER BY updated_at DESC")
    List<String> getConfigKeysByType(String configType);
    
    /**
     * Get configuration keys by TimeSafari DID
     */
    @Query("SELECT config_key FROM notification_config WHERE timesafari_did = :timesafariDid ORDER BY updated_at DESC")
    List<String> getConfigKeysByTimeSafariDid(String timesafariDid);
    
    // ===== MIGRATION QUERIES =====
    
    /**
     * Get configurations by plugin version
     */
    @Query("SELECT * FROM notification_config WHERE config_key LIKE 'plugin_version_%' ORDER BY updated_at DESC")
    List<NotificationConfigEntity> getConfigsByPluginVersion();
    
    /**
     * Get configurations that need migration
     */
    @Query("SELECT * FROM notification_config WHERE config_key LIKE 'migration_%' ORDER BY updated_at DESC")
    List<NotificationConfigEntity> getConfigsNeedingMigration();
    
    /**
     * Delete migration-related configurations
     */
    @Query("DELETE FROM notification_config WHERE config_key LIKE 'migration_%'")
    int deleteMigrationConfigs();
}
