using MembershipService.Domain.SeedWork;

namespace MembershipService.Domain.AggregatesModel.LevelAggregate;

/// <summary>
/// EN: Level benefit entity - represents a reward/perk for a level.
/// VI: Level benefit entity - đại diện cho reward/perk của một level.
/// </summary>
/// <remarks>
/// EN: Benefits are stored as JSON value for flexibility. Common types:
/// - discount_percent: {"percent": 10}
/// - free_shipping: {"enabled": true}
/// - priority_support: {"enabled": true}
/// - bonus_points: {"multiplier": 1.5}
/// - exclusive_access: {"feature": "early_sale"}
/// VI: Benefits được lưu dạng JSON để linh hoạt. Các loại thường gặp:
/// - discount_percent (giảm giá %)
/// - free_shipping (miễn phí ship)
/// - priority_support (hỗ trợ ưu tiên)
/// - bonus_points (nhân điểm)
/// - exclusive_access (truy cập độc quyền)
/// </remarks>
public class LevelBenefit : Entity
{
    private Guid _levelDefinitionId;
    private string _benefitType;
    private string _benefitValue; // JSON
    private string? _description;
    private bool _isActive;
    private DateTime _createdAt;

    /// <summary>
    /// EN: Foreign key to LevelDefinition.
    /// VI: Foreign key đến LevelDefinition.
    /// </summary>
    public Guid LevelDefinitionId => _levelDefinitionId;

    /// <summary>
    /// EN: Benefit type (discount_percent, free_shipping, etc.).
    /// VI: Loại benefit (discount_percent, free_shipping, etc.).
    /// </summary>
    public string BenefitType => _benefitType;

    /// <summary>
    /// EN: Benefit value as JSON string.
    /// VI: Giá trị benefit dạng chuỗi JSON.
    /// </summary>
    public string BenefitValue => _benefitValue;

    /// <summary>
    /// EN: Benefit description.
    /// VI: Mô tả benefit.
    /// </summary>
    public string? Description => _description;

    /// <summary>
    /// EN: Whether this benefit is active.
    /// VI: Benefit có đang active không.
    /// </summary>
    public bool IsActive => _isActive;

    /// <summary>
    /// EN: Creation timestamp.
    /// VI: Thời gian tạo.
    /// </summary>
    public DateTime CreatedAt => _createdAt;

    /// <summary>
    /// EN: Private constructor for EF Core.
    /// VI: Constructor private cho EF Core.
    /// </summary>
    protected LevelBenefit()
    {
        _benefitType = string.Empty;
        _benefitValue = "{}";
    }

    /// <summary>
    /// EN: Create new level benefit.
    /// VI: Tạo level benefit mới.
    /// </summary>
    /// <param name="levelDefinitionId">Level definition ID / ID của level definition</param>
    /// <param name="benefitType">Benefit type / Loại benefit</param>
    /// <param name="benefitValue">Benefit value as JSON / Giá trị benefit dạng JSON</param>
    /// <param name="description">Description / Mô tả</param>
    public LevelBenefit(
        Guid levelDefinitionId,
        string benefitType,
        string benefitValue,
        string? description = null) : this()
    {
        if (levelDefinitionId == Guid.Empty)
            throw new ArgumentException("Level definition ID cannot be empty", nameof(levelDefinitionId));
        if (string.IsNullOrWhiteSpace(benefitType))
            throw new ArgumentException("Benefit type cannot be empty", nameof(benefitType));
        if (string.IsNullOrWhiteSpace(benefitValue))
            throw new ArgumentException("Benefit value cannot be empty", nameof(benefitValue));

        Id = Guid.NewGuid();
        _levelDefinitionId = levelDefinitionId;
        _benefitType = benefitType;
        _benefitValue = benefitValue;
        _description = description;
        _isActive = true;
        _createdAt = DateTime.UtcNow;
    }

    /// <summary>
    /// EN: Update benefit value.
    /// VI: Cập nhật giá trị benefit.
    /// </summary>
    public void UpdateBenefitValue(string benefitValue)
    {
        if (string.IsNullOrWhiteSpace(benefitValue))
            throw new ArgumentException("Benefit value cannot be empty", nameof(benefitValue));

        _benefitValue = benefitValue;
    }

    /// <summary>
    /// EN: Update description.
    /// VI: Cập nhật mô tả.
    /// </summary>
    public void UpdateDescription(string? description)
    {
        _description = description;
    }

    /// <summary>
    /// EN: Activate this benefit.
    /// VI: Kích hoạt benefit này.
    /// </summary>
    public void Activate()
    {
        _isActive = true;
    }

    /// <summary>
    /// EN: Deactivate this benefit.
    /// VI: Vô hiệu hóa benefit này.
    /// </summary>
    public void Deactivate()
    {
        _isActive = false;
    }
}
