using System.Text.Json;
using Tiku.Domain.Common;

namespace Tiku.Domain.Commerce;

public sealed class PointActivityTask : AuditableTenantEntity
{
    public string TaskKey { get; set; } = string.Empty;
    public string Title { get; set; } = string.Empty;
    public string? Description { get; set; }
    public PointActivityTaskType TaskType { get; set; } = PointActivityTaskType.Manual;
    public PointActivityTaskStatus Status { get; set; } = PointActivityTaskStatus.Active;
    public int Points { get; set; }
    public int MaxClaimsPerUser { get; set; } = 1;
    public DateTimeOffset? StartsAt { get; set; }
    public DateTimeOffset? EndsAt { get; set; }
    public int SortOrder { get; set; }
    public JsonElement Rules { get; set; } = JsonDefaults.Object();
    public JsonElement Metadata { get; set; } = JsonDefaults.Object();
}

public sealed class PointActivityClaim : AuditableTenantEntity
{
    public Guid TaskId { get; set; }
    public Guid UserId { get; set; }
    public string TaskKey { get; set; } = string.Empty;
    public int Points { get; set; }
    public PointActivityClaimStatus Status { get; set; } = PointActivityClaimStatus.Claimed;
    public string? SourceType { get; set; }
    public Guid? SourceId { get; set; }
    public DateTimeOffset ClaimedAt { get; set; } = DateTimeOffset.UtcNow;
    public JsonElement Metadata { get; set; } = JsonDefaults.Object();
}

public sealed class PointExchangeItem : AuditableTenantEntity
{
    public Guid? RegionId { get; set; }
    public string ItemKey { get; set; } = string.Empty;
    public string Name { get; set; } = string.Empty;
    public string? Description { get; set; }
    public PointExchangeItemType ItemType { get; set; } = PointExchangeItemType.Entitlement;
    public PointExchangeItemStatus Status { get; set; } = PointExchangeItemStatus.Active;
    public int PointsCost { get; set; }
    public int? Stock { get; set; }
    public int? Days { get; set; }
    public int SortOrder { get; set; }
    public DateTimeOffset? StartsAt { get; set; }
    public DateTimeOffset? EndsAt { get; set; }
    public JsonElement FulfillmentPayload { get; set; } = JsonDefaults.Object();
    public JsonElement Metadata { get; set; } = JsonDefaults.Object();
}

public sealed class PointExchangeOrder : AuditableTenantEntity
{
    public Guid ItemId { get; set; }
    public Guid UserId { get; set; }
    public string OrderNo { get; set; } = string.Empty;
    public string ItemName { get; set; } = string.Empty;
    public PointExchangeItemType ItemType { get; set; } = PointExchangeItemType.Entitlement;
    public PointExchangeOrderStatus Status { get; set; } = PointExchangeOrderStatus.Completed;
    public int PointsCost { get; set; }
    public DateTimeOffset OrderedAt { get; set; } = DateTimeOffset.UtcNow;
    public DateTimeOffset? CompletedAt { get; set; }
    public DateTimeOffset? CancelledAt { get; set; }
    public JsonElement FulfillmentSnapshot { get; set; } = JsonDefaults.Object();
    public JsonElement Metadata { get; set; } = JsonDefaults.Object();
}

public enum PointActivityTaskType { Manual, DailyLogin, Practice, VocabularyReview, BadgeUnlock }
public enum PointActivityTaskStatus { Active, Disabled, Expired }
public enum PointActivityClaimStatus { Claimed, Cancelled }
public enum PointExchangeItemType { Entitlement, Coupon, Virtual, Other }
public enum PointExchangeItemStatus { Active, Disabled, SoldOut }
public enum PointExchangeOrderStatus { Pending, Completed, Cancelled, Failed }
