using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Haoliang.Models.System;
using Haoliang.Models.Production;

namespace Haoliang.Core.Services
{
    public interface IProductionStatisticsService
    {
        /// <summary>
        /// Calculate production trends for a specific device and time range
        /// </summary>
        Task<ProductionTrendAnalysis> CalculateProductionTrendsAsync(int deviceId, DateTime startDate, DateTime endDate);
        
        /// <summary>
        /// Generate comprehensive production report
        /// </summary>
        Task<ProductionReport> GenerateProductionReportAsync(ReportFilter filter);
        
        /// <summary>
        /// Calculate efficiency metrics for devices or programs
        /// </summary>
        Task<EfficiencyMetrics> CalculateEfficiencyMetricsAsync(EfficiencyFilter filter);
        
        /// <summary>
        /// Perform quality analysis based on production data
        /// </summary>
        Task<QualityAnalysis> PerformQualityAnalysisAsync(QualityFilter filter);
        
        /// <summary>
        /// Get production summary for dashboard display
        /// </summary>
        Task<DashboardSummary> GetDashboardSummaryAsync(DashboardFilter filter);
        
        /// <summary>
        /// Calculate OEE (Overall Equipment Effectiveness)
        /// </summary>
        Task<OeeMetrics> CalculateOeeAsync(int deviceId, DateTime date);
        
        /// <summary>
        /// Get production forecasts based on historical data
        /// </summary>
        Task<ProductionForecast> GenerateProductionForecastAsync(ForecastFilter filter);
        
        /// <summary>
        /// Analyze production anomalies and outliers
        /// </summary>
        Task<AnomalyAnalysis> DetectProductionAnomaliesAsync(AnomalyFilter filter);
    }
    
    // Supporting models for statistics
    public class ProductionTrendAnalysis
    {
        public int DeviceId { get; set; }
        public string DeviceName { get; set; }
        public DateTime PeriodStart { get; set; }
        public DateTime PeriodEnd { get; set; }
        public decimal TotalProduction { get; set; }
        public decimal AverageDailyProduction { get; set; }
        public decimal ProductionVariance { get; set; }
        public double TrendCoefficient { get; set; }
        public ProductionTrendDirection TrendDirection { get; set; }
        public List<DailyProduction> DailyData { get; set; }
    }
    
    public class DailyProduction
    {
        public DateTime Date { get; set; }
        public decimal Quantity { get; set; }
        public decimal Target { get; set; }
        public decimal Efficiency { get; set; }
        public List<ProductionRecord> Records { get; set; }
    }
    
    public class ProductionReport
    {
        public DateTime ReportDate { get; set; }
        public ReportType ReportType { get; set; }
        public List<ProductionSummaryItem> SummaryItems { get; set; }
        public ReportMetadata Metadata { get; set; }
    }
    
    public class ProductionSummaryItem
    {
        public int DeviceId { get; set; }
        public string DeviceName { get; set; }
        public string ProgramName { get; set; }
        public decimal Quantity { get; set; }
        public decimal TargetQuantity { get; set; }
        public decimal Efficiency { get; set; }
        public decimal QualityRate { get; set; }
        public TimeSpan Runtime { get; set; }
        public TimeSpan Downtime { get; set; }
    }
    
    public class EfficiencyMetrics
    {
        public int DeviceId { get; set; }
        public string DeviceName { get; set; }
        public DateTime PeriodStart { get; set; }
        public DateTime PeriodEnd { get; set; }
        public decimal Availability { get; set; }
        public decimal Performance { get; set; }
        public decimal Quality { get; set; }
        public decimal Oee { get; set; }
        public EquipmentUtilization Utilization { get; set; }
        public List<HourlyEfficiency> HourlyData { get; set; }
    }
    
    public class HourlyEfficiency
    {
        public DateTime Hour { get; set; }
        public decimal Availability { get; set; }
        public decimal Performance { get; set; }
        public decimal Quality { get; set; }
        public decimal Oee { get; set; }
    }
    
    public class QualityAnalysis
    {
        public int DeviceId { get; set; }
        public string DeviceName { get; set; }
        public DateTime PeriodStart { get; set; }
        public DateTime PeriodEnd { get; set; }
        public decimal TotalProduced { get; set; }
        public decimal TotalGood { get; set; }
        public decimal QualityRate { get; set; }
        public decimal DefectRate { get; set; }
        public List<QualityMetric> QualityMetrics { get; set; }
        public List<DefectAnalysis> DefectAnalysis { get; set; }
    }
    
    public class QualityMetric
    {
        public string MetricName { get; set; }
        public decimal Value { get; set; }
        public string Unit { get; set; }
        public DateTime Timestamp { get; set; }
    }
    
    public class DefectAnalysis
    {
        public string DefectType { get; set; }
        public int Count { get; set; }
        public decimal Percentage { get; set; }
        public List<DateTime> OccurrenceTimes { get; set; }
    }
    
    public class DashboardSummary
    {
        public DateTime GeneratedAt { get; set; }
        public int TotalDevices { get; set; }
        public int ActiveDevices { get; set; }
        public int OfflineDevices { get; set; }
        public decimal TotalProductionToday { get; set; }
        public decimal TotalProductionThisWeek { get; set; }
        public decimal TotalProductionThisMonth { get; set; }
        public decimal OverallEfficiency { get; set; }
        public decimal QualityRate { get; set; }
        public List<DeviceSummary> DeviceSummaries { get; set; }
        public List<AlertSummary> ActiveAlerts { get; set; }
    }
    
    public class DeviceSummary
    {
        public int DeviceId { get; set; }
        public string DeviceName { get; set; }
        public DeviceStatus Status { get; set; }
        public decimal TodayProduction { get; set; }
        public decimal Efficiency { get; set; }
        public decimal QualityRate { get; set; }
        public TimeSpan Runtime { get; set; }
        public string CurrentProgram { get; set; }
    }
    
