/**
 * NotificationContentEntity.java
 * 
 * Room entity for storing notification content with plugin-specific fields
 * Includes encryption support, TTL management, and TimeSafari integration
 * 
 * @author Matthew Raymer
 * @version 1.0.0
 * @since 2025-10-20
 */

package org.timesafari.dailynotification.entities;

import androidx.annotation.NonNull;
import androidx.room.ColumnInfo;
import androidx.room.Entity;
import androidx.room.Index;
import androidx.room.Ignore;
import androidx.room.PrimaryKey;

/**
 * Room entity representing notification content stored in the plugin database
 * 
 * This entity stores notification data with plugin-specific fields including:
 * - Plugin version tracking for migration support
 * - TimeSafari DID integration for user identification
 * - Encryption support for sensitive content
 * - TTL management for automatic cleanup
 * - Analytics fields for usage tracking
 */
@Entity(
    tableName = "notification_content",
    indices = {
        @Index(value = {"timesafari_did"}),
        @Index(value = {"notification_type"}),
        @Index(value = {"scheduled_time"}),
        @Index(value = {"created_at"}),
        @Index(value = {"plugin_version"})
    }
)
public class NotificationContentEntity {
    
    @PrimaryKey
    @NonNull
    @ColumnInfo(name = "id")
    public String id;
    
    @ColumnInfo(name = "plugin_version")
    public String pluginVersion;
    
    @ColumnInfo(name = "timesafari_did")
    public String timesafariDid;
    
    @ColumnInfo(name = "notification_type")
    public String notificationType;
    
    @ColumnInfo(name = "title")
    public String title;
    
    @ColumnInfo(name = "body")
    public String body;
    
    @ColumnInfo(name = "scheduled_time")
    public long scheduledTime;
    
    @ColumnInfo(name = "timezone")
    public String timezone;
    
    @ColumnInfo(name = "priority")
    public int priority;
    
    @ColumnInfo(name = "vibration_enabled")
    public boolean vibrationEnabled;
    
    @ColumnInfo(name = "sound_enabled")
    public boolean soundEnabled;
    
    @ColumnInfo(name = "media_url")
    public String mediaUrl;
    
    @ColumnInfo(name = "encrypted_content")
    public String encryptedContent;
    
    @ColumnInfo(name = "encryption_key_id")
    public String encryptionKeyId;
    
    @ColumnInfo(name = "created_at")
    public long createdAt;
    
    @ColumnInfo(name = "updated_at")
    public long updatedAt;
    
    @ColumnInfo(name = "ttl_seconds")
    public long ttlSeconds;
    
    @ColumnInfo(name = "delivery_status")
    public String deliveryStatus;
    
    @ColumnInfo(name = "delivery_attempts")
    public int deliveryAttempts;
    
    @ColumnInfo(name = "last_delivery_attempt")
    public long lastDeliveryAttempt;
    
    @ColumnInfo(name = "user_interaction_count")
    public int userInteractionCount;
    
    @ColumnInfo(name = "last_user_interaction")
    public long lastUserInteraction;
    
    @ColumnInfo(name = "metadata")
    public String metadata;
    
    /**
     * Default constructor for Room
     */
    public NotificationContentEntity() {
        this.createdAt = System.currentTimeMillis();
        this.updatedAt = System.currentTimeMillis();
        this.deliveryAttempts = 0;
        this.userInteractionCount = 0;
        this.ttlSeconds = 7 * 24 * 60 * 60; // Default 7 days
    }
    
    /**
     * Constructor with required fields
     */
    @Ignore
    public NotificationContentEntity(@NonNull String id, String pluginVersion, String timesafariDid, 
                                    String notificationType, String title, String body, 
                                    long scheduledTime, String timezone) {
        this();
        this.id = id;
        this.pluginVersion = pluginVersion;
        this.timesafariDid = timesafariDid;
        this.notificationType = notificationType;
        this.title = title;
        this.body = body;
        this.scheduledTime = scheduledTime;
        this.timezone = timezone;
    }
    
    /**
     * Check if this notification has expired based on TTL
     */
    public boolean isExpired() {
        long expirationTime = createdAt + (ttlSeconds * 1000);
        return System.currentTimeMillis() > expirationTime;
    }
    
    /**
     * Check if this notification is ready for delivery
     */
    public boolean isReadyForDelivery() {
        return System.currentTimeMillis() >= scheduledTime && !isExpired();
    }
    
    /**
     * Update the last updated timestamp
     */
    public void touch() {
        this.updatedAt = System.currentTimeMillis();
    }
    
    /**
     * Increment delivery attempts and update timestamp
     */
    public void recordDeliveryAttempt() {
        this.deliveryAttempts++;
        this.lastDeliveryAttempt = System.currentTimeMillis();
        touch();
    }
    
    /**
     * Record user interaction
     */
    public void recordUserInteraction() {
        this.userInteractionCount++;
        this.lastUserInteraction = System.currentTimeMillis();
        touch();
    }
    
    /**
     * Get time until expiration in milliseconds
     */
    public long getTimeUntilExpiration() {
        long expirationTime = createdAt + (ttlSeconds * 1000);
        return Math.max(0, expirationTime - System.currentTimeMillis());
    }
    
    /**
     * Get time until scheduled delivery in milliseconds
     */
    public long getTimeUntilDelivery() {
        return Math.max(0, scheduledTime - System.currentTimeMillis());
    }
    
    @Override
    public String toString() {
        return "NotificationContentEntity{" +
                "id='" + id + '\'' +
                ", pluginVersion='" + pluginVersion + '\'' +
                ", timesafariDid='" + timesafariDid + '\'' +
                ", notificationType='" + notificationType + '\'' +
                ", title='" + title + '\'' +
                ", scheduledTime=" + scheduledTime +
                ", deliveryStatus='" + deliveryStatus + '\'' +
                ", deliveryAttempts=" + deliveryAttempts +
                ", userInteractionCount=" + userInteractionCount +
                ", isExpired=" + isExpired() +
                ", isReadyForDelivery=" + isReadyForDelivery() +
                '}';
    }
}
