using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;

using Monitoring.Core.Domain;
using Monitoring.Core.Domain.EventManagement;

namespace Monitoring.Infrastructure.Data.Configurations;

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

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

		_ = builder.HasOne(x => x.EventCode)
			.WithMany()
			.HasForeignKey(x => x.EventCodeId)
			.IsRequired()
			.OnDelete(DeleteBehavior.Cascade);
		_ = builder.Property(x => x.EventCodeId)
			.HasConversion(x => x.Value, x => new(x));

		_ = builder.HasIndex(x => new
		{
			x.AssetId,
			x.EventCodeId,
		})
			.IsUnique();
	}
}
