using ERPCore.Domain.Entities;
using ERPCore.Domain.Enums;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;

namespace ERPCore.Infra.Persistence.Configurations;

public sealed class BundleSaleTemplateConfiguration : IEntityTypeConfiguration<BundleSaleTemplate>
{
    public void Configure(EntityTypeBuilder<BundleSaleTemplate> builder)
    {
        builder.ToTable("bundle_sale_templates");
        builder.HasKey(x => x.BundleSaleTemplateId);

        builder.Property(x => x.TemplateCode).IsRequired().HasMaxLength(50);
        builder.HasIndex(x => x.TemplateCode).IsUnique();
        builder.Property(x => x.TemplateName).IsRequired().HasMaxLength(200);
        builder.Property(x => x.Description).HasMaxLength(1000);
        builder.Property(x => x.Status)
            .HasConversion<string>().HasMaxLength(20).IsRequired()
            .HasDefaultValue(EntityStatus.Active);
        builder.Property(x => x.CreatedAt).IsRequired();
        builder.Property(x => x.ConcurrencyStamp)
            .IsRequired()
            .HasColumnType("integer")
            .HasDefaultValue(0)
            .IsConcurrencyToken();

        builder.HasMany(x => x.Lines)
            .WithOne(x => x.BundleSaleTemplate)
            .HasForeignKey(x => x.BundleSaleTemplateId)
            .OnDelete(DeleteBehavior.Cascade);
    }
}
