// EN: Shop branch entity.
// VI: Entity chi nhánh shop.

using MerchantService.Domain.Exceptions;
using MerchantService.Domain.SeedWork;

namespace MerchantService.Domain.AggregatesModel.ShopAggregate;

/// <summary>
/// EN: Represents a physical branch/location of a shop.
/// VI: Đại diện cho chi nhánh/địa điểm vật lý của shop.
/// </summary>
public class ShopBranch : Entity
{
    private Guid _shopId;
    private string _name = null!;
    private string? _code;
    private Address _address = null!;
    private GeoLocation? _location;
    private string? _phone;
    private OperatingHours? _operatingHours;
    private bool _isActive;
    private DateTime _createdAt;
    private DateTime? _updatedAt;

    /// <summary>
    /// EN: Parent shop ID.
    /// VI: ID shop cha.
    /// </summary>
    public Guid ShopId => _shopId;

    /// <summary>
    /// EN: Branch name (e.g., "Chi Nhánh Quận 1").
    /// VI: Tên chi nhánh (ví dụ: "Chi Nhánh Quận 1").
    /// </summary>
    public string Name => _name;

    /// <summary>
    /// EN: Branch code for internal reference (e.g., "HN01").
    /// VI: Mã chi nhánh để tham chiếu nội bộ (ví dụ: "HN01").
    /// </summary>
    public string? Code => _code;

    /// <summary>
    /// EN: Physical address of the branch.
    /// VI: Địa chỉ vật lý của chi nhánh.
    /// </summary>
    public Address Address => _address;

    /// <summary>
    /// EN: Geographic coordinates for map display.
    /// VI: Tọa độ địa lý để hiển thị bản đồ.
    /// </summary>
    public GeoLocation? Location => _location;

    /// <summary>
    /// EN: Branch phone number.
    /// VI: Số điện thoại chi nhánh.
    /// </summary>
    public string? Phone => _phone;

    /// <summary>
    /// EN: Operating hours for this branch.
    /// VI: Giờ hoạt động của chi nhánh này.
    /// </summary>
    public OperatingHours? OperatingHours => _operatingHours;

    /// <summary>
    /// EN: Whether branch is active.
    /// VI: Chi nhánh 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 ShopBranch()
    {
    }

    /// <summary>
    /// EN: Create a new shop branch.
    /// VI: Tạo chi nhánh shop mới.
    /// </summary>
    public ShopBranch(Guid shopId, string name, Address address, GeoLocation? location = null)
    {
        if (shopId == Guid.Empty)
            throw new DomainException("Shop ID cannot be empty");
        if (string.IsNullOrWhiteSpace(name))
            throw new DomainException("Branch name cannot be empty");
        ArgumentNullException.ThrowIfNull(address, nameof(address));

        Id = Guid.NewGuid();
        _shopId = shopId;
        _name = name.Trim();
        _address = address;
        _location = location;
        _isActive = true;
        _createdAt = DateTime.UtcNow;
    }

    /// <summary>
    /// EN: Update branch information.
    /// VI: Cập nhật thông tin chi nhánh.
    /// </summary>
    public void Update(string name, string? code, Address address, GeoLocation? location)
    {
        if (string.IsNullOrWhiteSpace(name))
            throw new DomainException("Branch name cannot be empty");

        _name = name.Trim();
        _code = code?.Trim();
        _address = address;
        _location = location;
        _updatedAt = DateTime.UtcNow;
    }

    /// <summary>
    /// EN: Set branch phone number.
    /// VI: Đặt số điện thoại chi nhánh.
    /// </summary>
    public void SetPhone(string? phone)
    {
        _phone = phone?.Trim();
        _updatedAt = DateTime.UtcNow;
    }

    /// <summary>
    /// EN: Set operating hours for this branch.
    /// VI: Đặt giờ hoạt động cho chi nhánh này.
    /// </summary>
    public void SetOperatingHours(OperatingHours? hours)
    {
        _operatingHours = hours;
        _updatedAt = DateTime.UtcNow;
    }

    /// <summary>
    /// EN: Activate the branch.
    /// VI: Kích hoạt chi nhánh.
    /// </summary>
    public void Activate()
    {
        _isActive = true;
        _updatedAt = DateTime.UtcNow;
    }

    /// <summary>
    /// EN: Deactivate the branch.
    /// VI: Vô hiệu hóa chi nhánh.
    /// </summary>
    public void Deactivate()
    {
        _isActive = false;
        _updatedAt = DateTime.UtcNow;
    }
}
