using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
using Monitoring.Core.Domain.ServiceOrganizationManagement;

namespace Monitoring.Infrastructure.Data.Configurations;

public class ServiceOrganizationConfiguration : IEntityTypeConfiguration<ServiceOrganization>
{
    public void Configure(EntityTypeBuilder<ServiceOrganization> builder)
    {
        builder.ToTable("ServiceOrganizations");

        builder.HasKey(x => x.Id);
        builder.Property(x => x.Id)
            .IsRequired()
            .HasConversion(x => x.Value, x => new ServiceOrganizationId(x));

        builder.OwnsOne(
            x => x.Name,
            nameBuilder =>
            {
                nameBuilder.Property(n => n.Value)
                    .HasColumnName("Name")
                    .HasMaxLength(ServiceOrganizationName.MaxLength)
                    .IsRequired();
                nameBuilder.HasIndex(n => n.Value)
                    .IsUnique();
            });

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

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

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

        builder.Property(x => x.Id)
        .IsRequired()
        .HasConversion(x => x.Value, x => new ServiceOrganizationId(x));
    }
}