using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
using UniVerse.Domain.Entities;

namespace UniVerse.Infrastructure.Data.Configurations;

public class UserAchievementConfiguration : IEntityTypeConfiguration<UserAchievement>
{
    public void Configure(EntityTypeBuilder<UserAchievement> builder)
    {
        builder.ToTable("user_achievements");

        builder.HasKey(ua => ua.Id);
        builder.Property(ua => ua.Id).HasColumnName("id");
        builder.Property(ua => ua.UserId).HasColumnName("user_id");
        builder.Property(ua => ua.AchievementId).HasColumnName("achievement_id");
        builder.Property(ua => ua.AwardedAt).HasColumnName("awarded_at").HasDefaultValueSql("NOW()");

        builder.HasOne(ua => ua.User)
            .WithMany(u => u.UserAchievements)
            .HasForeignKey(ua => ua.UserId)
            .OnDelete(DeleteBehavior.Cascade);

        builder.HasOne(ua => ua.Achievement)
            .WithMany(a => a.UserAchievements)
            .HasForeignKey(ua => ua.AchievementId)
            .OnDelete(DeleteBehavior.Cascade);

        builder.HasIndex(ua => new { ua.UserId, ua.AchievementId }).IsUnique();
    }
}
