namespace SimpleLIS;

using Microsoft.EntityFrameworkCore;
using SimpleLIS.Models;

public class HL7DbContext : DbContext
{
#pragma warning disable CS8618
    public DbSet<Message> Messages { get; set; }
#pragma warning restore CS8618
#pragma warning disable CS8618
    public DbSet<Patient> Patients { get; set; }
#pragma warning restore CS8618
#pragma warning disable CS8618
    public DbSet<Observation> Observations { get; set; }
#pragma warning restore CS8618

    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
    {
        optionsBuilder.UseSqlite("Data Source=hl7.db");
    }

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        modelBuilder.Entity<Message>()
            .HasKey(m => m.MessageId);

        modelBuilder.Entity<Patient>()
            .HasKey(p => p.PatientId);

        modelBuilder.Entity<Observation>()
            .HasKey(o => o.ObservationId);

        modelBuilder.Entity<Message>()
            .HasMany(m => m.Observations)
            .WithOne(o => o.Message)
            .HasForeignKey(o => o.MessageId);

        modelBuilder.Entity<Message>()
            .HasOne(m => m.Patient)
            .WithMany(p => p.Messages)
            .HasForeignKey(m => m.PatientId)
            .OnDelete(DeleteBehavior.Cascade);

        modelBuilder.Entity<Observation>()
            .HasOne(o => o.Message)
            .WithMany(m => m.Observations)
            .HasForeignKey(o => o.MessageId)
            .OnDelete(DeleteBehavior.Cascade);
    }
}
