// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;

namespace FloraMultiPlatformFrontend.Data.Entities;

/// <summary>
/// Represents pre-aggregated surveyor data at various levels of detail (LOD).
/// Used to optimize performance when loading large time ranges by storing
/// time-weighted averages at different bucket sizes.
/// </summary>
[Table("CachedAggregatedSurveyorData")]
public class CachedAggregatedSurveyorData
{
    /// <summary>
    /// Primary key
    /// </summary>
    [Key]
    public int Id { get; set; }

    /// <summary>
    /// Machine identifier (e.g., "M2725")
    /// </summary>
    public string MachineCode { get; set; } = string.Empty;

    /// <summary>
    /// Tag ID from the surveyor data
    /// </summary>
    public int TagId { get; set; }

    /// <summary>
    /// Level of Detail (0-5):
    /// 0 = Raw (no aggregation)
    /// 1 = 3 seconds
    /// 2 = 15 seconds
    /// 3 = 1 minute
    /// 4 = 5 minutes
    /// 5 = 30 minutes
    /// </summary>
    public int LevelOfDetail { get; set; }

    /// <summary>
    /// Start time of the aggregation bucket
    /// </summary>
    public DateTime BucketStartTime { get; set; }

    /// <summary>
    /// Time-weighted average value for this bucket.
    /// Calculated as: SUM(value × duration) / SUM(duration)
    /// This correctly handles sparse change-only data.
    /// </summary>
    public double TimeWeightedAvg { get; set; }

    /// <summary>
    /// Minimum value seen in this bucket.
    /// Preserves spikes/valleys even when aggregated.
    /// </summary>
    public double MinValue { get; set; }

    /// <summary>
    /// Maximum value seen in this bucket.
    /// Preserves spikes/valleys even when aggregated.
    /// </summary>
    public double MaxValue { get; set; }

    /// <summary>
    /// Number of original data points that contributed to this bucket.
    /// Useful for identifying sparse vs. dense regions.
    /// </summary>
    public int OriginalPointCount { get; set; }

    /// <summary>
    /// Total duration (in milliseconds) covered by the data in this bucket.
    /// May be less than bucket size if there are gaps.
    /// </summary>
    public long TotalDurationMs { get; set; }

    /// <summary>
    /// Timestamp when this cached entry was created
    /// </summary>
    public DateTime CachedAt { get; set; }
}
