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

namespace Monitoring.Infrastructure.Data.Configurations;

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

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

		builder
			.HasOne(x => x.ResetBy)
			.WithMany()
			.HasForeignKey(x => x.ResetById)
			.IsRequired()
			.OnDelete(DeleteBehavior.NoAction);

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

		builder.Property(x => x.Comment).IsRequired().HasMaxLength(Description.MaxLength);
	}
}
