package com.gnx.telemedicine.config;

import lombok.Getter;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;

/**
 * Configuration for API versioning.
 * Supports multiple API versions with deprecation and sunset policies.
 */
@Configuration
@Getter
public class ApiVersionConfig {
    
    /**
     * Current API version.
     */
    @Value("${api.version.current:v1}")
    private String currentVersion;
    
    /**
     * Minimum supported API version.
     */
    @Value("${api.version.minimum:v1}")
    private String minimumVersion;
    
    /**
     * API version prefix (e.g., "/api/v3").
     */
    @Value("${api.version.prefix:/api}")
    private String versionPrefix;
    
    /**
     * Enable API versioning.
     */
    @Value("${api.versioning.enabled:true}")
    private boolean versioningEnabled;
    
    /**
     * Show version in response headers.
     */
    @Value("${api.versioning.show-in-headers:true}")
    private boolean showVersionInHeaders;
    
    /**
     * Show deprecation warnings in response headers.
     */
    @Value("${api.versioning.show-deprecation-warnings:true}")
    private boolean showDeprecationWarnings;
    
    // Explicit getters for boolean fields (Lombok @Getter should generate these, but adding for safety)
    public boolean isVersioningEnabled() {
        return versioningEnabled;
    }
    
    public boolean isShowVersionInHeaders() {
        return showVersionInHeaders;
    }
    
    public boolean isShowDeprecationWarnings() {
        return showDeprecationWarnings;
    }
    
    /**
     * Get the full API path for a version.
     * @param version API version (e.g., "v1")
     * @return Full API path (e.g., "/api/v3")
     */
    public String getApiPath(String version) {
        return versionPrefix + "/" + version;
    }
    
    /**
     * Get the current API path.
     * @return Current API path (e.g., "/api/v3")
     */
    public String getCurrentApiPath() {
        return getApiPath(currentVersion);
    }
    
    /**
     * Check if a version is supported.
     * @param version API version to check
     * @return true if version is supported
     */
    public boolean isVersionSupported(String version) {
        if (!versioningEnabled) {
            return true; // If versioning is disabled, all versions are supported
        }
        
        // Extract version number (e.g., "v1" -> 1)
        int versionNum = extractVersionNumber(version);
        int minVersionNum = extractVersionNumber(minimumVersion);
        int currentVersionNum = extractVersionNumber(currentVersion);
        
        return versionNum >= minVersionNum && versionNum <= currentVersionNum;
    }
    
    /**
     * Extract version number from version string.
     * @param version Version string (e.g., "v1", "1")
     * @return Version number
     */
    private int extractVersionNumber(String version) {
        if (version == null || version.isEmpty()) {
            return 1; // Default to v1
        }
        
        // Remove "v" prefix if present
        String versionNum = version.startsWith("v") ? version.substring(1) : version;
        
        try {
            return Integer.parseInt(versionNum);
        } catch (NumberFormatException e) {
            return 1; // Default to v1 if parsing fails
        }
    }
}

