using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
using IamService.Domain.AggregatesModel.ComplianceAggregate;

namespace IamService.Infrastructure.EntityConfigurations;

public class ComplianceReportEntityConfiguration : IEntityTypeConfiguration<ComplianceReport>
{
    public void Configure(EntityTypeBuilder<ComplianceReport> builder)
    {
        builder.ToTable("ComplianceReports");
        builder.HasKey(x => x.Id);
        builder.Property(x => x.Id).ValueGeneratedNever();

        builder.Property<string>("_name").HasColumnName("Name").HasMaxLength(200).IsRequired();
        builder.Property<Guid>("_generatedByUserId").HasColumnName("GeneratedByUserId").IsRequired();
        builder.Property<DateTime>("_createdAt").HasColumnName("CreatedAt").IsRequired();
        builder.Property<DateTime?>("_completedAt").HasColumnName("CompletedAt");
        builder.Property<string?>("_summary").HasColumnName("Summary").HasMaxLength(4000);
        builder.Property<int>("_totalChecks").HasColumnName("TotalChecks");
        builder.Property<int>("_passedChecks").HasColumnName("PassedChecks");
        builder.Property<int>("_failedChecks").HasColumnName("FailedChecks");

        builder.Property<ComplianceReportType>("_reportType")
            .HasColumnName("ReportTypeId")
            .HasConversion(v => v.Id, v => ComplianceReportType.FromId(v) ?? ComplianceReportType.GDPR);

        builder.Property<ComplianceReportStatus>("_status")
            .HasColumnName("StatusId")
            .HasConversion(v => v.Id, v => ComplianceReportStatus.FromId(v) ?? ComplianceReportStatus.Pending);

        builder.HasMany(x => x.Violations).WithOne().HasForeignKey("_reportId").OnDelete(DeleteBehavior.Cascade);
        builder.Ignore(x => x.DomainEvents);
        builder.HasIndex("_createdAt").HasDatabaseName("IX_ComplianceReports_CreatedAt");
    }
}

public class ComplianceViolationEntityConfiguration : IEntityTypeConfiguration<ComplianceViolation>
{
    public void Configure(EntityTypeBuilder<ComplianceViolation> builder)
    {
        builder.ToTable("ComplianceViolations");
        builder.HasKey(x => x.Id);
        builder.Property(x => x.Id).ValueGeneratedNever();

        builder.Property<Guid>("_reportId").HasColumnName("ReportId").IsRequired();
        builder.Property<string>("_rule").HasColumnName("Rule").HasMaxLength(200).IsRequired();
        builder.Property<string>("_description").HasColumnName("Description").HasMaxLength(1000).IsRequired();
        builder.Property<string?>("_remediation").HasColumnName("Remediation").HasMaxLength(1000);
        builder.Property<string?>("_affectedResource").HasColumnName("AffectedResource").HasMaxLength(200);
        builder.Property<bool>("_resolved").HasColumnName("Resolved");
        builder.Property<DateTime?>("_resolvedAt").HasColumnName("ResolvedAt");

        builder.Property<ViolationSeverity>("_severity")
            .HasColumnName("SeverityId")
            .HasConversion(v => v.Id, v => ViolationSeverity.FromId(v) ?? ViolationSeverity.Medium);

        builder.Ignore(x => x.DomainEvents);
    }
}