    public class AlertSummary
    {
        public int AlertId { get; set; }
        public string DeviceName { get; set; }
        public AlertType AlertType { get; set; }
        public string Message { get; set; }
        public DateTime CreatedAt { get; set; }
        public bool IsActive { get; set; }
    }
    
    public class OeeMetrics
    {
        public int DeviceId { get; set; }
        public string DeviceName { get; set; }
        public DateTime Date { get; set; }
        public decimal Availability { get; set; }
        public decimal Performance { get; set; }
        public decimal Quality { get; set; }
        public decimal Oee { get; set; }
        public TimeSpan PlannedProductionTime { get; set; }
        public TimeSpan ActualProductionTime { get; set; }
        public TimeSpan Downtime { get; set; }
        public decimal IdealCycleTime { get; set; }
        public decimal TotalCycleTime { get; set; }
        public decimal TotalPieces { get; set; }
        public decimal GoodPieces { get; set; }
    }
    
    public class ProductionForecast
    {
        public int DeviceId { get; set; }
        public string DeviceName { get; set; }
        public DateTime ForecastStartDate { get; set; }
        public DateTime ForecastEndDate { get; set; }
        public List<ForecastItem> DailyForecasts { get; set; }
        public decimal ForecastAccuracy { get; set; }
        public ForecastModel ModelUsed { get; set; }
    }
    
    public class ForecastItem
    {
        public DateTime Date { get; set; }
        public decimal ForecastedQuantity { get; set; }
        public decimal ConfidenceLower { get; set; }
        public decimal ConfidenceUpper { get; set; }
        public decimal ActualQuantity { get; set; }
        public decimal Variance { get; set; }
    }
    
    public class AnomalyAnalysis
    {
        public int DeviceId { get; set; }
        public string DeviceName { get; set; }
        public DateTime AnalysisStartDate { get; set; }
        public DateTime AnalysisEndDate { get; set; }
        public List<ProductionAnomaly> Anomalies { get; set; }
        public AnomalySeverity OverallSeverity { get; set; }
    }
    
    public class ProductionAnomaly
    {
        public DateTime Timestamp { get; set; }
        public AnomalyType Type { get; set; }
        public AnomalySeverity Severity { get; set; }
        public decimal Deviation { get; set; }
        public string Description { get; set; }
        public AnomalyAction RecommendedAction { get; set; }
    }
    
    // Filter models
    public class ReportFilter
    {
        public List<int> DeviceIds { get; set; }
        public List<string> ProgramNames { get; set; }
        public DateTime StartDate { get; set; }
        public DateTime EndDate { get; set; }
        public ReportType ReportType { get; set; }
        public GroupBy GroupBy { get; set; }
    }
    
    public class EfficiencyFilter
    {
        public List<int> DeviceIds { get; set; }
        public DateTime StartDate { get; set; }
        public DateTime EndDate { get; set; }
        public EfficiencyMetric Metrics { get; set; }
        public GroupBy GroupBy { get; set; }
    }
    
    public class QualityFilter
    {
        public List<int> DeviceIds { get; set; }
        public List<string> ProgramNames { get; set; }
        public DateTime StartDate { get; set; }
        public DateTime EndDate { get; set; }
        public QualityMetricType MetricType { get; set; }
    }
    
    public class DashboardFilter
    {
        public List<int> DeviceIds { get; set; }
        public DateTime Date { get; set; }
        public bool IncludeAlerts { get; set; } = true;
    }
    
    public class ForecastFilter
    {
        public int DeviceId { get; set; }
        public int DaysToForecast { get; set; }
        public ForecastModel Model { get; set; }
        public DateTime HistoricalDataStart { get; set; }
        public DateTime HistoricalDataEnd { get; set; }
    }
    
    public class AnomalyFilter
    {
        public List<int> DeviceIds { get; set; }
        public DateTime StartDate { get; set; }
        public DateTime EndDate { get; set; }
        public AnomalyType? Type { get; set; }
        public AnomalySeverity? MinSeverity { get; set; }
    }
    
    // Enum types
    public enum ProductionTrendDirection
    {
        Increasing,
        Decreasing,
        Stable,
        Volatile
    }
    
    public enum ReportType
    {
        Daily,
        Weekly,
        Monthly,
        Custom
    }
    
    public enum ReportMetadata
    {
        GeneratedAt,
        GeneratedBy,
        Period,
        DeviceCount,
        TotalProduction,
        AverageEfficiency
    }
    
    public enum EquipmentUtilization
    {
        Low,
        Medium,
        High,
        Optimal
    }
    
    public enum AlertType
    {
        DeviceOffline,
        ProductionAnomaly,
        QualityIssue,
        MaintenanceRequired,
        SystemError
    }
    
    public enum GroupBy
    {
        Device,
        Program,
        Date,
        Hour
    }
    
    public enum QualityMetricType
    {
        DefectRate,
        FirstPassYield,
        ReworkRate,
        ScrapRate
    }
    
    public enum ForecastModel
    {
        Linear,
        ExponentialSmoothing,
        Seasonal,
        MovingAverage,
        MachineLearning
    }
    
    public enum AnomalyType
    {
        ProductionDrop,
        QualitySpike,
        DowntimeSpike,
        EfficiencyDrop,
        RuntimeAnomaly
    }
    
    public enum AnomalySeverity
    {
        Low,
        Medium,
        High,
        Critical
    }
    
    public enum AnomalyAction
    {
        Monitor,
        Investigate,
        Alert,
        Shutdown
    }
    
    public enum EfficiencyMetric
    {
        Availability,
        Performance,
        Quality,
        Oee,
        All
    }
}