using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
using WhatsAppService.Domain.AggregatesModel.WhatsAppAccountAggregate;

namespace WhatsAppService.Infrastructure.EntityConfigurations;

/// <summary>
/// EN: EF Core configuration for WhatsAppAccount entity.
/// VI: Cấu hình EF Core cho entity WhatsAppAccount.
/// </summary>
public class WhatsAppAccountEntityTypeConfiguration : IEntityTypeConfiguration<WhatsAppAccount>
{
    public void Configure(EntityTypeBuilder<WhatsAppAccount> builder)
    {
        builder.ToTable("whatsapp_accounts");
        builder.HasKey(e => e.Id);
        builder.Ignore(e => e.DomainEvents);

        builder.Property(e => e.Id).HasColumnName("id").IsRequired();
        builder.Property<Guid>("_shopId").HasColumnName("shop_id").IsRequired();
        builder.Property<string>("_phoneNumberId").HasColumnName("phone_number_id").HasMaxLength(50).IsRequired();
        builder.Property<string>("_accessTokenEncrypted").HasColumnName("access_token_encrypted").IsRequired();
        builder.Property<string?>("_webhookUrl").HasColumnName("webhook_url").HasMaxLength(500);
        builder.Property<string?>("_webhookVerifyToken").HasColumnName("webhook_verify_token").HasMaxLength(100);
        builder.Property<string>("_messageTier").HasColumnName("message_tier").HasMaxLength(20).HasDefaultValue("tier_1");
        builder.Property<DateTime>("_createdAt").HasColumnName("created_at").IsRequired();
        builder.Property<DateTime?>("_updatedAt").HasColumnName("updated_at");

        builder.Property(e => e.StatusId).HasColumnName("status_id").IsRequired();
        builder.HasOne(e => e.Status).WithMany().HasForeignKey(e => e.StatusId).OnDelete(DeleteBehavior.Restrict);

        builder.HasIndex("_shopId").HasDatabaseName("idx_whatsapp_accounts_shop");
        builder.HasIndex("_phoneNumberId").IsUnique().HasDatabaseName("idx_whatsapp_accounts_phone");
    }
}

/// <summary>
/// EN: EF Core configuration for WhatsAppAccountStatus enumeration.
/// VI: Cấu hình EF Core cho enumeration WhatsAppAccountStatus.
/// </summary>
public class WhatsAppAccountStatusEntityTypeConfiguration : IEntityTypeConfiguration<WhatsAppAccountStatus>
{
    public void Configure(EntityTypeBuilder<WhatsAppAccountStatus> builder)
    {
        builder.ToTable("whatsapp_account_statuses");
        builder.HasKey(s => s.Id);
        builder.Property(s => s.Id).HasColumnName("id").ValueGeneratedNever().IsRequired();
        builder.Property(s => s.Name).HasColumnName("name").HasMaxLength(50).IsRequired();

        builder.HasData(
            WhatsAppAccountStatus.Pending,
            WhatsAppAccountStatus.Active,
            WhatsAppAccountStatus.Suspended,
            WhatsAppAccountStatus.Disconnected
        );
    }
}
