using foodmarket.Domain.Sales;
using Microsoft.EntityFrameworkCore;

namespace foodmarket.Infrastructure.Persistence.Configurations;

public static class SalesConfigurations
{
    public static void ConfigureSales(this ModelBuilder b)
    {
        b.Entity<RetailSale>(e =>
        {
            e.ToTable("retail_sales");
            e.Property(x => x.Number).HasMaxLength(50).IsRequired();
            e.Property(x => x.Notes).HasMaxLength(1000);
            e.Property(x => x.Subtotal).HasPrecision(18, 4);
            e.Property(x => x.DiscountTotal).HasPrecision(18, 4);
            e.Property(x => x.Total).HasPrecision(18, 4);
            e.Property(x => x.PaidCash).HasPrecision(18, 4);
            e.Property(x => x.PaidCard).HasPrecision(18, 4);

            e.HasOne(x => x.Store).WithMany().HasForeignKey(x => x.StoreId).OnDelete(DeleteBehavior.Restrict);
            e.HasOne(x => x.RetailPoint).WithMany().HasForeignKey(x => x.RetailPointId).OnDelete(DeleteBehavior.Restrict);
            e.HasOne(x => x.Customer).WithMany().HasForeignKey(x => x.CustomerId).OnDelete(DeleteBehavior.Restrict);
            e.HasOne(x => x.Currency).WithMany().HasForeignKey(x => x.CurrencyId).OnDelete(DeleteBehavior.Restrict);

            e.HasMany(x => x.Lines).WithOne(l => l.RetailSale).HasForeignKey(l => l.RetailSaleId).OnDelete(DeleteBehavior.Cascade);

            e.HasIndex(x => new { x.OrganizationId, x.Number }).IsUnique();
            e.HasIndex(x => new { x.OrganizationId, x.Date });
            e.HasIndex(x => new { x.OrganizationId, x.Status });
            e.HasIndex(x => new { x.OrganizationId, x.CashierUserId });
        });

        b.Entity<RetailSaleLine>(e =>
        {
            e.ToTable("retail_sale_lines");
            e.Property(x => x.Quantity).HasPrecision(18, 4);
            e.Property(x => x.UnitPrice).HasPrecision(18, 4);
            e.Property(x => x.Discount).HasPrecision(18, 4);
            e.Property(x => x.LineTotal).HasPrecision(18, 4);
            e.Property(x => x.VatPercent).HasPrecision(5, 2);

            e.HasOne(x => x.Product).WithMany().HasForeignKey(x => x.ProductId).OnDelete(DeleteBehavior.Restrict);
            e.HasIndex(x => new { x.OrganizationId, x.ProductId });
        });
    }
}
