using AyCode.Core.Serializers.Attributes;
using AyCode.Core.Serializers.Toons;
using FruitBank.Common.Dtos;
using FruitBank.Common.Interfaces;
using LinqToDB.Mapping;
using Mango.Nop.Core.Entities;
using System.ComponentModel.DataAnnotations.Schema;

namespace FruitBank.Common.Entities;

public interface IStockTakingItemPallet : IMeasuringItemPalletBase
{
    int StockTakingItemId { get; set; }
    public StockTakingItem? StockTakingItem{ get; set; }
}

[AcBinarySerializable(false, true, false, true, false, false)]
[ToonDescription("Weight record for inventory item", Purpose = "Granular weight-based evidence for a stock taking line item. NOTE: This record is mandatory for every inventory item. If weighing is skipped (non-measurable), it serves as a container for TrayQuantity with zeroed weight fields. The term 'Pallet' is a legacy naming convention.")]
[LinqToDB.Mapping.Table(Name = FruitBankConstClient.StockTakingItemPalletDbTableName)]
[System.ComponentModel.DataAnnotations.Schema.Table(FruitBankConstClient.StockTakingItemPalletDbTableName)]
public sealed class StockTakingItemPallet : MeasuringItemPalletBase, IStockTakingItemPallet
{
    public int StockTakingItemId
    {
        get => ForeignItemId;
        set => ForeignItemId = value;
    }

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

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

    public override bool IsValidSafeMeasuringValues()
    {
        return StockTakingItemId > 0 && TrayQuantity >= 0 && TareWeight >= 0 && PalletWeight >= 0 && NetWeight >= 0 && GrossWeight >= 0;
    }

    public override bool IsValidMeasuringValues(bool isMeasurable)
    {
        return StockTakingItemId > 0 && TrayQuantity >= 0 && ((!isMeasurable && NetWeight == 0 && GrossWeight == 0 && PalletWeight == 0 && TareWeight == 0) 
                                                             || (isMeasurable && NetWeight >= 0 && GrossWeight >= 0 && PalletWeight >= 0 && TareWeight >= 0));
    }

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