using Lada.Framework.Data.Domains.ErrorReports;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;

namespace Lada.Framework.Data.Mappings.EntityMappings.ErrorReports
{
    public class ErrorReportMap : IEntityMappingConfiguration<ErrorReport>
    {
        public void Map(EntityTypeBuilder<ErrorReport> builder)
        {
            builder.HasKey(er => er.Id);
            builder.Property(er => er.Id).UseIdentityColumn(1);

            // Cấu hình các thuộc tính
            builder.Property(er => er.Url)
                .HasMaxLength(2048)
                .IsRequired();

            builder.Property(er => er.ErrorDescription)
                .HasMaxLength(2000)
                .IsRequired();

            builder.Property(er => er.AdminNote)
                .HasMaxLength(1000);

            builder.Property(er => er.ReporterName)
                .HasMaxLength(255);

            builder.Property(er => er.ReporterEmail)
                .HasMaxLength(255);

            builder.Property(er => er.ReporterPhone)
                .HasMaxLength(20);

            builder.Property(er => er.BrowserInfo)
                .HasMaxLength(500);

            builder.Property(er => er.DeviceInfo)
                .HasMaxLength(500);

            // Quan hệ với ErrorReportMedia (1-nhiều)
            builder.HasMany(er => er.ErrorReportMedias)
                .WithOne(erm => erm.ErrorReport)
                .HasForeignKey(erm => erm.ErrorReportId)
                .OnDelete(DeleteBehavior.Cascade);

            // Index
            builder.HasIndex(er => er.Status);
            builder.HasIndex(er => er.CreatedTime);
            builder.HasIndex(er => er.ReportedByUserId);

            builder.ToTable("ErrorReport");
        }
    }
}
