package com.social.media.infrastructure.persistence.analytics.entity;

import jakarta.persistence.*;
import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.Map;

/**
 * JPA Entity for AccountMetrics aggregate persistence
 */
@Entity
@Table(name = "account_metrics")
public class AccountMetricsEntity {
    
    @Id
    @Column(name = "id", columnDefinition = "VARCHAR(255)")
    private String id;
    
    @Column(name = "company_id", nullable = false)
    private Long companyId;
    
    @Column(name = "social_account_id", nullable = false)
    private Long socialAccountId;
    
    @Enumerated(EnumType.STRING)
    @Column(name = "period", nullable = false)
    private TimePeriodEntity period;
    
    @Column(name = "period_start", nullable = false)
    private LocalDateTime periodStart;
    
    @Column(name = "period_end", nullable = false)
    private LocalDateTime periodEnd;
    
    // Metrics stored as individual columns for better query performance
    @ElementCollection
    @CollectionTable(name = "account_metrics_values", joinColumns = @JoinColumn(name = "account_metrics_id"))
    @MapKeyColumn(name = "metric_type")
    @MapKeyEnumerated(EnumType.STRING)
    @Column(name = "metric_value", precision = 19, scale = 4)
    private Map<MetricTypeEntity, BigDecimal> metricValues = new HashMap<>();
    
    @Column(name = "collected_at", nullable = false)
    private LocalDateTime collectedAt;
    
    @Column(name = "created_at", nullable = false)
    private LocalDateTime createdAt;
    
    @Column(name = "updated_at", nullable = false)
    private LocalDateTime updatedAt;
    
    @Version
    private Long version;
    
    // Default constructor for JPA
    public AccountMetricsEntity() {}
    
    // Getters and Setters
    public String getId() {
        return id;
    }
    
    public void setId(String id) {
        this.id = id;
    }
    
    public Long getCompanyId() {
        return companyId;
    }
    
    public void setCompanyId(Long companyId) {
        this.companyId = companyId;
    }
    
    public Long getSocialAccountId() {
        return socialAccountId;
    }
    
    public void setSocialAccountId(Long socialAccountId) {
        this.socialAccountId = socialAccountId;
    }
    
    public TimePeriodEntity getPeriod() {
        return period;
    }
    
    public void setPeriod(TimePeriodEntity period) {
        this.period = period;
    }
    
    public LocalDateTime getPeriodStart() {
        return periodStart;
    }
    
    public void setPeriodStart(LocalDateTime periodStart) {
        this.periodStart = periodStart;
    }
    
    public LocalDateTime getPeriodEnd() {
        return periodEnd;
    }
    
    public void setPeriodEnd(LocalDateTime periodEnd) {
        this.periodEnd = periodEnd;
    }
    
    public Map<MetricTypeEntity, BigDecimal> getMetricValues() {
        return metricValues;
    }
    
    public void setMetricValues(Map<MetricTypeEntity, BigDecimal> metricValues) {
        this.metricValues = metricValues != null ? metricValues : new HashMap<>();
    }
    
    public LocalDateTime getCollectedAt() {
        return collectedAt;
    }
    
    public void setCollectedAt(LocalDateTime collectedAt) {
        this.collectedAt = collectedAt;
    }
    
    public LocalDateTime getCreatedAt() {
        return createdAt;
    }
    
    public void setCreatedAt(LocalDateTime createdAt) {
        this.createdAt = createdAt;
    }
    
    public LocalDateTime getUpdatedAt() {
        return updatedAt;
    }
    
    public void setUpdatedAt(LocalDateTime updatedAt) {
        this.updatedAt = updatedAt;
    }
    
    public Long getVersion() {
        return version;
    }
    
    public void setVersion(Long version) {
        this.version = version;
    }
}
