using Microsoft.EntityFrameworkCore;
using RolemasterDb.App.Domain;

namespace RolemasterDb.App.Data;

public sealed class RolemasterDbContext(DbContextOptions<RolemasterDbContext> options) : DbContext(options)
{
    public DbSet<ArmorType> ArmorTypes => Set<ArmorType>();
    public DbSet<AttackTable> AttackTables => Set<AttackTable>();
    public DbSet<AttackRollBand> AttackRollBands => Set<AttackRollBand>();
    public DbSet<AttackResult> AttackResults => Set<AttackResult>();
    public DbSet<CriticalTable> CriticalTables => Set<CriticalTable>();
    public DbSet<CriticalGroup> CriticalGroups => Set<CriticalGroup>();
    public DbSet<CriticalColumn> CriticalColumns => Set<CriticalColumn>();
    public DbSet<CriticalRollBand> CriticalRollBands => Set<CriticalRollBand>();
    public DbSet<CriticalResult> CriticalResults => Set<CriticalResult>();
    public DbSet<CriticalBranch> CriticalBranches => Set<CriticalBranch>();
    public DbSet<CriticalEffect> CriticalEffects => Set<CriticalEffect>();

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        modelBuilder.Entity<ArmorType>(entity =>
        {
            entity.HasIndex(item => item.Code).IsUnique();
            entity.Property(item => item.Code).HasMaxLength(32);
            entity.Property(item => item.Label).HasMaxLength(128);
        });

        modelBuilder.Entity<AttackTable>(entity =>
        {
            entity.HasIndex(item => item.Slug).IsUnique();
            entity.Property(item => item.Slug).HasMaxLength(64);
            entity.Property(item => item.DisplayName).HasMaxLength(128);
            entity.Property(item => item.AttackKind).HasMaxLength(32);
        });

        modelBuilder.Entity<AttackRollBand>(entity =>
        {
            entity.HasIndex(item => new { item.AttackTableId, item.Label }).IsUnique();
            entity.HasIndex(item => new { item.AttackTableId, item.MinRoll, item.MaxRoll });
        });

        modelBuilder.Entity<AttackResult>(entity =>
        {
            entity.HasIndex(item => new { item.AttackTableId, item.ArmorTypeId, item.AttackRollBandId }).IsUnique();
            entity.Property(item => item.CriticalType).HasMaxLength(64);
            entity.Property(item => item.CriticalSeverity).HasMaxLength(8);
            entity.Property(item => item.RawNotation).HasMaxLength(256);
        });

        modelBuilder.Entity<CriticalTable>(entity =>
        {
            entity.HasIndex(item => item.Slug).IsUnique();
            entity.Property(item => item.Slug).HasMaxLength(64);
            entity.Property(item => item.DisplayName).HasMaxLength(128);
            entity.Property(item => item.Family).HasMaxLength(32);
        });

        modelBuilder.Entity<CriticalGroup>(entity =>
        {
            entity.HasIndex(item => new { item.CriticalTableId, item.GroupKey }).IsUnique();
            entity.Property(item => item.GroupKey).HasMaxLength(64);
        });

        modelBuilder.Entity<CriticalColumn>(entity =>
        {
            entity.HasIndex(item => new { item.CriticalTableId, item.ColumnKey }).IsUnique();
            entity.Property(item => item.ColumnKey).HasMaxLength(64);
            entity.Property(item => item.Role).HasMaxLength(32);
        });

        modelBuilder.Entity<CriticalRollBand>(entity =>
        {
            entity.HasIndex(item => new { item.CriticalTableId, item.Label }).IsUnique();
            entity.HasIndex(item => new { item.CriticalTableId, item.MinRoll, item.MaxRoll });
        });

        modelBuilder.Entity<CriticalResult>(entity =>
        {
            entity.HasIndex(item => new { item.CriticalTableId, item.CriticalGroupId, item.CriticalColumnId, item.CriticalRollBandId }).IsUnique();
            entity.Property(item => item.ParseStatus).HasMaxLength(32);
            entity.Property(item => item.SourceImagePath).HasMaxLength(512);
        });

        modelBuilder.Entity<CriticalBranch>(entity =>
        {
            entity.HasIndex(item => item.CriticalResultId);
            entity.HasIndex(item => new { item.CriticalResultId, item.SortOrder });
            entity.Property(item => item.BranchKind).HasMaxLength(32);
            entity.Property(item => item.ConditionKey).HasMaxLength(128);
        });

        modelBuilder.Entity<CriticalEffect>(entity =>
        {
            entity.HasIndex(item => item.EffectCode);
            entity.HasIndex(item => item.CriticalResultId);
            entity.HasIndex(item => item.CriticalBranchId);
            entity.Property(item => item.EffectCode).HasMaxLength(64);
            entity.Property(item => item.Target).HasMaxLength(32);
            entity.Property(item => item.ValueExpression).HasMaxLength(128);
            entity.Property(item => item.BodyPart).HasMaxLength(64);
            entity.Property(item => item.SourceType).HasMaxLength(32);
        });
    }
}
