using Microsoft.EntityFrameworkCore;

using Monitoring.Core.Domain.EventManagement;

namespace Monitoring.Infrastructure.Data.Configurations;

public class FaultConfiguration : IEntityTypeConfiguration<Fault>
{
	public void Configure(Microsoft.EntityFrameworkCore.Metadata.Builders.EntityTypeBuilder<Fault> 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.Faults)
			.HasForeignKey(x => x.EventId)
			.IsRequired()
			.OnDelete(DeleteBehavior.Cascade);
		_ = builder.Property(x => x.EventId)
			.HasConversion(x => x.Value, x => new(x));

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