using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
using PromotionService.Domain.AggregatesModel.RedemptionAggregate;

namespace PromotionService.Infrastructure.EntityConfigurations;

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

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

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

        builder.Property(r => r.VoucherId)
            .HasColumnName("voucher_id")
            .IsRequired();

        builder.Property(r => r.CampaignId)
            .HasColumnName("campaign_id")
            .IsRequired();

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

        builder.Property(r => r.OrderId)
            .HasColumnName("order_id");

        builder.Property(r => r.AmountUsed)
            .HasColumnName("amount_used")
            .HasPrecision(18, 2)
            .IsRequired();

        builder.Property(r => r.AmountRefunded)
            .HasColumnName("amount_refunded")
            .HasPrecision(18, 2);

        builder.Property(r => r.ExecutionReference)
            .HasColumnName("execution_reference")
            .HasMaxLength(100);

        builder.Property(r => r.RedeemedAt)
            .HasColumnName("redeemed_at")
            .IsRequired();

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

        // Indexes
        builder.HasIndex(r => r.VoucherId)
            .HasDatabaseName("ix_redemptions_voucher_id");

        builder.HasIndex(r => r.UserId)
            .HasDatabaseName("ix_redemptions_user_id");

        builder.HasIndex(r => r.CampaignId)
            .HasDatabaseName("ix_redemptions_campaign_id");

        builder.HasIndex(r => r.OrderId)
            .HasDatabaseName("ix_redemptions_order_id");
    }
}
