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

namespace Monitoring.Infrastructure.Data.Configurations;

public class AlarmConfiguration : IEntityTypeConfiguration<Alarm>
{
	public void Configure(
		Microsoft.EntityFrameworkCore.Metadata.Builders.EntityTypeBuilder<Alarm> builder
	)
	{
		_ = builder.HasKey(x => x.Id);
		_ = builder.Property(x => x.Id).IsRequired().HasConversion(x => x.Value, x => new(x));

		_ = builder
			.HasOne(x => x.Event)
			.WithMany(x => x.Alarms)
			.HasForeignKey(x => x.EventId)
			.IsRequired()
			.OnDelete(DeleteBehavior.NoAction);
		_ = builder.Property(x => x.EventId).HasConversion(x => x.Value, x => new(x));

		_ = builder
			.HasOne(x => x.Zone)
			.WithMany(x => x.Alarms)
			.HasForeignKey(x => x.ZoneId)
			.IsRequired()
			.OnDelete(DeleteBehavior.NoAction);
		_ = builder.Property(x => x.ZoneId).HasConversion(x => x.Value, x => new(x));

		_ = builder
			.HasOne(x => x.Scope)
			.WithMany(x => x.Alarms)
			.HasForeignKey(x => x.ScopeId)
			.IsRequired(false)
			.OnDelete(DeleteBehavior.NoAction);
		_ = builder.Property(x => x.ScopeId).HasConversion(x => x.Value, x => new(x));
	}
}
