// EN: Category entity for product categorization.
// VI: Entity Category cho phân loại sản phẩm.

using CatalogService.Domain.Exceptions;
using CatalogService.Domain.SeedWork;

namespace CatalogService.Domain.AggregatesModel.ProductAggregate;

/// <summary>
/// EN: Category entity for hierarchical product categorization.
/// VI: Entity Category cho phân loại sản phẩm phân cấp.
/// </summary>
public class Category : Entity
{
    private Guid _shopId;
    private string _name = null!;
    private string? _description;
    private Guid? _parentId;
    private int _displayOrder;
    private string? _imageUrl;
    private bool _isActive;
    private DateTime _createdAt;
    private DateTime? _updatedAt;

    /// <summary>
    /// EN: Shop ID that owns this category.
    /// VI: ID shop sở hữu danh mục này.
    /// </summary>
    public Guid ShopId => _shopId;

    /// <summary>
    /// EN: Category name.
    /// VI: Tên danh mục.
    /// </summary>
    public string Name => _name;

    /// <summary>
    /// EN: Category description.
    /// VI: Mô tả danh mục.
    /// </summary>
    public string? Description => _description;

    /// <summary>
    /// EN: Parent category ID (for hierarchical categories).
    /// VI: ID danh mục cha (cho danh mục phân cấp).
    /// </summary>
    public Guid? ParentId => _parentId;

    /// <summary>
    /// EN: Category image URL.
    /// VI: URL hình ảnh danh mục.
    /// </summary>
    public string? ImageUrl => _imageUrl;

    /// <summary>
    /// EN: Display order.
    /// VI: Thứ tự hiển thị.
    /// </summary>
    public int DisplayOrder => _displayOrder;

    /// <summary>
    /// EN: Is category active.
    /// VI: Danh mục có đang hoạt động không.
    /// </summary>
    public bool IsActive => _isActive;

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

    /// <summary>
    /// EN: Last update timestamp.
    /// VI: Thời gian cập nhật cuối.
    /// </summary>
    public DateTime? UpdatedAt => _updatedAt;

    /// <summary>
    /// EN: Private constructor for EF Core.
    /// VI: Constructor private cho EF Core.
    /// </summary>
    protected Category()
    {
    }

    /// <summary>
    /// EN: Create a new category.
    /// VI: Tạo danh mục mới.
    /// </summary>
    public Category(
        Guid shopId,
        string name,
        string? description = null,
        Guid? parentId = null,
        int displayOrder = 0,
        string? imageUrl = null)
    {
        if (shopId == Guid.Empty)
            throw new DomainException("Shop ID cannot be empty");
        if (string.IsNullOrWhiteSpace(name))
            throw new DomainException("Category name cannot be empty");

        Id = Guid.NewGuid();
        _shopId = shopId;
        _name = name.Trim();
        _description = description?.Trim();
        _parentId = parentId;
        _displayOrder = displayOrder;
        _imageUrl = imageUrl;
        _isActive = true;
        _createdAt = DateTime.UtcNow;
    }

    /// <summary>
    /// EN: Update category information.
    /// VI: Cập nhật thông tin danh mục.
    /// </summary>
    public void UpdateInfo(string name, string? description, int displayOrder)
    {
        if (string.IsNullOrWhiteSpace(name))
            throw new DomainException("Category name cannot be empty");

        _name = name.Trim();
        _description = description?.Trim();
        _displayOrder = displayOrder;
        _updatedAt = DateTime.UtcNow;
    }

    /// <summary>
    /// EN: Update category image.
    /// VI: Cập nhật hình ảnh danh mục.
    /// </summary>
    public void UpdateImage(string? imageUrl)
    {
        _imageUrl = imageUrl;
        _updatedAt = DateTime.UtcNow;
    }

    /// <summary>
    /// EN: Update parent category.
    /// VI: Cập nhật danh mục cha.
    /// </summary>
    public void UpdateParent(Guid? parentId)
    {
        if (parentId == Id)
            throw new DomainException("Category cannot be its own parent");

        _parentId = parentId;
        _updatedAt = DateTime.UtcNow;
    }

    /// <summary>
    /// EN: Activate category.
    /// VI: Kích hoạt danh mục.
    /// </summary>
    public void Activate()
    {
        _isActive = true;
        _updatedAt = DateTime.UtcNow;
    }

    /// <summary>
    /// EN: Deactivate category.
    /// VI: Vô hiệu hóa danh mục.
    /// </summary>
    public void Deactivate()
    {
        _isActive = false;
        _updatedAt = DateTime.UtcNow;
    }
}
