using AyCode.Core.Serializers.Attributes;
using AyCode.Core.Serializers.Toons;
﻿using FruitBank.Common.Dtos;
using FruitBank.Common.Enums;
using FruitBank.Common.Interfaces;
using LinqToDB.Mapping;
using Newtonsoft.Json;
using Nop.Core.Domain.Orders;
using System.ComponentModel.DataAnnotations.Schema;
using Column = LinqToDB.Mapping.ColumnAttribute;
using Table = LinqToDB.Mapping.TableAttribute;

namespace FruitBank.Common.Entities;

[AcBinarySerializable(false, true, false, true, false, false)]
[ToonDescription("Pallet measurements for order items with audit tracking", Purpose = "A measurement record for outgoing goods, used to verify that the net weight being sent to the customer is accurate and audited. NOTE: Despite the 'Pallet' name, this is a general measurement record that is ALWAYS created for every item. If the product is not measurable (IsMeasurable=false), weights are recorded as 0.0 and only TrayQuantity is stored.")]
[Table(Name = FruitBankConstClient.OrderItemPalletDbTableName)]
[System.ComponentModel.DataAnnotations.Schema.Table(FruitBankConstClient.OrderItemPalletDbTableName)]
public sealed class OrderItemPallet : MeasuringItemPalletBase, IOrderItemPallet
{
    public int OrderItemId
    {
        get => ForeignItemId;
        set => ForeignItemId = value;
    }

    [ToonDescription(Purpose = "User/Customer ID of the quality auditor")]
    public int RevisorId { get; set; }

    [NotColumn, NotMapped, JsonIgnore, System.Text.Json.Serialization.JsonIgnore]
    [ToonDescription(Purpose = "Status flag", BusinessRule = "get => RevisorId > 0")]
    public bool IsAudited => RevisorId > 0;

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

    [NotColumn, NotMapped, JsonIgnore, System.Text.Json.Serialization.JsonIgnore]
    [ToonDescription(BusinessRule = "get => IsAudited ? MeasuringStatus.Audited : base.MeasuringStatus")]
    public override MeasuringStatus MeasuringStatus => IsAudited ? MeasuringStatus.Audited : base.MeasuringStatus;
    public override double CalculateNetWeight() => base.CalculateNetWeight();

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

    [NotColumn, NotMapped, JsonIgnore, System.Text.Json.Serialization.JsonIgnore]
    [ToonDescription(BusinessRule = "get => double.Round(NetWeight / TrayQuantity, 1)")]
    public double AverageWeight => double.Round(NetWeight / TrayQuantity, 1);

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

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