using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
using TakeoutSaaS.Domain.Tenants.Entities;
using TakeoutSaaS.Domain.Tenants.Enums;

namespace TakeoutSaaS.Infrastructure.App.Persistence.Configurations;

/// <summary>
/// <see cref="TenantBillingStatement"/> EF Core 映射配置。
/// </summary>
public sealed class TenantBillingStatementConfiguration : IEntityTypeConfiguration<TenantBillingStatement>
{
    /// <inheritdoc />
    public void Configure(EntityTypeBuilder<TenantBillingStatement> builder)
    {
        builder.ToTable("tenant_billing_statements");
        builder.HasKey(x => x.Id);

        // 1. 字段约束
        builder.Property(x => x.StatementNo).HasMaxLength(64).IsRequired();
        builder.Property(x => x.BillingType).HasConversion<int>();
        builder.Property(x => x.AmountDue).HasPrecision(18, 2);
        builder.Property(x => x.DiscountAmount).HasPrecision(18, 2);
        builder.Property(x => x.TaxAmount).HasPrecision(18, 2);
        builder.Property(x => x.AmountPaid).HasPrecision(18, 2);
        builder.Property(x => x.Currency).HasMaxLength(8).HasDefaultValue("CNY");
        builder.Property(x => x.Status).HasConversion<int>();

        // 2. JSON 字段（当前以 text 存储 JSON 字符串，便于兼容历史迁移）
        builder.Property(x => x.LineItemsJson).HasColumnType("text");

        // 3. 备注字段
        builder.Property(x => x.Notes).HasMaxLength(512);

        // 4. 唯一约束与索引
        builder.HasIndex(x => new { x.TenantId, x.StatementNo }).IsUnique();

        // 5. 性能索引（高频查询：租户+状态+到期日）
        builder.HasIndex(x => new { x.TenantId, x.Status, x.DueDate })
            .HasDatabaseName("idx_billing_tenant_status_duedate");

        // 6. 逾期扫描索引（仅索引 Pending/Overdue）
        builder.HasIndex(x => new { x.Status, x.DueDate })
            .HasDatabaseName("idx_billing_status_duedate")
            .HasFilter($"\"Status\" IN ({(int)TenantBillingStatus.Pending}, {(int)TenantBillingStatus.Overdue})");

        // 7. 创建时间索引（支持列表倒序）
        builder.HasIndex(x => x.CreatedAt)
            .HasDatabaseName("idx_billing_created_at");
    }
}
