using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
using Monitoring.Core.Domain.AssetManagement;
using Monitoring.Core.Domain.CameraManagement;

namespace Monitoring.Infrastructure.Data.Configurations;

public class CameraConfiguration : IEntityTypeConfiguration<Camera>
{
    public void Configure(EntityTypeBuilder<Camera> 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()
            .HasMaxLength(CameraName.MaxLength)
            .HasConversion(x => x.Value, x => CameraName.Create(x));

        builder.Property(x => x.Address)
            .IsRequired()
            .HasMaxLength(CameraAddress.MaxLength)
            .HasConversion(x => x.Value, x => CameraAddress.Create(x));

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

        builder.HasIndex(x => x.AssetId);
    }
}