// src/Monitoring/Monitoring.Infrastructure/Data/Configurations/AssetStatusConfiguration.cs
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
using Monitoring.Core.Domain.AssetStatus;
using Monitoring.Core.Domain.AssetManagement;

namespace Monitoring.Infrastructure.Data.Configurations;

/// <summary>
/// Конфигурация для сущности AssetStatus
/// </summary>
public class AssetStatusConfiguration : IEntityTypeConfiguration<AssetStatus>
{
    public void Configure(EntityTypeBuilder<AssetStatus> builder)
    {
        // Первичный ключ
        builder.HasKey(x => x.Id);
        builder.Property(x => x.Id)
            .HasConversion(x => x.Value, x => new AssetStatusId(x))
            .ValueGeneratedNever()
            .IsRequired();
        
        // Настройка внешнего ключа
        builder.Property(x => x.AssetId)
            .HasConversion(x => x.Value, x => new AssetId(x))
            .IsRequired();

        builder.HasOne<Asset>()
            .WithMany()
            .HasForeignKey(x => x.AssetId)
            .OnDelete(DeleteBehavior.Cascade);

        builder.HasIndex(x => x.AssetId);
        builder.HasIndex(x => x.Timestamp);
        builder.HasIndex(x => new { x.AssetId, x.Timestamp });

        builder.Property(x => x.StatusChangeReason)
            .HasMaxLength(500)
            .IsRequired();

        builder.Property(x => x.Timestamp)
            .IsRequired();

        builder.Property(x => x.IsOnline)
            .IsRequired();

        builder.ToTable("AssetStatuses");
    }
}