package com.social.media.domain.shared.enums;

/**
 * Enumeration of company statuses in the system.
 * 
 * This enum defines the possible states a company can be in throughout their lifecycle
 * in the Social Media Manager platform, including business and billing statuses.
 * 
 * @author Social Media Manager Team
 * @version 2.0
 * @since 2025-01-01
 */
public enum CompanyStatus {
    
    /**
     * Company is active and fully operational.
     * All features are available based on the company's plan.
     */
    ATIVO("Ativo", "Company is active and operational", true),
    
    /**
     * Company is active and fully operational (English version).
     * All features are available based on the company's plan.
     */
    ACTIVE("Active", "Company is active and operational", true),
    
    /**
     * Company is temporarily inactive.
     * Access is suspended but data is preserved.
     */
    INATIVO("Inativo", "Company is temporarily inactive", false),
    
    /**
     * Company is suspended due to policy violations or billing issues.
     * Access is restricted pending resolution.
     */
    SUSPENSO("Suspenso", "Company is suspended", false),
    
    /**
     * Company subscription has been cancelled.
     * Limited access during grace period, then data archival.
     */
    CANCELADO("Cancelado", "Company subscription is cancelled", false);
    
    private final String displayName;
    private final String description;
    private final boolean allowsAccess;
    
    CompanyStatus(String displayName, String description, boolean allowsAccess) {
        this.displayName = displayName;
        this.description = description;
        this.allowsAccess = allowsAccess;
    }
    
    /**
     * Gets the human-readable display name of the status.
     * 
     * @return the display name
     */
    public String getDisplayName() {
        return displayName;
    }
    
    /**
     * Gets the description of what this status means.
     * 
     * @return the description
     */
    public String getDescription() {
        return description;
    }
    
    /**
     * Checks if users from this company can access the system.
     * 
     * @return true if access is allowed
     */
    public boolean allowsAccess() {
        return allowsAccess;
    }
    
    /**
     * Checks if the company can create new content.
     * 
     * @return true if content creation is allowed
     */
    public boolean allowsContentCreation() {
        return this == ATIVO;
    }
    
    /**
     * Checks if the company can publish content to social media.
     * 
     * @return true if publishing is allowed
     */
    public boolean allowsPublishing() {
        return this == ATIVO;
    }
    
    /**
     * Checks if the company can access analytics and reports.
     * 
     * @return true if analytics access is allowed
     */
    public boolean allowsAnalytics() {
        return allowsAccess; // Suspended companies can still view analytics
    }
    
    /**
     * Checks if the company status requires immediate attention.
     * 
     * @return true if the status needs attention
     */
    public boolean needsAttention() {
        return this == SUSPENSO || this == CANCELADO;
    }
    
    /**
     * Checks if the company is in a terminal state (cancelled).
     * 
     * @return true if the company is in a terminal state
     */
    public boolean isTerminal() {
        return this == CANCELADO;
    }
    
    /**
     * Gets the possible status transitions from the current status.
     * 
     * @return array of possible next statuses
     */
    public CompanyStatus[] getPossibleTransitions() {
        return switch (this) {
            case ATIVO, ACTIVE -> new CompanyStatus[]{INATIVO, SUSPENSO, CANCELADO};
            case INATIVO -> new CompanyStatus[]{ATIVO, ACTIVE, SUSPENSO, CANCELADO};
            case SUSPENSO -> new CompanyStatus[]{ATIVO, ACTIVE, INATIVO, CANCELADO};
            case CANCELADO -> new CompanyStatus[]{}; // Terminal state
        };
    }
    
    /**
     * Checks if transition to the target status is allowed.
     * 
     * @param targetStatus the status to transition to
     * @return true if the transition is allowed
     */
    public boolean canTransitionTo(CompanyStatus targetStatus) {
        if (targetStatus == null || targetStatus == this) {
            return false;
        }
        
        CompanyStatus[] allowedTransitions = getPossibleTransitions();
        for (CompanyStatus allowed : allowedTransitions) {
            if (allowed == targetStatus) {
                return true;
            }
        }
        
        return false;
    }
    
    /**
     * Gets the grace period in days for cancelled companies.
     * 
     * @return grace period in days, or 0 if not applicable
     */
    public int getGracePeriodDays() {
        return switch (this) {
            case CANCELADO -> 30; // 30 days grace period for cancelled companies
            case SUSPENSO -> 7;   // 7 days for suspended companies
            case ATIVO, ACTIVE, INATIVO -> 0;
        };
    }
}
