using System.Linq;
using Application;
using Domain;
using Infrastructure.EntityTypes;
using Microsoft.EntityFrameworkCore;

namespace Infrastructure
{
  public class HNDbContext : DbContext, IData
  {
    public DbSet<Link> Links { get; set; }
    public DbSet<Comment> Comments { get; set; }

    IQueryable<Link> IData.Links => Links;

    IQueryable<Comment> IData.Comments => Comments;

    public HNDbContext() : base()
    {

    }

    public HNDbContext(DbContextOptions<HNDbContext> options) : base(options)
    {

    }

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
      base.OnModelCreating(modelBuilder);

      // Soit
      // modelBuilder.ApplyConfiguration(new LinkEntityType());
      // modelBuilder.ApplyConfiguration(new CommentEntityType());

      // Ou
      modelBuilder.ApplyConfigurationsFromAssembly(GetType().Assembly);
    }

    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
    {
      if (!optionsBuilder.IsConfigured)
      {
        optionsBuilder.UseSqlite("Data Source=:memory:");
      }
    }
  }
}