using System;
using System.Collections.Generic;
using Xunit;
using Haoliang.Models.Device;
using Haoliang.Models.Template;
using Haoliang.Models.Production;
using Haoliang.Models.User;
using Haoliang.Models.System;

namespace Haoliang.Tests
{
    public class ModelTests
    {
        [Fact]
        public void CNCDevice_CreatedWithValidData_HasCorrectProperties()
        {
            // Arrange & Act
            var device = new CNCDevice
            {
                DeviceCode = "TEST001",
                DeviceName = "测试设备",
                IPAddress = "192.168.1.100",
                HttpUrl = "http://192.168.1.100/api/status",
                CollectionInterval = 30,
                TemplateId = 1,
                IsAvailable = true,
                IsOnline = false
            };

            // Assert
            Assert.Equal("TEST001", device.DeviceCode);
            Assert.Equal("测试设备", device.DeviceName);
            Assert.Equal("192.168.1.100", device.IPAddress);
            Assert.Equal("http://192.168.1.100/api/status", device.HttpUrl);
            Assert.Equal(30, device.CollectionInterval);
            Assert.Equal(1, device.TemplateId);
            Assert.True(device.IsAvailable);
            Assert.False(device.IsOnline);
        }

        [Fact]
        public void CNCDevice_WhenCreated_HasDefaultTimestamps()
        {
            // Arrange & Act
            var device = new CNCDevice();
            var beforeCreation = DateTime.Now;

            // Assert
            Assert.True(device.CreatedAt >= beforeCreation || device.CreatedAt == DateTime.MinValue);
            Assert.True(device.UpdatedAt >= beforeCreation || device.UpdatedAt == DateTime.MinValue);
        }

        [Fact]
        public void TagData_CreatedWithValidData_HasCorrectProperties()
        {
            // Arrange & Act
            var tag = new TagData
            {
                Id = "_io_status",
                Desc = "设备状态",
                Quality = "Good",
                Value = "Running",
                Time = DateTime.Now
            };

            // Assert
            Assert.Equal("_io_status", tag.Id);
            Assert.Equal("设备状态", tag.Desc);
            Assert.Equal("Good", tag.Quality);
            Assert.Equal("Running", tag.Value);
            Assert.NotNull(tag.Time);
        }

        [Fact]
        public void CNCBrandTemplate_CreatedWithValidData_HasCorrectProperties()
        {
            // Arrange & Act
            var template = new CNCBrandTemplate
            {
                BrandName = "发那科",
                Description = "发那科CNC设备模板",
                IsEnabled = true,
                FieldMappings = new List<TemplateFieldMapping>()
            };

            // Assert
            Assert.Equal("发那科", template.BrandName);
            Assert.Equal("发那科CNC设备模板", template.Description);
            Assert.True(template.IsEnabled);
            Assert.Empty(template.FieldMappings);
        }

        [Fact]
        public void ProductionRecord_CreatedWithValidData_HasCorrectProperties()
        {
            // Arrange & Act
            var record = new ProductionRecord
            {
                DeviceId = 1,
                NCProgram = "O0001",
                ProductionDate = DateTime.Now,
                Quantity = 100,
                QualityRate = 95.5m,
                OperatorId = 1
            };

            // Assert
            Assert.Equal(1, record.DeviceId);
            Assert.Equal("O0001", record.NCProgram);
            Assert.Equal(DateTime.Now.Date, record.ProductionDate.Date);
            Assert.Equal(100, record.Quantity);
            Assert.Equal(95.5m, record.QualityRate);
            Assert.Equal(1, record.OperatorId);
            Assert.NotNull(record.CreatedAt);
        }

        [Fact]
        public void ProductionRealtimeData_CreatedWithValidData_HasCorrectProperties()
        {
            // Arrange & Act
            var realtimeData = new ProductionRealtimeData
            {
                DeviceId = 1,
                DeviceCode = "CNC001",
                DeviceName = "车床1号",
                Status = "Running",
                IsRunning = true,
                NCProgram = "O0001",
                CurrentCount = 1500,
                TodayQuantity = 50,
                LastUpdateTime = DateTime.Now
            };

            // Assert
            Assert.Equal(1, realtimeData.DeviceId);
            Assert.Equal("CNC001", realtimeData.DeviceCode);
            Assert.Equal("车床1号", realtimeData.DeviceName);
            Assert.Equal("Running", realtimeData.Status);
            Assert.True(realtimeData.IsRunning);
            Assert.Equal("O0001", realtimeData.NCProgram);
            Assert.Equal(1500, realtimeData.CurrentCount);
            Assert.Equal(50, realtimeData.TodayQuantity);
            Assert.NotNull(realtimeData.LastUpdateTime);
        }

        [Fact]
        public void User_CreatedWithValidData_HasCorrectProperties()
        {
            // Arrange & Act
            var user = new User
            {
                Username = "admin",
                PasswordHash = "hashed_password",
                RealName = "管理员",
                Email = "admin@example.com",
                Phone = "13800138000",
                RoleId = 1,
                IsActive = true,
                LastLoginTime = DateTime.Now
            };

            // Assert
            Assert.Equal("admin", user.Username);
            Assert.Equal("hashed_password", user.PasswordHash);
            Assert.Equal("管理员", user.RealName);
            Assert.Equal("admin@example.com", user.Email);
            Assert.Equal("13800138000", user.Phone);
            Assert.Equal(1, user.RoleId);
            Assert.True(user.IsActive);
            Assert.NotNull(user.LastLoginTime);
        }

        [Fact]
        public void SystemHealth_CreatedWithValidData_HasCorrectProperties()
        {
            // Arrange & Act
            var health = new SystemHealth
            {
                CheckTime = DateTime.Now,
                DatabaseConnected = true,
                OnlineDeviceCount = 10,
                TotalDeviceCount = 15,
                ActiveAlarmCount = 2,
                CollectionSuccessRate = 98.5,
                AverageResponseTime = 120.5,
                DatabaseSize = 1024000000,
                CpuUsage = 45.2,
                MemoryUsage = 78.3,
                DiskUsage = 65.8
            };

            // Assert
            Assert.True(health.DatabaseConnected);
            Assert.Equal(10, health.OnlineDeviceCount);
            Assert.Equal(15, health.TotalDeviceCount);
            Assert.Equal(2, health.ActiveAlarmCount);
            Assert.Equal(98.5, health.CollectionSuccessRate);
            Assert.Equal(120.5, health.AverageResponseTime);
            Assert.True(health.CpuUsage > 0);
            Assert.True(health.MemoryUsage > 0);
            Assert.True(health.DiskUsage > 0);
        }

        [Fact]
        public void Alarm_CreatedWithValidData_HasCorrectProperties()
        {
            // Arrange & Act
            var alarm = new Alarm
            {
                AlarmType = "设备离线",
                AlarmLevel = "高",
                AlarmContent = "设备连接超时",
                DeviceId = 1,
                DeviceName = "CNC001",
                IsResolved = false,
                OccurrenceTime = DateTime.Now
            };

            // Assert
            Assert.Equal("设备离线", alarm.AlarmType);
            Assert.Equal("高", alarm.AlarmLevel);
            Assert.Equal("设备连接超时", alarm.AlarmContent);
            Assert.Equal(1, alarm.DeviceId);
            Assert.Equal("CNC001", alarm.DeviceName);
            Assert.False(alarm.IsResolved);
            Assert.NotNull(alarm.OccurrenceTime);
        }
    }
}