using Lada.Framework.Data.Domains.Orders;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;

namespace Lada.Framework.Data.Mappings.EntityMappings.Orders
{
    public class OrderMap : IEntityMappingConfiguration<Order>
    {
        public void Map(EntityTypeBuilder<Order> builder)
        {
            builder.HasKey(o => o.Id);
            builder.HasIndex(o => o.Code).IsUnique();
            builder.Property(o => o.Id).ValueGeneratedOnAdd();

            #region One-to-One
            builder.HasOne(o => o.OrderFin).WithOne(fin => fin.Order).HasForeignKey<OrderFin>(ops => ops.Id);
            builder.HasOne(o => o.OrderOps).WithOne(ops => ops.Order).HasForeignKey<OrderOps>(ops => ops.Id);
            builder.HasOne(o => o.OrderTimeTrack).WithOne(ot => ot.Order).HasForeignKey<OrderTimeTrack>(ot => ot.Id);
            #endregion


            #region One-to-Many
            builder.HasMany(o => o.Addresses).WithOne(add => add.Order).HasForeignKey(add => add.OrderId);
            builder.HasMany(o => o.Tracks).WithOne(tr => tr.Order).HasForeignKey(add => add.OrderId);
            builder.HasMany(o => o.Packages).WithOne(od => od.Order).HasForeignKey(od => od.OrderId);
            #endregion


            builder.ToTable("Order");
        }
    }
}
