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

namespace Tiku.Infrastructure.Persistence.Configurations;

internal sealed class RegionConfiguration : IEntityTypeConfiguration<Region>
{
    public void Configure(EntityTypeBuilder<Region> builder)
    {
        builder.ConfigureTenantEntity("regions");
        builder.ConfigureTimestamps();
        builder.Property(entity => entity.LegacyId).HasMaxLength(64);
        builder.Property(entity => entity.Name).HasMaxLength(200);
        builder.Property(entity => entity.Code).HasMaxLength(50);
        builder.Property(entity => entity.ShortName).HasMaxLength(100);
        builder.Property(entity => entity.FullName).HasMaxLength(300);
        builder.Property(entity => entity.Icon).HasMaxLength(2048);
        builder.Property(entity => entity.Pinyin).HasMaxLength(255);
        builder.Property(entity => entity.Config).IsJson("{}");
        builder.HasIndex(entity => new { entity.TenantId, entity.LegacyId }).IsUnique();
    }
}

internal sealed class RegionModuleConfiguration : IEntityTypeConfiguration<RegionModule>
{
    public void Configure(EntityTypeBuilder<RegionModule> builder)
    {
        builder.ConfigureTenantEntity("region_modules");
        builder.ConfigureTimestamps();
        builder.Property(entity => entity.LegacyId).HasMaxLength(64);
        builder.Property(entity => entity.Name).HasMaxLength(200);
        builder.Property(entity => entity.Type).HasMaxLength(50);
        builder.Property(entity => entity.Icon).HasMaxLength(2048);
        builder.Property(entity => entity.Color).HasMaxLength(50);
        builder.Property(entity => entity.TextColor).HasMaxLength(50);
        builder.Property(entity => entity.Route).HasMaxLength(500);
        builder.HasIndex(entity => new { entity.TenantId, entity.LegacyId }).IsUnique();

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

internal sealed class ModuleNodeConfiguration : IEntityTypeConfiguration<ModuleNode>
{
    public void Configure(EntityTypeBuilder<ModuleNode> builder)
    {
        builder.ConfigureTenantEntity("module_nodes");
        builder.ConfigureTimestamps();
        builder.Property(entity => entity.LegacyId).HasMaxLength(64);
        builder.Property(entity => entity.LegacyParentId).HasMaxLength(64);
        builder.Property(entity => entity.LegacyModuleId).HasMaxLength(64);
        builder.Property(entity => entity.Type).HasSnakeCaseEnum();
        builder.Property(entity => entity.Name).HasMaxLength(300);
        builder.Property(entity => entity.Path).HasMaxLength(1000);
        builder.Property(entity => entity.Metadata).IsJson("{}");
        builder.HasIndex(entity => new { entity.TenantId, entity.LegacyId }).IsUnique();

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

internal sealed class SchoolConfiguration : IEntityTypeConfiguration<School>
{
    public void Configure(EntityTypeBuilder<School> builder)
    {
        builder.ConfigureTenantEntity("schools");
        builder.ConfigureTimestamps();
        builder.Property(entity => entity.LegacyId).HasMaxLength(64);
        builder.Property(entity => entity.Name).HasMaxLength(300);
        builder.Property(entity => entity.ProfessionalExamDate).HasMaxLength(255);
        builder.Property(entity => entity.Metadata).IsJson("{}");
        builder.HasIndex(entity => new { entity.TenantId, entity.LegacyId }).IsUnique();

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

internal sealed class MajorConfiguration : IEntityTypeConfiguration<Major>
{
    public void Configure(EntityTypeBuilder<Major> builder)
    {
        builder.ConfigureTenantEntity("majors");
        builder.ConfigureTimestamps();
        builder.Property(entity => entity.LegacyId).HasMaxLength(64);
        builder.Property(entity => entity.Name).HasMaxLength(300);
        builder.HasIndex(entity => new { entity.TenantId, entity.LegacyId }).IsUnique();

        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);
    }
}

internal sealed class SubjectConfiguration : IEntityTypeConfiguration<Subject>
{
    public void Configure(EntityTypeBuilder<Subject> builder)
    {
        builder.ConfigureTenantEntity("subjects");
        builder.ConfigureTimestamps();
        builder.Property(entity => entity.LegacyId).HasMaxLength(64);
        builder.Property(entity => entity.Name).HasMaxLength(300);
        builder.Property(entity => entity.Type).HasNullableSnakeCaseEnum();
        builder.Property(entity => entity.MajorLegacyIds).IsJson("[]");
        builder.Property(entity => entity.Icon).HasMaxLength(2048);
        builder.Property(entity => entity.Stats).IsJson("{}");
        builder.HasIndex(entity => new { entity.TenantId, entity.LegacyId }).IsUnique();

        builder.HasOne<Region>().WithMany()
            .HasForeignKey(entity => new { entity.TenantId, entity.RegionId })
            .HasPrincipalKey(entity => new { entity.TenantId, entity.Id })
            .OnDelete(DeleteBehavior.Restrict);
        builder.HasOne<RegionModule>().WithMany()
            .HasForeignKey(entity => new { entity.TenantId, entity.ModuleId })
            .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);
        builder.HasOne<ModuleNode>().WithMany()
            .HasForeignKey(entity => new { entity.TenantId, entity.NodeId })
            .HasPrincipalKey(entity => new { entity.TenantId, entity.Id })
            .OnDelete(DeleteBehavior.Restrict);
    }
}

internal sealed class CategoryConfiguration : IEntityTypeConfiguration<Category>
{
    public void Configure(EntityTypeBuilder<Category> builder)
    {
        builder.ConfigureTenantEntity("categories");
        builder.ConfigureTimestamps();
        builder.Property(entity => entity.LegacyId).HasMaxLength(64);
        builder.Property(entity => entity.Name).HasMaxLength(300);
        builder.Property(entity => entity.CategoryType).HasNullableSnakeCaseEnum();
        builder.HasIndex(entity => new { entity.TenantId, entity.LegacyId }).IsUnique();

        builder.HasOne<Subject>().WithMany()
            .HasForeignKey(entity => new { entity.TenantId, entity.SubjectId })
            .HasPrincipalKey(entity => new { entity.TenantId, entity.Id })
            .OnDelete(DeleteBehavior.Restrict);
        builder.HasOne<ModuleNode>().WithMany()
            .HasForeignKey(entity => new { entity.TenantId, entity.NodeId })
            .HasPrincipalKey(entity => new { entity.TenantId, entity.Id })
            .OnDelete(DeleteBehavior.Restrict);
    }
}