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

using Monitoring.Core.Domain;

namespace Monitoring.Infrastructure.Data.Configurations;

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

		_ = builder.HasIndex(x => new { x.MemberId, x.AssetGroupId })
			.IsUnique();

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

		_ = builder.HasOne(x => x.Member)
			.WithMany(x => x.Accesses)
			.HasForeignKey(x => x.MemberId)
			.IsRequired()
			.OnDelete(DeleteBehavior.Cascade);
		_ = builder.Property(x => x.MemberId)
			.HasConversion(x => x.Value, x => new(x));
	}
}
