using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
using Tiku.Domain.Import;
using Tiku.Domain.Tenancy;

namespace Tiku.Infrastructure.Persistence.Configurations;

internal sealed class PocketBaseImportRunConfiguration : IEntityTypeConfiguration<PocketBaseImportRun>
{
    public void Configure(EntityTypeBuilder<PocketBaseImportRun> builder)
    {
        builder.ConfigureEntity("pb_import_runs");
        builder.HasAlternateKey(entity => new { entity.TenantId, entity.Id });
        builder.Property(entity => entity.SourceName).HasMaxLength(500);
        builder.Property(entity => entity.SourceKind).HasSnakeCaseEnum();
        builder.Property(entity => entity.Status).HasSnakeCaseEnum();
        builder.Property(entity => entity.Stats).IsJson("{}");
        builder.Property(entity => entity.StartedAt).HasDefaultValueSql("now()");
        builder.HasIndex(entity => new { entity.TenantId, entity.Status, entity.StartedAt });
        builder.HasOne<Tenant>().WithMany().HasForeignKey(entity => entity.TenantId).OnDelete(DeleteBehavior.Cascade);
    }
}

internal sealed class PocketBaseRawRecordConfiguration : IEntityTypeConfiguration<PocketBaseRawRecord>
{
    public void Configure(EntityTypeBuilder<PocketBaseRawRecord> builder)
    {
        builder.ToTable("pb_raw_records");
        builder.HasKey(entity => new { entity.RunId, entity.CollectionName, entity.LegacyId });
        builder.Property(entity => entity.CollectionName).HasMaxLength(100);
        builder.Property(entity => entity.LegacyId).HasMaxLength(100);
        builder.Property(entity => entity.Record).IsJson("{}");
        builder.Property(entity => entity.Errors).IsJson("[]");
        builder.Property(entity => entity.ImportedAt).HasDefaultValueSql("now()");
        builder.HasIndex(entity => new { entity.TenantId, entity.CollectionName, entity.LegacyId });
        builder.HasOne<PocketBaseImportRun>().WithMany()
            .HasForeignKey(entity => new { entity.TenantId, entity.RunId })
            .HasPrincipalKey(entity => new { entity.TenantId, entity.Id })
            .OnDelete(DeleteBehavior.Cascade);
        builder.HasOne<Tenant>().WithMany().HasForeignKey(entity => entity.TenantId).OnDelete(DeleteBehavior.Cascade);
    }
}

internal sealed class PocketBaseImportIssueConfiguration : IEntityTypeConfiguration<PocketBaseImportIssue>
{
    public void Configure(EntityTypeBuilder<PocketBaseImportIssue> builder)
    {
        builder.ConfigureEntity("pb_import_issues");
        builder.HasAlternateKey(entity => new { entity.TenantId, entity.Id });
        builder.Property(entity => entity.CollectionName).HasMaxLength(100);
        builder.Property(entity => entity.LegacyId).HasMaxLength(100);
        builder.Property(entity => entity.Severity).HasSnakeCaseEnum();
        builder.Property(entity => entity.IssueCode).HasMaxLength(100);
        builder.Property(entity => entity.FieldPath).HasMaxLength(500);
        builder.Property(entity => entity.RawValueSample).HasMaxLength(2000);
        builder.Property(entity => entity.CreatedAt).HasDefaultValueSql("now()");
        builder.HasIndex(entity => new { entity.RunId, entity.Severity, entity.CollectionName });
        builder.HasIndex(entity => new { entity.TenantId, entity.CollectionName, entity.LegacyId });
        builder.HasOne<PocketBaseImportRun>().WithMany()
            .HasForeignKey(entity => new { entity.TenantId, entity.RunId })
            .HasPrincipalKey(entity => new { entity.TenantId, entity.Id })
            .OnDelete(DeleteBehavior.Cascade);
        builder.HasOne<Tenant>().WithMany().HasForeignKey(entity => entity.TenantId).OnDelete(DeleteBehavior.Cascade);
    }
}
