using System;
using System.Collections.Generic;
using Haoliang.Models.Common;

namespace Haoliang.Models.Models.System
{
    /// <summary>
    /// Rule execution history
    /// </summary>
    public class RuleExecutionHistory
    {
        public int ExecutionId { get; set; }
        public int RuleId { get; set; }
        public string RuleName { get; set; }
        public string InputDataJson { get; set; }
        public string Result { get; set; }
        public bool Success { get; set; }
        public string ErrorMessage { get; set; }
        public TimeSpan ExecutionTime { get; set; }
        public DateTime ExecutionTimeUtc { get; set; }
        public string ExecutedBy { get; set; }
        public string Context { get; set; }
    }

    /// <summary>
    /// Cache statistics
    /// </summary>
    public class CacheStats
    {
        public long TotalItems { get; set; }
        public long HitCount { get; set; }
        public long MissCount { get; set; }
        public double HitRate => HitCount + MissCount > 0 ? (double)HitCount / (HitCount + MissCount) : 0;
        public long MemoryUsageBytes { get; set; }
        public DateTime LastCleared { get; set; }
        public Dictionary<string, long> ItemsByType { get; set; }
        public Dictionary<string, long> EvictionReasons { get; set; }
    }

    /// <summary>
    /// WebSocket statistics
    /// </summary>
    public class WebSocketStats
    {
        public DateTime Timestamp { get; set; }
        public int ConnectedClients { get; set; }
        public int TotalConnections { get; set; }
        public int DisconnectedClients { get; set; }
        public int ActiveStreams { get; set; }
        public long MessagesSent { get; set; }
        public long MessagesReceived { get; set; }
        public long BytesSent { get; set; }
        public long BytesReceived { get; set; }
        public Dictionary<string, int> ClientsByType { get; set; }
        public Dictionary<string, int> MessagesByType { get; set; }
    }

    /// <summary>
    /// System performance metrics
    /// </summary>
    public class PerformanceMetrics
    {
        public DateTime Timestamp { get; set; }
        public double CpuUsagePercent { get; set; }
        public double MemoryUsagePercent { get; set; }
        public double DiskUsagePercent { get; set; }
        public double NetworkUsageMbps { get; set; }
        public int ActiveThreads { get; set; }
        public int QueueLength { get; set; }
        public double ResponseTimeMs { get; set; }
        public double ThroughputPerSecond { get; set; }
        public Dictionary<string, double> CustomMetrics { get; set; }
    }

    /// <summary>
    /// Device performance metrics
    /// </summary>
    public class DevicePerformanceMetrics
    {
        public int DeviceId { get; set; }
        public string DeviceName { get; set; }
        public DateTime Timestamp { get; set; }
        public double CpuUsagePercent { get; set; }
        public double MemoryUsagePercent { get; set; }
        public double TemperatureCelsius { get; set; }
        public double VibrationLevel { get; set; }
        public double PowerConsumptionKW { get; set; }
        public double ToolWearPercent { get; set; }
        public double SpindleRpm { get; set; }
        public double FeedRateMmMin { get; set; }
        public double PositionAccuracyMm { get; set; }
        public Dictionary<string, double> CustomMetrics { get; set; }
    }

    /// <summary>
    /// Production quality metrics
    /// </summary>
    public class QualityMetrics
    {
        public int DeviceId { get; set; }
        public string DeviceName { get; set; }
        public DateTime PeriodStart { get; set; }
        public DateTime PeriodEnd { get; set; }
        public int TotalProduced { get; set; }
        public int TotalGood { get; set; }
        public int TotalRejected { get; set; }
        public decimal FirstPassYieldPercent { get; set; }
        public decimal ReworkRatePercent { get; set; }
        public decimal ScrapRatePercent { get; set; }
        public decimal QualityIndex { get; set; }
        public List<DefectAnalysis> Defects { get; set; }
        public List<InspectionRecord> Inspections { get; set; }
    }

    /// <summary>
    /// Defect analysis
    /// </summary>
    public class DefectAnalysis
    {
        public string DefectType { get; set; }
        public int Count { get; set; }
        public decimal Percentage { get; set; }
        public decimal SeverityScore { get; set; }
        public List<DateTime> OccurrenceTimes { get; set; }
        public string RootCause { get; set; }
        public string CorrectiveAction { get; set; }
        public string Responsibility { get; set; }
    }

