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

namespace Monitoring.Infrastructure.Data.Configurations;

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

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

		builder
			.HasOne(x => x.Sender)
			.WithMany()
			.HasForeignKey(x => x.SenderId)
			.IsRequired()
			.OnDelete(DeleteBehavior.NoAction);

		builder.Property(x => x.SentAt).IsRequired();

		// FireDepartment is in another bounded context; map FK only
		builder
			.Property(x => x.DepartmentId)
			.IsRequired()
			.HasConversion(x => x.Value, x => new(x));

		builder
			.Property(x => x.DispatcherName)
			.IsRequired()
			.HasConversion(x => x.Value, x => Name.Create(x))
			.HasMaxLength(Name.MaxLength);

		builder
			.Property(x => x.Comment)
			.IsRequired()
			.HasConversion(x => x.Value, x => Description.Create(x))
			.HasMaxLength(Description.MaxLength);
	}
}
