using Microsoft.EntityFrameworkCore;

using Monitoring.Core.Domain.UserLogManagement;

namespace Monitoring.Infrastructure.Data.Configurations;

public class UserLogConfiguration : IEntityTypeConfiguration<UserLog>
{
	public void Configure(Microsoft.EntityFrameworkCore.Metadata.Builders.EntityTypeBuilder<UserLog> 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(x => x.Logs)
			.IsRequired()
			.HasForeignKey(x => x.UserId)
			.OnDelete(DeleteBehavior.NoAction);
		_ = builder.Property(x => x.UserId)
			.HasConversion(x => x.Value, x => new(x));

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

		_ = builder.Property(x => x.CreatedAt)
			.IsRequired();
		_ = builder.HasIndex(x => x.CreatedAt);

		_ = builder.Property(x => x.Information)
			.IsRequired()
			.HasMaxLength(UserLogInformation.MaxLength)
			.HasConversion(x => x.Value, x => UserLogInformation.Create(x));
	}
}
