/**
 * NotificationDeliveryEntity.java
 * 
 * Room entity for tracking notification delivery events and analytics
 * Provides detailed tracking of delivery attempts, failures, and user interactions
 * 
 * @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.ForeignKey;
import androidx.room.Index;
import androidx.room.Ignore;
import androidx.room.PrimaryKey;

/**
 * Room entity for tracking notification delivery events
 * 
 * This entity provides detailed analytics and tracking for:
 * - Delivery attempts and their outcomes
 * - User interaction patterns
 * - Performance metrics
 * - Error tracking and debugging
 */
@Entity(
    tableName = "notification_delivery",
    foreignKeys = @ForeignKey(
        entity = NotificationContentEntity.class,
        parentColumns = "id",
        childColumns = "notification_id",
        onDelete = ForeignKey.CASCADE
    ),
    indices = {
        @Index(value = {"notification_id"}),
        @Index(value = {"delivery_timestamp"}),
        @Index(value = {"delivery_status"}),
        @Index(value = {"user_interaction_type"}),
        @Index(value = {"timesafari_did"})
    }
)
public class NotificationDeliveryEntity {
    
    @PrimaryKey
    @NonNull
    @ColumnInfo(name = "id")
    public String id;
    
    @ColumnInfo(name = "notification_id")
    public String notificationId;
    
    @ColumnInfo(name = "timesafari_did")
    public String timesafariDid;
    
    @ColumnInfo(name = "delivery_timestamp")
    public long deliveryTimestamp;
    
    @ColumnInfo(name = "delivery_status")
    public String deliveryStatus;
    
    @ColumnInfo(name = "delivery_method")
    public String deliveryMethod;
    
    @ColumnInfo(name = "delivery_attempt_number")
    public int deliveryAttemptNumber;
    
    @ColumnInfo(name = "delivery_duration_ms")
    public long deliveryDurationMs;
    
    @ColumnInfo(name = "user_interaction_type")
    public String userInteractionType;
    
    @ColumnInfo(name = "user_interaction_timestamp")
    public long userInteractionTimestamp;
    
    @ColumnInfo(name = "user_interaction_duration_ms")
    public long userInteractionDurationMs;
    
    @ColumnInfo(name = "error_code")
    public String errorCode;
    
    @ColumnInfo(name = "error_message")
    public String errorMessage;
    
    @ColumnInfo(name = "device_info")
    public String deviceInfo;
    
    @ColumnInfo(name = "network_info")
    public String networkInfo;
    
    @ColumnInfo(name = "battery_level")
    public int batteryLevel;
    
    @ColumnInfo(name = "doze_mode_active")
    public boolean dozeModeActive;
    
    @ColumnInfo(name = "exact_alarm_permission")
    public boolean exactAlarmPermission;
    
    @ColumnInfo(name = "notification_permission")
    public boolean notificationPermission;
    
    @ColumnInfo(name = "metadata")
    public String metadata;
    
    /**
     * Default constructor for Room
     */
    public NotificationDeliveryEntity() {
        this.deliveryTimestamp = System.currentTimeMillis();
        this.deliveryAttemptNumber = 1;
        this.deliveryDurationMs = 0;
        this.userInteractionDurationMs = 0;
        this.batteryLevel = -1;
        this.dozeModeActive = false;
        this.exactAlarmPermission = false;
        this.notificationPermission = false;
    }
    
    /**
     * Constructor for delivery tracking
     */
    @Ignore
    public NotificationDeliveryEntity(@NonNull String id, String notificationId, 
                                      String timesafariDid, String deliveryStatus, 
                                      String deliveryMethod) {
        this();
        this.id = id;
        this.notificationId = notificationId;
        this.timesafariDid = timesafariDid;
        this.deliveryStatus = deliveryStatus;
        this.deliveryMethod = deliveryMethod;
    }
    
    /**
     * Record successful delivery
     */
    public void recordSuccessfulDelivery(long durationMs) {
        this.deliveryStatus = "delivered";
        this.deliveryDurationMs = durationMs;
        this.deliveryTimestamp = System.currentTimeMillis();
    }
    
    /**
     * Record failed delivery
     */
    public void recordFailedDelivery(String errorCode, String errorMessage, long durationMs) {
        this.deliveryStatus = "failed";
        this.errorCode = errorCode;
        this.errorMessage = errorMessage;
        this.deliveryDurationMs = durationMs;
        this.deliveryTimestamp = System.currentTimeMillis();
    }
    
    /**
     * Record user interaction
     */
    public void recordUserInteraction(String interactionType, long durationMs) {
        this.userInteractionType = interactionType;
        this.userInteractionTimestamp = System.currentTimeMillis();
        this.userInteractionDurationMs = durationMs;
    }
    
    /**
     * Set device context information
     */
    public void setDeviceContext(int batteryLevel, boolean dozeModeActive, 
                                 boolean exactAlarmPermission, boolean notificationPermission) {
        this.batteryLevel = batteryLevel;
        this.dozeModeActive = dozeModeActive;
        this.exactAlarmPermission = exactAlarmPermission;
        this.notificationPermission = notificationPermission;
    }
    
    /**
     * Check if this delivery was successful
     */
    public boolean isSuccessful() {
        return "delivered".equals(deliveryStatus);
    }
    
    /**
     * Check if this delivery had user interaction
     */
    public boolean hasUserInteraction() {
        return userInteractionType != null && !userInteractionType.isEmpty();
    }
    
    /**
     * Get delivery age in milliseconds
     */
    public long getDeliveryAge() {
        return System.currentTimeMillis() - deliveryTimestamp;
    }
    
    /**
     * Get time since user interaction in milliseconds
     */
    public long getTimeSinceUserInteraction() {
        if (userInteractionTimestamp == 0) {
            return -1; // No interaction recorded
        }
        return System.currentTimeMillis() - userInteractionTimestamp;
    }
    
    @Override
    public String toString() {
        return "NotificationDeliveryEntity{" +
                "id='" + id + '\'' +
                ", notificationId='" + notificationId + '\'' +
                ", deliveryStatus='" + deliveryStatus + '\'' +
                ", deliveryMethod='" + deliveryMethod + '\'' +
                ", deliveryAttemptNumber=" + deliveryAttemptNumber +
                ", userInteractionType='" + userInteractionType + '\'' +
                ", errorCode='" + errorCode + '\'' +
                ", isSuccessful=" + isSuccessful() +
                ", hasUserInteraction=" + hasUserInteraction() +
                '}';
    }
}
