using AdsAnalyticsService.Domain.SeedWork;

namespace AdsAnalyticsService.Domain.AggregatesModel.MetricsAggregate;

/// <summary>
/// EN: Campaign metrics aggregate root - performance metrics per campaign.
/// VI: Campaign metrics aggregate root - chỉ số hiệu suất theo campaign.
/// </summary>
public class CampaignMetrics : Entity, IAggregateRoot
{
    private Guid _campaignId;
    private DateTime _date;
    private long _impressions;
    private long _clicks;
    private long _conversions;
    private decimal _spend;
    private decimal _revenue;

    public Guid CampaignId => _campaignId;
    public DateTime Date => _date;
    public long Impressions => _impressions;
    public long Clicks => _clicks;
    public long Conversions => _conversions;
    public decimal Spend => _spend;
    public decimal Revenue => _revenue;

    // Calculated metrics
    public decimal CTR => _impressions > 0 ? (decimal)_clicks / _impressions * 100 : 0;
    public decimal CPC => _clicks > 0 ? _spend / _clicks : 0;
    public decimal CPA => _conversions > 0 ? _spend / _conversions : 0;
    public decimal ROAS => _spend > 0 ? _revenue / _spend : 0;

    protected CampaignMetrics() { }

    public CampaignMetrics(Guid campaignId, DateTime date)
    {
        Id = Guid.NewGuid();
        _campaignId = campaignId;
        _date = date.Date; // Normalize to start of day
    }

    public void RecordImpression() => _impressions++;
    public void RecordClick() => _clicks++;
    public void RecordConversion() => _conversions++;
    public void AddSpend(decimal amount) => _spend += amount;
    public void AddRevenue(decimal amount) => _revenue += amount;
}
