using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
using Tiku.Domain.Catalog;

namespace Tiku.Infrastructure.Persistence.Configurations;

internal sealed class ScorelineFieldConfiguration : IEntityTypeConfiguration<ScorelineField>
{
    public void Configure(EntityTypeBuilder<ScorelineField> builder)
    {
        builder.ConfigureTenantEntity("scoreline_fields");
        builder.ConfigureTimestamps();
        builder.Property(entity => entity.LegacyId).HasMaxLength(64);
        builder.Property(entity => entity.FieldKey).HasMaxLength(64);
        builder.Property(entity => entity.FieldName).HasMaxLength(200);
        builder.Property(entity => entity.FieldType).HasMaxLength(32);
        builder.Property(entity => entity.Unit).HasMaxLength(32);
        builder.Property(entity => entity.Options).IsJson("[]");
        builder.Property(entity => entity.Placeholder).HasMaxLength(200);
        builder.Property(entity => entity.Description).HasMaxLength(1000);
        builder.HasIndex(entity => new { entity.TenantId, entity.LegacyId }).IsUnique();
        builder.HasIndex(entity => new { entity.TenantId, entity.RegionId, entity.FieldKey }).IsUnique();
        builder.HasIndex(entity => new { entity.TenantId, entity.RegionId, entity.IsFilter, entity.SortOrder });

        builder.HasOne<Region>()
            .WithMany()
            .HasForeignKey(entity => new { entity.TenantId, entity.RegionId })
            .HasPrincipalKey(entity => new { entity.TenantId, entity.Id })
            .OnDelete(DeleteBehavior.Restrict);
    }
}

internal sealed class ScorelineRecordConfiguration : IEntityTypeConfiguration<ScorelineRecord>
{
    public void Configure(EntityTypeBuilder<ScorelineRecord> builder)
    {
        builder.ConfigureTenantEntity("scoreline_records");
        builder.ConfigureTimestamps();
        builder.Property(entity => entity.LegacyId).HasMaxLength(64);
        builder.Property(entity => entity.SchoolName).HasMaxLength(300);
        builder.Property(entity => entity.MajorName).HasMaxLength(300);
        builder.Property(entity => entity.FieldValues).IsJson("{}");
        builder.HasIndex(entity => new { entity.TenantId, entity.LegacyId }).IsUnique();
        builder.HasIndex(entity => new { entity.TenantId, entity.RegionId, entity.SchoolId, entity.MajorId, entity.Year });
        builder.HasIndex(entity => new { entity.TenantId, entity.Year });
        builder.HasIndex(entity => new { entity.TenantId, entity.Year, entity.SchoolName, entity.MajorName, entity.Id })
            .IsDescending(false, true, false, false, false);

        builder.HasOne<Region>()
            .WithMany()
            .HasForeignKey(entity => new { entity.TenantId, entity.RegionId })
            .HasPrincipalKey(entity => new { entity.TenantId, entity.Id })
            .OnDelete(DeleteBehavior.Restrict);
        builder.HasOne<School>()
            .WithMany()
            .HasForeignKey(entity => new { entity.TenantId, entity.SchoolId })
            .HasPrincipalKey(entity => new { entity.TenantId, entity.Id })
            .OnDelete(DeleteBehavior.Restrict);
        builder.HasOne<Major>()
            .WithMany()
            .HasForeignKey(entity => new { entity.TenantId, entity.MajorId })
            .HasPrincipalKey(entity => new { entity.TenantId, entity.Id })
            .OnDelete(DeleteBehavior.Restrict);
    }
}
