namespace WalletService.Infrastructure.EntityConfigurations;

using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
using WalletService.Domain.AggregatesModel.PointAccountAggregate;

/// <summary>
/// EN: EF Core configuration for PointAccount aggregate
/// VI: Cấu hình EF Core cho aggregate PointAccount
/// </summary>
public class PointAccountEntityTypeConfiguration : IEntityTypeConfiguration<PointAccount>
{
    public void Configure(EntityTypeBuilder<PointAccount> builder)
    {
        builder.ToTable("point_accounts");

        builder.HasKey(p => p.Id);

        builder.Property(p => p.Id)
            .HasColumnName("id")
            .ValueGeneratedNever();

        builder.Property(p => p.UserId)
            .HasColumnName("user_id")
            .IsRequired();

        builder.HasIndex(p => p.UserId)
            .IsUnique()
            .HasDatabaseName("ix_point_accounts_user_id");

        builder.Property(p => p.TotalPoints)
            .HasColumnName("total_points")
            .IsRequired();

        builder.Property(p => p.AvailablePoints)
            .HasColumnName("available_points")
            .IsRequired();

        builder.Property(p => p.CreatedAt)
            .HasColumnName("created_at")
            .IsRequired();

        builder.Property(p => p.UpdatedAt)
            .HasColumnName("updated_at")
            .IsRequired();

        // EN: Configure navigation to transactions
        // VI: Cấu hình navigation đến transactions
        builder.HasMany(p => p.Transactions)
            .WithOne()
            .HasForeignKey(t => t.AccountId)
            .OnDelete(DeleteBehavior.Cascade);

        builder.Ignore(p => p.DomainEvents);
    }
}
