using Microsoft.EntityFrameworkCore;
using EonaCat.Logger.Server.Models;

namespace EonaCat.Logger.Server.Data;

public class LoggerDbContext : DbContext
{
    public LoggerDbContext(DbContextOptions<LoggerDbContext> options)
        : base(options) { }

    public DbSet<LogEntry> LogEntries { get; set; }
    public DbSet<Application> Applications { get; set; }

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        modelBuilder.Entity<LogEntry>(entity =>
        {
            entity.HasKey(e => e.Id);
            entity.HasIndex(e => e.Timestamp);
            entity.HasIndex(e => e.ApplicationName);
            entity.HasIndex(e => e.Level);
            entity.HasIndex(e => e.Environment);
            entity.Property(e => e.PropertiesJson).HasColumnType("TEXT");
        });

        modelBuilder.Entity<Application>(entity =>
        {
            entity.HasKey(e => e.Id);
            entity.HasIndex(e => e.Name).IsUnique();
        });
    }
}
