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

namespace Monitoring.Infrastructure.Data.Configurations;

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

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

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

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