using AyCode.Core.Extensions;
using AyCode.Core.Interfaces;
using AyCode.Core.Serializers.Attributes;
using AyCode.Core.Serializers.Binaries;
using AyCode.Core.Serializers.Jsons;
using MemoryPack;
using MessagePack;
using MongoDB.Bson.Serialization.Attributes;
using Newtonsoft.Json;

namespace AyCode.Core.Tests.TestModels;

#region Shared Enums

/// <summary>
/// Common status enum for all test entities
/// </summary>
public enum TestStatus
{
    Pending = 5,
    Active = 10,
    Processing = 20,
    Completed = 30,
    Shipped = 40,
    OnHold = 50
}

/// <summary>
/// Priority levels for tasks and projects
/// </summary>
public enum TestPriority
{
    Low = 0,
    Medium = 1,
    High = 2,
    Critical = 3
}

/// <summary>
/// User roles for testing
/// </summary>
public enum TestUserRole
{
    User = 0,
    Manager = 1,
    Admin = 2
}

#endregion

#region 5-Level Test Hierarchy (Order -> Item -> Pallet -> Measurement -> Point)

/// <summary>
/// Level 1: Main order - root of the hierarchy
/// </summary>
[AcBinarySerializable(true)]
public partial class TestOrder_Circ_Ref : IId<int>
{
    public int Id { get; set; }
    public string OrderNumber { get; set; } = "";
    public TestStatus Status { get; set; } = TestStatus.Pending;
    public DateTime CreatedAt { get; set; } = DateTime.UtcNow;
    public DateTime? PaidDateUtc { get; set; }
    public decimal TotalAmount { get; set; }

    // Level 2 collection
    public List<TestOrderItem_Circ_Ref> Items { get; set; } = [];

    // Shared reference properties (for $id/$ref testing)
    public SharedTag_All_True? PrimaryTag { get; set; }
    public SharedTag_All_True? SecondaryTag { get; set; }
    public SharedUser_All_True? Owner { get; set; }
    public SharedCategory_All_True? Category { get; set; }

    // Collection of shared references
    public List<SharedTag_All_True> Tags { get; set; } = [];
    public MetadataInfo_All_True? OrderMetadata { get; set; }
    public MetadataInfo_All_True? AuditMetadata { get; set; }
    public List<MetadataInfo_All_True> MetadataList { get; set; } = [];

    // NoMerge collection for testing replace behavior
    [JsonNoMergeCollection]
    public List<TestOrderItem_Circ_Ref> NoMergeItems { get; set; } = [];

    public object? Parent { get; set; }
}
/// <summary>
/// Level 2: Order item with pallets
/// </summary>
[AcBinarySerializable(true)]
public partial class TestOrderItem_Circ_Ref : IId<int>
{
    public int Id { get; set; }
    [AcStringIntern(true)]
    public string ProductName { get; set; } = "";
    public int Quantity { get; set; }
    public decimal UnitPrice { get; set; }
    public TestStatus Status { get; set; } = TestStatus.Pending;

    // Level 3 collection
    public List<TestPallet_All_True> Pallets { get; set; } = [];

    // Shared references
    public SharedTag_All_True? Tag { get; set; }
    public SharedUser_All_True? Assignee { get; set; }
    public MetadataInfo_All_True? ItemMetadata { get; set; }

    public TestOrder_Circ_Ref? ParentOrder { get; set; }
}
#endregion

#region Guid-based IId types

/// <summary>
/// Order with Guid Id - for testing Guid-based IId
/// </summary>
[AcBinarySerializable(true)]
public class TestGuidOrder : IId<Guid>
{
    public Guid Id { get; set; }
    public string Code { get; set; } = "";
    public List<TestGuidItem> Items { get; set; } = [];
    public int Count { get; set; }
}

/// <summary>
/// Item with Guid Id
/// </summary>
[AcBinarySerializable(true)]
public class TestGuidItem : IId<Guid>
{
    public Guid Id { get; set; }
    public string Name { get; set; } = "";
    public int Qty { get; set; }
}

#endregion

#region Test-specific classes

/// <summary>
/// Simulates NopCommerce GenericAttribute - stores key-value pairs where DateTime values
/// are stored as strings in the database.
/// </summary>
[AcBinarySerializable(true)]
public class TestGenericAttribute
{
    public int Id { get; set; }
    public string Key { get; set; } = "";
    public string Value { get; set; } = "";
}

/// <summary>
/// DTO with GenericAttributes collection - simulates OrderDto with string-stored DateTime values.
/// This reproduces the production bug where Binary serialization was thought to corrupt DateTime strings.
/// </summary>
[AcBinarySerializable(true)]
public class TestDtoWithGenericAttributes : IId<int>
{
    public int Id { get; set; }
    public string Name { get; set; } = "";
    public List<TestGenericAttribute> GenericAttributes { get; set; } = [];
}

/// <summary>
/// Order with nullable collections for null vs empty testing
/// </summary>
[AcBinarySerializable(true)]
public class TestOrderWithNullableCollections
{
    public int Id { get; set; }
    public string OrderNumber { get; set; } = "";
    public List<TestOrderItem_All_True>? Items { get; set; }
    public List<string>? Tags { get; set; }
}

