using Microsoft.EntityFrameworkCore;

namespace ImageBoardServerApp.Data;

internal sealed class AppDBContext : DbContext
{
    public DbSet<UserData> Users { get; set; }
    public DbSet<PostData> Posts { get; set; }
    public DbSet<ImageData> Images { get; set; }
    public DbSet<CommentData> Comments { get; set; }
    public DbSet<ReportData> Reports { get; set; }
    public DbSet<BoardData> Boards { get; set; }

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

    protected override void OnModelCreating(ModelBuilder mb)
    {
        mb.Entity<PostData>()
            .HasMany(post => post.Comments)
            .WithOne(comment => comment.Post);
        mb.Entity<PostData>()
            .HasOne(post => post.Image)
            .WithOne(image => image.Post);

        mb.Entity<CommentData>()
            .HasOne(comment => comment.Image)
            .WithOne(image => image.Comment);

        mb.Entity<UserData>()
            .HasMany(user => user.Posts)
            .WithOne(post => post.User);
        mb.Entity<UserData>()
            .HasMany(user => user.Comments)
            .WithOne(comment => comment.User);

        mb.Entity<ReportData>()
            .HasOne(report => report.UserReported)
            .WithMany(user => user.RecivedReports);
        mb.Entity<ReportData>()
            .HasOne(report => report.UserReporter)
            .WithMany(user => user.SubmittedReports);
        mb.Entity<ReportData>()
            .HasOne(report => report.ReportedPost)
            .WithOne(post => post.Report);
        mb.Entity<ReportData>()
            .HasOne(report => report.ReportedComment)
            .WithOne(comment => comment.Report);
    }
}