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

namespace Monitoring.Infrastructure.Data.Configurations;

public class CameraViewConfiguration : IEntityTypeConfiguration<CameraView>
{
    public void Configure(EntityTypeBuilder<CameraView> 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()
            .HasForeignKey(x => x.UserId)
            .IsRequired()
            .OnDelete(DeleteBehavior.NoAction);
        builder.Property(x => x.UserId)
            .HasConversion(x => x.Value, x => new(x));

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

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

        builder.Property(x => x.DateTime).IsRequired();
        builder.Property(x => x.StartVideo).IsRequired();
        builder.Property(x => x.EndVideo).IsRequired();

        builder.HasIndex(x => new { x.UserId, x.DateTime });
        builder.HasIndex(x => new { x.CameraId, x.DateTime });
        builder.HasIndex(x => x.AlarmId);
    }
}