using Microsoft.EntityFrameworkCore;

using Monitoring.Core.Domain;
using Monitoring.Core.Domain.AssetManagement;

namespace Monitoring.Infrastructure.Data.Configurations;

public class ResponsiblePersonConfiguration : IEntityTypeConfiguration<ResponsiblePerson>
{
	public void Configure(Microsoft.EntityFrameworkCore.Metadata.Builders.EntityTypeBuilder<ResponsiblePerson> builder)
	{
		_ = builder.Property(x => x.Name)
			.IsRequired()
			.HasMaxLength(AssetName.MaxLength);

		_ = builder.Property(x => x.Key)
			.IsRequired()
			.HasMaxLength(AssetName.MaxLength);

		_ = builder.Property(x => x.Phone1)
			.IsRequired()
			.HasMaxLength(AssetName.MaxLength);

		_ = builder.Property(x => x.Phone2)
			.IsRequired()
			.HasMaxLength(AssetName.MaxLength);

		_ = builder.Property(x => x.Note)
			.IsRequired()
			.HasMaxLength(AssetDescription.MaxLength);

		_ = builder.HasOne(x => x.Asset)
			.WithMany(x => x.ResponsiblePersons)
			.HasForeignKey(x => x.AssetId)
			.IsRequired()
			.OnDelete(DeleteBehavior.Cascade);
		_ = builder.Property(x => x.AssetId)
			.HasConversion(x => x.Value, x => new(x));
	}
}
