using BlogPlatform.Models;

namespace BlogPlatform;

using Microsoft.EntityFrameworkCore;


public class BlogPlatformDbContext: DbContext
{
    public DbSet<Comment> Comments { get; set; }
    public DbSet<Post> Posts { get; set; }
    public DbSet<User> Users { get; set; }
    

    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
    {
        string connectionString =
            "Server=romaric-thibault.fr;" +
            "Database=BlogPlatform_cristiano;" +
            "User Id=cristiano;" +
            "Password=Onto9-Cage-Afflicted;" +
            "TrustServerCertificate=True";
        
        optionsBuilder.UseSqlServer(connectionString);
    }

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        modelBuilder.Entity<Comment>()
            .Property(comment => comment.CreatedAt)
            .HasDefaultValueSql("getdate()")
            .ValueGeneratedOnAdd();
        
        modelBuilder.Entity<Post>()
            .Property(post => post.CreatedAt)
            .HasDefaultValueSql("getdate()")
            .ValueGeneratedOnAdd();
        
        modelBuilder.Entity<User>()
            .Property(user => user.CreatedAt)
            .HasDefaultValueSql("getdate()")
            .ValueGeneratedOnAdd();
        
        modelBuilder.Entity<Comment>()
            .HasOne(c => c.User)
            .WithMany(u => u.Comments)
            .HasForeignKey(c => c.UserId)
            .OnDelete(DeleteBehavior.NoAction);
        
        
    }
}