using Microsoft.EntityFrameworkCore;

using Monitoring.Core.Domain.EventManagement;

namespace Monitoring.Infrastructure.Data.Configurations;

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

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

		_ = builder.Property(x => x.AreaIndex)
			.IsRequired()
			.HasConversion(x => x.Value, x => new(x));

		_ = builder.Property(x => x.ZoneIndex)
			.IsRequired()
			.HasConversion(x => x.Value, x => new(x));

		_ = builder.Property(x => x.CreatedAt)
			.IsRequired();

		_ = builder.Property(x => x.ReceivedAt)
			.IsRequired();

		_ = builder.HasOne(x => x.Asset)
			.WithMany(x => x.Events)
			.HasForeignKey(x => x.AssetId)
			.IsRequired()
			.OnDelete(DeleteBehavior.Cascade);
		_ = builder.Property(x => x.AssetId)
			.HasConversion(x => x.Value, x => new(x));
	}
}