/// <summary>
/// Class with all primitive types for WASM/serialization testing
/// </summary>
[MemoryPackable]
[AcBinarySerializable(true)]
public partial class PrimitiveTestClass
{
    public int IntValue { get; set; }
    public long LongValue { get; set; }
    public double DoubleValue { get; set; }
    public decimal DecimalValue { get; set; }
    public float FloatValue { get; set; }
    public bool BoolValue { get; set; }
    public string StringValue { get; set; } = "";
    public Guid GuidValue { get; set; }
    public DateTime DateTimeValue { get; set; }
    public TestStatus EnumValue { get; set; }
    public byte ByteValue { get; set; }
    public short ShortValue { get; set; }
    public int? NullableInt { get; set; }
    public int? NullableIntNull { get; set; }
}

/// <summary>
/// Class with extended primitive types for full serializer coverage.
/// Includes DateTimeOffset, TimeSpan, Dictionary, null properties.
/// </summary>
[AcBinarySerializable(true)]
public class ExtendedPrimitiveTestClass
{
    public int Id { get; set; }
    public string Name { get; set; } = "";
    
    // Extended primitive types not covered in PrimitiveTestClass
    public DateTimeOffset DateTimeOffsetValue { get; set; }
    public TimeSpan TimeSpanValue { get; set; }
    public uint UIntValue { get; set; }
    public ulong ULongValue { get; set; }
    public ushort UShortValue { get; set; }
    public sbyte SByteValue { get; set; }
    public char CharValue { get; set; }
    
    // Dictionary property for WriteDictionary coverage in object context
    public Dictionary<string, int>? Counts { get; set; }
    public Dictionary<string, string>? Labels { get; set; }
    
    // Nullable properties that will be null
    public string? NullString { get; set; }
    public TestOrderItem_All_True? NullObject { get; set; }
    
    // Nested object for complex serialization
    public SharedTag_All_True? Tag { get; set; }
}

/// <summary>
/// Class with array of objects containing null items for WriteNull coverage
/// </summary>
[AcBinarySerializable(true)]
public class ObjectWithNullItems
{
    public int Id { get; set; }
    public List<object?> MixedItems { get; set; } = [];
}

#endregion

#region Property Mismatch DTOs (Server has more properties than Client)

/// <summary>
/// "Server-side" DTO with extra properties that the "client" doesn't know about.
/// Used to test SkipValue functionality when deserializing unknown properties.
/// </summary>
[AcBinarySerializable(true)]
public class ServerCustomerDto : IId<int>
{
    public int Id { get; set; }
    
    // Common properties (both server and client have these)
    public string FirstName { get; set; } = "";
    public string LastName { get; set; } = "";
    
    // Extra properties (only server has these - client will skip them)
    public string Email { get; set; } = "";
    public string Phone { get; set; } = "";
    public string Address { get; set; } = "";
    public string City { get; set; } = "";
    public string Country { get; set; } = "";
    public string PostalCode { get; set; } = "";
    public string Company { get; set; } = "";
    public string Department { get; set; } = "";
    public string Notes { get; set; } = "";
    public DateTime CreatedAt { get; set; } = DateTime.UtcNow;
    public DateTime? LastLoginAt { get; set; }
    public TestStatus Status { get; set; } = TestStatus.Active;
    public bool IsVerified { get; set; }
    public int LoginCount { get; set; }
    public decimal Balance { get; set; }
}

/// <summary>
/// "Client-side" DTO with fewer properties than the server version.
/// When deserializing ServerCustomerDto data into this type, 
/// the deserializer must skip unknown properties correctly 
/// while still maintaining string intern table consistency.
/// </summary>
[AcBinarySerializable(true)]
public class ClientCustomerDto : IId<int>
{
    public int Id { get; set; }
    
    // Only basic properties - client doesn't need all server fields
    public string FirstName { get; set; } = "";
    public string LastName { get; set; } = "";
}

/// <summary>
/// Server DTO with nested objects that client doesn't know about.
/// Tests skipping complex nested structures.
/// </summary>
[AcBinarySerializable(true)]
public class ServerOrderWithExtras : IId<int>
{
    public int Id { get; set; }
    public string OrderNumber { get; set; } = "";
    public decimal TotalAmount { get; set; }
    
    // Nested object that client doesn't have
    public ServerCustomerDto? Customer { get; set; }
    
    // List of objects that client doesn't know about
    public List<ServerCustomerDto> RelatedCustomers { get; set; } = [];
    
    // Extra simple properties
    public string InternalNotes { get; set; } = "";
    public string ProcessingCode { get; set; } = "";
}

/// <summary>
/// Client version of the order - doesn't have Customer/RelatedCustomers properties.
/// </summary>
[AcBinarySerializable(true)]
public class ClientOrderSimple : IId<int>
{
    public int Id { get; set; }
    public string OrderNumber { get; set; } = "";
    public decimal TotalAmount { get; set; }
}

#endregion
