using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
using MembershipService.Domain.AggregatesModel.StampCardAggregate;

namespace MembershipService.Infrastructure.EntityConfigurations;

/// <summary>
/// EN: Entity configuration for StampCard aggregate.
/// VI: Cau hinh entity cho StampCard aggregate.
/// </summary>
public class StampCardEntityTypeConfiguration : IEntityTypeConfiguration<StampCard>
{
    public void Configure(EntityTypeBuilder<StampCard> builder)
    {
        builder.ToTable("stamp_cards");

        // EN: Primary key
        // VI: Primary key
        builder.HasKey(sc => sc.Id);

        builder.Property(sc => sc.Id)
            .HasColumnName("id")
            .IsRequired();

        // EN: Shop ID
        // VI: ID shop
        builder.Property("_shopId")
            .HasColumnName("shop_id")
            .IsRequired();

        // EN: Member ID
        // VI: ID member
        builder.Property("_memberId")
            .HasColumnName("member_id")
            .IsRequired();

        // EN: Card name
        // VI: Ten card
        builder.Property("_cardName")
            .HasColumnName("card_name")
            .HasMaxLength(200)
            .IsRequired();

        // EN: Total stamps required for reward
        // VI: Tong stamp can cho thuong
        builder.Property("_totalStampsRequired")
            .HasColumnName("total_stamps_required")
            .IsRequired()
            .HasDefaultValue(10);

        // EN: Current stamps collected
        // VI: So stamp hien tai da thu thap
        builder.Property("_currentStamps")
            .HasColumnName("current_stamps")
            .IsRequired()
            .HasDefaultValue(0);

        // EN: Whether card is completed
        // VI: Card da hoan thanh chua
        builder.Property("_isCompleted")
            .HasColumnName("is_completed")
            .IsRequired()
            .HasDefaultValue(false);

        // EN: Whether reward has been claimed
        // VI: Thuong da duoc nhan chua
        builder.Property("_rewardClaimed")
            .HasColumnName("reward_claimed")
            .IsRequired()
            .HasDefaultValue(false);

        // EN: Timestamps
        // VI: Timestamps
        builder.Property("_createdAt")
            .HasColumnName("created_at")
            .IsRequired();

        builder.Property("_completedAt")
            .HasColumnName("completed_at");

        // EN: Ignore domain events (not persisted)
        // VI: Bo qua domain events (khong luu)
        builder.Ignore(sc => sc.DomainEvents);

        // EN: Indexes for common queries
        // VI: Indexes cho cac query thuong dung

        // EN: Composite index for member+shop lookup (most common query)
        // VI: Composite index cho tra cuu member+shop (query thuong dung nhat)
        builder.HasIndex("_shopId", "_memberId")
            .HasDatabaseName("ix_stamp_cards_shop_member");

        // EN: Index for shop-level admin queries
        // VI: Index cho cac query admin cap shop
        builder.HasIndex("_shopId")
            .HasDatabaseName("ix_stamp_cards_shop_id");

        // EN: Index for filtering by completion status
        // VI: Index cho loc theo trang thai hoan thanh
        builder.HasIndex("_isCompleted")
            .HasDatabaseName("ix_stamp_cards_is_completed");

        builder.HasIndex("_createdAt")
            .HasDatabaseName("ix_stamp_cards_created_at");
    }
}
