using AdsTrackingService.Domain.SeedWork;

namespace AdsTrackingService.Domain.AggregatesModel.TrackingPixelAggregate;

/// <summary>
/// EN: Tracking pixel aggregate root - tracks ad impressions and clicks.
/// VI: Tracking pixel aggregate root - theo dõi impression và click quảng cáo.
/// </summary>
public class TrackingPixel : Entity, IAggregateRoot
{
    private Guid _advertiserId;
    private string _pixelCode = null!;
    private bool _isActive;

    public Guid AdvertiserId => _advertiserId;
    public string PixelCode => _pixelCode;
    public bool IsActive => _isActive;
    public DateTime CreatedAt { get; private set; }

    protected TrackingPixel() { }

    public TrackingPixel(Guid advertiserId)
    {
        Id = Guid.NewGuid();
        _advertiserId = advertiserId;
        _pixelCode = GeneratePixelCode();
        _isActive = true;
        CreatedAt = DateTime.UtcNow;
    }

    public void Deactivate() => _isActive = false;
    public void Activate() => _isActive = true;

    private static string GeneratePixelCode() => Guid.NewGuid().ToString("N")[..16].ToUpper();
}

/// <summary>
/// EN: Pixel event - individual tracking event (impression, click, pageview).
/// VI: Pixel event - sự kiện tracking riêng lẻ.
/// </summary>
public class PixelEvent : Entity
{
    private Guid _pixelId;
    private Guid _adId;
    private Guid _userId;
    private PixelEventType _eventType;
    private string? _userAgent;
    private string? _ipAddress;
    private DateTime _timestamp;

    public Guid PixelId => _pixelId;
    public Guid AdId => _adId;
    public Guid UserId => _userId;
    public PixelEventType EventType => _eventType;
    public string? UserAgent => _userAgent;
    public string? IpAddress => _ipAddress;
    public DateTime Timestamp => _timestamp;

    protected PixelEvent() { }

    public PixelEvent(Guid pixelId, Guid adId, Guid userId, PixelEventType eventType, string? userAgent = null, string? ipAddress = null)
    {
        Id = Guid.NewGuid();
        _pixelId = pixelId;
        _adId = adId;
        _userId = userId;
        _eventType = eventType;
        _userAgent = userAgent;
        _ipAddress = ipAddress;
        _timestamp = DateTime.UtcNow;
    }
}

/// <summary>
/// EN: Pixel event types for tracking user behavior.
/// VI: Các loại sự kiện pixel để theo dõi hành vi người dùng.
/// </summary>
public enum PixelEventType
{
    Impression = 1,
    Click = 2,
    PageView = 3,
    ViewContent = 4,        // EN: Product/content view / VI: Xem sản phẩm/nội dung
    AddToCart = 5,          // EN: Add to cart / VI: Thêm vào giỏ hàng
    InitiateCheckout = 6,   // EN: Begin checkout / VI: Bắt đầu thanh toán
    Purchase = 7,           // EN: Complete purchase / VI: Hoàn tất mua hàng
    Lead = 8                // EN: Lead generation / VI: Thu thập thông tin khách hàng
}
