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

namespace Haoliang.Models.Production
{
public class ProductionRecordBasic
    {
        public int Id { get; set; }
        public int DeviceId { get; set; }
        public string NCProgram { get; set; }
        public DateTime ProductionDate { get; set; }
        public int Quantity { get; set; }
        public decimal QualityRate { get; set; }
        public DateTime? StartTime { get; set; }
        public DateTime? EndTime { get; set; }
        public int? OperatorId { get; set; }
        public DateTime CreatedAt { get; set; }
    }

    // Duplicate definition removed - see ProductionStatisticsBasic above

    public class ProgramProductionSummary
    {
        public int Id { get; set; }
        public int DeviceId { get; set; }
        public string NCProgram { get; set; }
        public DateTime ProductionDate { get; set; }
        public int TotalQuantity { get; set; }
        public int ValidQuantity { get; set; }
        public int InvalidQuantity { get; set; }
        public decimal QualityRate { get; set; }
    }

    public class ProductionStatisticsBasic
    {
        public DateTime Date { get; set; }
        public int DeviceId { get; set; }
        public string DeviceName { get; set; }
        public string NCProgram { get; set; }
        public int TotalQuantity { get; set; }
        public int ValidQuantity { get; set; }
        public int InvalidQuantity { get; set; }
        public decimal QualityRate { get; set; }
    }

    public class ProductionRealtimeData
    {
        public int DeviceId { get; set; }
        public string DeviceCode { get; set; }
        public string DeviceName { get; set; }
        public string Status { get; set; }
        public bool IsRunning { get; set; }
        public string NCProgram { get; set; }
        public int CurrentCount { get; set; }
        public int TodayQuantity { get; set; }
        public DateTime LastUpdateTime { get; set; }
    }

    public class ProductionCalculator
    {
        public static int CalculateProduction(DeviceCurrentStatus current, DeviceCurrentStatus last)
        {
            // 同一程序连续加工
            if (current.NCProgram == last.NCProgram)
            {
                int diff = current.CumulativeCount - last.CumulativeCount;
                return Math.Max(0, diff); // 异常值保护，避免负数
            }
            
            // 程序切换逻辑
            return CalculateProgramSwitchProduction(current, last);
        }
        
        private static int CalculateProgramSwitchProduction(DeviceCurrentStatus current, DeviceCurrentStatus last)
        {
            // 检查是否切换到新程序
            if (IsNewProgram(current.NCProgram, last.NCProgram))
            {
                // 新程序以当前累计数为起点
                return current.CumulativeCount;
            }
            else
            {
                // 切回历史程序，视为重新开始
                return 0;
            }
        }
        
        public static bool IsNewProgram(string currentProgram, string lastProgram)
        {
            // 实现程序切换判断逻辑
            return currentProgram != lastProgram;
        }
        
        public static void CrossDayReset(DeviceCurrentStatus current, DeviceCurrentStatus last)
        {
            // 跨天处理：0点自动重置
            if (current.RecordTime.Date != last.RecordTime.Date)
            {
                // 新日期以首次采集累计值为起点
                // 这里需要在业务逻辑中处理
            }
        }
    }
}