using Microsoft.EntityFrameworkCore;

using Monitoring.Core.Domain;

namespace Monitoring.Infrastructure.Data.Configurations;

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

		_ = builder.HasIndex(x => new { x.OrganizationId, x.UserId })
			.IsUnique();

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

		_ = builder.HasOne(x => x.User)
			.WithMany(x => x.Members)
			.HasForeignKey(x => x.UserId)
			.IsRequired()
			.OnDelete(DeleteBehavior.Cascade);
		_ = builder.Property(x => x.UserId)
			.HasConversion(x => x.Value, x => new(x));
	}
}
