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

using Monitoring.Core.Domain.CallManagement;

namespace Monitoring.Infrastructure.Data.Configurations;

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

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

		builder
			.HasOne(x => x.Caller)
			.WithMany()
			.HasForeignKey(x => x.CallerId)
			.IsRequired()
			.OnDelete(DeleteBehavior.NoAction);

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

		builder
			.HasOne(x => x.ResponsiblePerson)
			.WithMany()
			.HasForeignKey(x => x.ResponsiblePersonId)
			.IsRequired()
			.OnDelete(DeleteBehavior.NoAction);

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

		builder
			.Property(x => x.Commentary)
			.IsRequired()
			.HasConversion(x => x.Value, x => CallCommentary.Create(x));
	}
}
