using Microsoft.EntityFrameworkCore;
using ROLAC.API.Entities.Logging;

namespace ROLAC.API.Data.Logging;

/// <summary>
/// Single source of truth for the SystemLog / AuditLog table schema. Applied by
/// <see cref="AppDbContext"/> (so the startup migration creates the tables) AND by
/// <see cref="LogDbContext"/> (so runtime reads/writes map to the same shape).
/// </summary>
public static class LogModelConfiguration
{
    public static void Configure(ModelBuilder builder)
    {
        builder.Entity<SystemLog>(entity =>
        {
            entity.ToTable("SystemLogs");
            entity.HasKey(e => e.Id);
            entity.Property(e => e.Level).HasConversion<byte>();
            entity.Property(e => e.Category).HasMaxLength(256).IsRequired();
            entity.Property(e => e.Message).IsRequired();          // text
            entity.Property(e => e.RequestPath).HasMaxLength(2048);
            entity.Property(e => e.HttpMethod).HasMaxLength(10);
            entity.Property(e => e.UserId).HasMaxLength(450);
            entity.Property(e => e.IpAddress).HasMaxLength(45);
            entity.Property(e => e.CorrelationId).HasMaxLength(64);

            entity.HasIndex(e => e.Timestamp);
            entity.HasIndex(e => e.Level);
            entity.HasIndex(e => new { e.Timestamp, e.Level });
            entity.HasIndex(e => e.UserId).HasFilter("\"UserId\" IS NOT NULL");
        });

        builder.Entity<AuditLog>(entity =>
        {
            entity.ToTable("AuditLogs");
            entity.HasKey(e => e.Id);
            entity.Property(e => e.Level).HasConversion<byte>();
            entity.Property(e => e.Action).HasMaxLength(40).IsRequired();
            entity.Property(e => e.Category).HasMaxLength(40).IsRequired();
            entity.Property(e => e.EntityName).HasMaxLength(128);
            entity.Property(e => e.EntityId).HasMaxLength(64);
            entity.Property(e => e.Changes).HasColumnType("jsonb");
            entity.Property(e => e.Summary).HasMaxLength(512);
            entity.Property(e => e.UserId).HasMaxLength(450);
            entity.Property(e => e.UserEmail).HasMaxLength(256);
            entity.Property(e => e.IpAddress).HasMaxLength(45);
            entity.Property(e => e.CorrelationId).HasMaxLength(64);

            entity.HasIndex(e => e.Timestamp);
            entity.HasIndex(e => new { e.Category, e.Timestamp });
            entity.HasIndex(e => new { e.EntityName, e.EntityId });
            entity.HasIndex(e => e.Action);
            entity.HasIndex(e => e.UserId).HasFilter("\"UserId\" IS NOT NULL");
        });
    }
}
