using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
using Monitoring.Core.Domain.EventManagement;

namespace Monitoring.Infrastructure.Data.Configurations;

public class AlarmScopeStatusChangingConfiguration
	: IEntityTypeConfiguration<AlarmScopeStatusChanging>
{
	public void Configure(EntityTypeBuilder<AlarmScopeStatusChanging> builder)
	{
		builder.HasKey(x => x.Id);
		builder.Property(x => x.Id).IsRequired().HasConversion(x => x.Value, x => new(x));

		builder
			.HasOne(x => x.AlarmScope)
			.WithMany(x => x.StatusHistory)
			.HasForeignKey(x => x.AlarmScopeId)
			.IsRequired()
			.OnDelete(DeleteBehavior.NoAction);
		builder.Property(x => x.AlarmScopeId).HasConversion(x => x.Value, x => new(x)).IsRequired();

		builder
			.HasOne(x => x.User)
			.WithMany()
			.HasForeignKey(x => x.UserId)
			.OnDelete(DeleteBehavior.NoAction);
		builder.Property(x => x.UserId).HasConversion(x => x!.Value, x => new(x)).IsRequired(false);

		builder.Property(x => x.CreatedAt).IsRequired();
		builder.Property(x => x.Status).IsRequired();
	}
}
