using AyCode.Core.Serializers.Attributes;
using AyCode.Core.Serializers.Toons;
using FruitBank.Common.Interfaces;
using LinqToDB.Mapping;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Security.Cryptography.X509Certificates;

namespace FruitBank.Common.Entities;

[AcBinarySerializable(false, true, false, true, false, false)]
[ToonDescription("Pallet measurements for shipping items", Purpose = "The smallest unit of measurement tracking, representing a single physical measurement event. NOTE: Technically named 'Pallet' for legacy reasons, but it is ALWAYS created even if goods arrive without a physical pallet. For non-measurable products, weights are 0.0 and only TrayQuantity is tracked for tare-weight calculations.")]
[LinqToDB.Mapping.Table(Name = FruitBankConstClient.ShippingItemPalletDbTableName)]
[System.ComponentModel.DataAnnotations.Schema.Table(FruitBankConstClient.ShippingItemPalletDbTableName)]
public sealed class ShippingItemPallet : MeasuringItemPalletBase, IShippingItemPallet
{
    public int ShippingItemId
    {
        get => ForeignItemId;
        set => ForeignItemId = value;
    }

    //[Newtonsoft.Json.JsonIgnore, System.Text.Json.Serialization.JsonIgnore]
    [LinqToDB.Mapping.Association(ThisKey = nameof(ShippingItemId), OtherKey = nameof(ShippingItem.Id), CanBeNull = true)]
    public ShippingItem? ShippingItem { get; set; }

    public override double CalculateNetWeight() => base.CalculateNetWeight();

    public override bool IsValidSafeMeasuringValues()
    {
        return ShippingItemId > 0 && base.IsValidSafeMeasuringValues();
    }

    /// <summary>
    /// "Szigorúbb" mint az IsValidSafeMeasuringValues()
    /// </summary>
    /// <param name="isMeasurable"></param>
    /// <returns></returns>
    public override bool IsValidMeasuringValues(bool isMeasurable)
    {
        return ShippingItemId > 0 && base.IsValidMeasuringValues(isMeasurable);
    }

    public override void SetParentPropToNull()
    {
        ShippingItem = null;
    }
}