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

namespace UniVerse.Infrastructure.Data.Configurations;

public class CoinTransactionConfiguration : IEntityTypeConfiguration<CoinTransaction>
{
    public void Configure(EntityTypeBuilder<CoinTransaction> builder)
    {
        builder.ToTable("coin_transactions");

        builder.HasKey(ct => ct.Id);
        builder.Property(ct => ct.Id).HasColumnName("id");
        builder.Property(ct => ct.UserId).HasColumnName("user_id");
        builder.Property(ct => ct.Amount).HasColumnName("amount");
        builder.Property(ct => ct.Type).HasColumnName("type");
        builder.Property(ct => ct.ReviewId).HasColumnName("review_id");
        builder.Property(ct => ct.AchievementId).HasColumnName("achievement_id");
        builder.Property(ct => ct.Description).HasColumnName("description").HasMaxLength(500);
        builder.Property(ct => ct.CreatedAt).HasColumnName("created_at").HasDefaultValueSql("NOW()");

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

        builder.HasOne(ct => ct.Review)
            .WithMany(r => r.CoinTransactions)
            .HasForeignKey(ct => ct.ReviewId)
            .OnDelete(DeleteBehavior.SetNull);

        builder.HasOne(ct => ct.Achievement)
            .WithMany()
            .HasForeignKey(ct => ct.AchievementId)
            .OnDelete(DeleteBehavior.SetNull);

        builder.HasIndex(ct => ct.UserId);
    }
}
