using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.ChangeTracking;
using Microsoft.EntityFrameworkCore.ValueGeneration;
using Modules.Rating.Api.Database.Entities;

namespace Modules.Rating.Api.Database;

public class RatingDbContext : DbContext
{
    public DbSet<Vote> Votes { get; set; }

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

    }

    protected override void ConfigureConventions(ModelConfigurationBuilder configurationBuilder)
    {
        configurationBuilder.Properties<Enum>().HaveConversion<string>();
    }

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        modelBuilder.Entity<Vote>(q =>
        {
            q.HasKey(q => new { q.SubjectId, q.ObjectId });
            q.Property(q => q.VoteDate).HasDefaultValueSql("now()").ValueGeneratedOnAdd();
            q.Property(q => q.LastUpdate).HasDefaultValueSql("now()").ValueGeneratedOnAddOrUpdate();
        });
    }
}
