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

namespace PromotionService.Infrastructure.EntityConfigurations;

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

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

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

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

        builder.Property(v => v.Code)
            .HasColumnName("code")
            .HasMaxLength(20)
            .IsRequired();

        builder.Property(v => v.OwnerId)
            .HasColumnName("owner_id");

        builder.Property(v => v.FaceValue)
            .HasColumnName("face_value")
            .HasPrecision(18, 2)
            .IsRequired();

        builder.Property(v => v.RemainingValue)
            .HasColumnName("remaining_value")
            .HasPrecision(18, 2)
            .IsRequired();

        builder.Property(v => v.StatusId)
            .HasColumnName("status_id")
            .IsRequired();

        builder.Ignore(v => v.Status);

        builder.Property(v => v.ClaimedAt)
            .HasColumnName("claimed_at");

        builder.Property(v => v.ExpiresAt)
            .HasColumnName("expires_at");

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

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

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

        // Indexes
        builder.HasIndex(v => v.Code)
            .IsUnique()
            .HasDatabaseName("ix_vouchers_code");

        builder.HasIndex(v => v.OwnerId)
            .HasDatabaseName("ix_vouchers_owner_id");

        builder.HasIndex(v => v.CampaignId)
            .HasDatabaseName("ix_vouchers_campaign_id");

        builder.HasIndex(v => v.StatusId)
            .HasDatabaseName("ix_vouchers_status_id");
    }
}
