using Lada.Framework.Data.Domains.Finance;
using Lada.Framework.Data.Mappings;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;

namespace Lada.Framework.Data.Mappings.EntityMappings.Finances
{
    public class IncomeHistoryMap : IEntityMappingConfiguration<IncomeHistory>
    {
        public void Map(EntityTypeBuilder<IncomeHistory> b)
        {
            b.ToTable("IncomeHistories");

            b.HasKey(x => x.Id);

            b.Property(x => x.IncomeId)
                .IsRequired()
                .HasComment("ID của doanh thu");

            b.Property(x => x.Status)
                .IsRequired()
                .HasComment("Trạng thái doanh thu");

            b.Property(x => x.Reason)
                .HasMaxLength(1000)
                .HasComment("Lý do từ chối (nếu có)");

            b.Property(x => x.Note)
                .HasMaxLength(500)
                .HasComment("Ghi chú");

            // Relationship với Income (Many:1)
            b.HasOne(x => x.Income)
                .WithMany()
                .HasForeignKey(x => x.IncomeId)
                .OnDelete(DeleteBehavior.Cascade);

            // Index
            b.HasIndex(x => x.IncomeId)
                .HasDatabaseName("IX_IncomeHistories_IncomeId");

            b.HasIndex(x => x.CreatedTime)
                .HasDatabaseName("IX_IncomeHistories_CreatedTime");

            b.HasIndex(x => new { x.IncomeId, x.CreatedTime })
                .HasDatabaseName("IX_IncomeHistories_IncomeId_CreatedTime");
        }
    }
}

