using Microsoft.EntityFrameworkCore;

using Monitoring.Core.Domain.ReportManagement;

namespace Monitoring.Infrastructure.Data.Configurations;

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

		_ = builder.Property(x => x.Name)
			.IsRequired()
			.HasConversion(x => x.Value, x => ReportName.Create(x));

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

		_ = builder.Property(x => x.Data)
			.IsRequired()
			.HasColumnType("jsonb");

		_ = builder.HasOne(x => x.User)
			.WithMany(x => x.Reports)
			.HasForeignKey(x => x.UserId)
			.IsRequired()
			.OnDelete(DeleteBehavior.Cascade);

		_ = builder.Property(x => x.Type)
			.IsRequired();
	}
}
