package com.social.media.domain.analytics.valueobject;

/**
 * Enumeration of time periods for analytics aggregation
 */
public enum TimePeriod {
    HOURLY("HORARIO", 1),
    DAILY("DIARIO", 24),
    WEEKLY("SEMANAL", 24 * 7),
    MONTHLY("MENSAL", 24 * 30),
    QUARTERLY("TRIMESTRAL", 24 * 90),
    YEARLY("ANUAL", 24 * 365);
    
    private final String dbValue;
    private final int hours;
    
    TimePeriod(String dbValue, int hours) {
        this.dbValue = dbValue;
        this.hours = hours;
    }
    
    public String getDbValue() {
        return dbValue;
    }
    
    public int getHours() {
        return hours;
    }
    
    public int getDays() {
        return hours / 24;
    }
    
    public boolean isShortTerm() {
        return this == HOURLY || this == DAILY;
    }
    
    public boolean isMediumTerm() {
        return this == WEEKLY || this == MONTHLY;
    }
    
    public boolean isLongTerm() {
        return this == QUARTERLY || this == YEARLY;
    }
    
    public TimePeriod getNextLargerPeriod() {
        return switch (this) {
            case HOURLY -> DAILY;
            case DAILY -> WEEKLY;
            case WEEKLY -> MONTHLY;
            case MONTHLY -> QUARTERLY;
            case QUARTERLY -> YEARLY;
            case YEARLY -> YEARLY; // No larger period
        };
    }
    
    public TimePeriod getNextSmallerPeriod() {
        return switch (this) {
            case YEARLY -> QUARTERLY;
            case QUARTERLY -> MONTHLY;
            case MONTHLY -> WEEKLY;
            case WEEKLY -> DAILY;
            case DAILY -> HOURLY;
            case HOURLY -> HOURLY; // No smaller period
        };
    }
    
    public static TimePeriod fromDbValue(String dbValue) {
        for (TimePeriod period : values()) {
            if (period.dbValue.equals(dbValue)) {
                return period;
            }
        }
        throw new IllegalArgumentException("Unknown time period: " + dbValue);
    }
}
