using Microsoft.EntityFrameworkCore;
using Monitoring.Core.Domain.CheckListManagement;

namespace Monitoring.Infrastructure.Data.Configurations;

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

		_ = builder
			.HasOne(x => x.Item)
			.WithMany(x => x.Translations)
			.HasForeignKey(x => x.ItemId)
			.IsRequired()
			.OnDelete(DeleteBehavior.Cascade);
		_ = builder.Property(x => x.ItemId).IsRequired().HasConversion(x => x.Value, x => new(x));

		_ = builder.Property(x => x.LanguageCode).IsRequired().HasMaxLength(10);
		_ = builder.Property(x => x.Name).IsRequired();
		_ = builder.Property(x => x.Description).IsRequired();

		_ = builder.HasIndex(x => new { x.ItemId, x.LanguageCode }).IsUnique();
	}
}