    /// <summary>
    /// Inspection record
    /// </summary>
    public class InspectionRecord
    {
        public int InspectionId { get; set; }
        public DateTime InspectionTime { get; set; }
        public string Inspector { get; set; }
        public string InspectionType { get; set; }
        public bool Passed { get; set; }
        public decimal Score { get; set; }
        public List<Defect> FoundDefects { get; set; }
        public string Notes { get; set; }
        public string ImageUrl { get; set; }
    }

    /// <summary>
    /// Defect detail
    /// </summary>
    public class Defect
    {
        public string DefectType { get; set; }
        public string Location { get; set; }
        public decimal Severity { get; set; }
        public string Description { get; set; }
        public bool Critical { get; set; }
    }

    /// <summary>
    /// System health status
    /// </summary>
    public class SystemHealthStatus
    {
        public DateTime Timestamp { get; set; }
        public SystemHealth OverallHealth { get; set; }
        public Dictionary<string, ComponentHealth> ComponentHealth { get; set; }
        public List<HealthAlert> ActiveAlerts { get; set; }
        public SystemUptime Uptime { get; set; }
        public Dictionary<string, string> HealthChecks { get; set; }
    }

    /// <summary>
    /// Component health status
    /// </summary>
    public class ComponentHealth
    {
        public string ComponentName { get; set; }
        public HealthStatus Status { get; set; }
        public double PerformanceScore { get; set; }
        public double AvailabilityScore { get; set; }
        public double QualityScore { get; set; }
        public DateTime LastCheck { get; set; }
        public string Message { get; set; }
        public Dictionary<string, object> Metrics { get; set; }
    }

    /// <summary>
    /// Health alert
    /// </summary>
    public class HealthAlert
    {
        public string AlertId { get; set; }
        public string Component { get; set; }
        public AlertSeverity Severity { get; set; }
        public string Message { get; set; }
        public DateTime OccurredAt { get; set; }
        public bool Resolved { get; set; }
        public DateTime? ResolvedAt { get; set; }
        public string Resolution { get; set; }
    }

    /// <summary>
    /// System uptime information
    /// </summary>
    public class SystemUptime
    {
        public DateTime StartTime { get; set; }
        public TimeSpan Uptime { get; set; }
        public int RestartCount { get; set; }
        public DateTime LastRestart { get; set; }
        public string LastRestartReason { get; set; }
        public Dictionary<string, TimeSpan> ComponentUptimes { get; set; }
    }

    /// <summary>
    /// User preferences
    /// </summary>
    public class UserPreferences
    {
        public string UserId { get; set; }
        public string Language { get; set; }
        public string Theme { get; set; }
        public string TimeZone { get; set; }
        public bool EmailNotifications { get; set; }
        public bool SMSNotifications { get; set; }
        public bool PushNotifications { get; set; }
        public List<string> DashboardLayout { get; set; }
        public List<string> FavoriteReports { get; set; }
        public Dictionary<string, object> CustomSettings { get; set; }
        public DateTime LastUpdated { get; set; }
    }

    /// <summary>
    /// Audit log entry
    /// </summary>
    public class AuditLog
    {
        public int AuditId { get; set; }
        public DateTime Timestamp { get; set; }
        public string UserId { get; set; }
        public string UserName { get; set; }
        public string Action { get; set; }
        public string EntityType { get; set; }
        public int? EntityId { get; set; }
        public string Description { get; set; }
        public string IPAddress { get; set; }
        public string UserAgent { get; set; }
        public bool Success { get; set; }
        public string ErrorMessage { get; set; }
        public Dictionary<string, object> OldValues { get; set; }
        public Dictionary<string, object> NewValues { get; set; }
    }

    /// <summary>
    /// System backup information
    /// </summary>
    public class BackupInfo
    {
        public string BackupId { get; set; }
        public DateTime BackupTime { get; set; }
        public string BackupType { get; set; }
        public long SizeBytes { get; set; }
        public string Status { get; set; }
        public string Location { get; set; }
        public bool IsEncrypted { get; set; }
        public bool IsCompressed { get; set; }
        public List<string> IncludedComponents { get; set; }
        public string VerificationHash { get; set; }
        public DateTime? NextScheduledBackup { get; set; }
    }

    #region Enums

    public enum SystemHealth
    {
        Healthy,
        Warning,
        Critical,
        Unknown
    }

    public enum HealthStatus
    {
        Healthy,
        Degraded,
        Warning,
        Critical,
        Unknown
    }

    public enum AlertSeverity
    {
        Low,
        Medium,
        High,
        Critical
    }

    #endregion
}