package stirling.software.common.configuration;

import java.io.File;

import lombok.extern.slf4j.Slf4j;

@Slf4j
public class InstallationPathConfig {
    private static final String BASE_PATH;

    // Root paths
    private static final String LOG_PATH;
    private static final String CONFIG_PATH;
    private static final String CUSTOM_FILES_PATH;
    private static final String CLIENT_WEBUI_PATH;
    private static final String PIPELINE_PATH;

    // Config paths
    private static final String SETTINGS_PATH;
    private static final String CUSTOM_SETTINGS_PATH;
    private static final String SCRIPTS_PATH;
    private static final String BACKUP_PATH;

    // Backup paths
    private static final String BACKUP_DB_PATH;
    private static final String BACKUP_PRIVATE_KEY_PATH;

    // Custom file paths
    private static final String STATIC_PATH;
    private static final String TEMPLATES_PATH;
    private static final String SIGNATURES_PATH;
    private static final String PRIVATE_KEY_PATH;

    static {
        BASE_PATH = initializeBasePath();

        // Initialize root paths
        LOG_PATH = BASE_PATH + "logs" + File.separator;
        CONFIG_PATH = BASE_PATH + "configs" + File.separator;
        CUSTOM_FILES_PATH = BASE_PATH + "customFiles" + File.separator;
        CLIENT_WEBUI_PATH = BASE_PATH + "clientWebUI" + File.separator;
        PIPELINE_PATH = BASE_PATH + "pipeline" + File.separator;

        // Initialize config paths
        SETTINGS_PATH = CONFIG_PATH + "settings.yml";
        CUSTOM_SETTINGS_PATH = CONFIG_PATH + "custom_settings.yml";
        SCRIPTS_PATH = CONFIG_PATH + "scripts" + File.separator;
        BACKUP_PATH = CONFIG_PATH + "backup" + File.separator;

        // Initialize backup paths
        BACKUP_DB_PATH = BACKUP_PATH + "db" + File.separator;
        BACKUP_PRIVATE_KEY_PATH = BACKUP_PATH + "keys" + File.separator;

        // Initialize custom file paths
        STATIC_PATH = CUSTOM_FILES_PATH + "static" + File.separator;
        TEMPLATES_PATH = CUSTOM_FILES_PATH + "templates" + File.separator;
        SIGNATURES_PATH = CUSTOM_FILES_PATH + "signatures" + File.separator;
        PRIVATE_KEY_PATH = CONFIG_PATH + "db" + File.separator + "keys" + File.separator;
    }

    private static String initializeBasePath() {
        // Allow tests / harnesses to redirect the entire state tree (configs,
        // backups, customFiles, pipeline, logs) to an isolated location via
        // -Dstirling.base-path=... or STIRLING_BASE_PATH=... so a Playwright
        // run never touches a developer's working state.
        String override = System.getProperty("stirling.base-path");
        if (override == null || override.isBlank()) {
            override = System.getenv("STIRLING_BASE_PATH");
        }
        if (override != null && !override.isBlank()) {
            boolean hasTrailingSeparator = override.endsWith("/") || override.endsWith("\\");
            return hasTrailingSeparator ? override : override + File.separator;
        }
        return "." + File.separator;
    }

    public static String getPath() {
        return BASE_PATH;
    }

    public static String getLogPath() {
        return LOG_PATH;
    }

    public static String getConfigPath() {
        return CONFIG_PATH;
    }

    public static String getCustomFilesPath() {
        return CUSTOM_FILES_PATH;
    }

    public static String getClientWebUIPath() {
        return CLIENT_WEBUI_PATH;
    }

    public static String getScriptsPath() {
        return SCRIPTS_PATH;
    }

    public static String getPipelinePath() {
        return PIPELINE_PATH;
    }

    public static String getSettingsPath() {
        return SETTINGS_PATH;
    }

    public static String getCustomSettingsPath() {
        return CUSTOM_SETTINGS_PATH;
    }

    public static String getStaticPath() {
        return STATIC_PATH;
    }

    public static String getTemplatesPath() {
        return TEMPLATES_PATH;
    }

    public static String getSignaturesPath() {
        return SIGNATURES_PATH;
    }

    public static String getPrivateKeyPath() {
        return BACKUP_PRIVATE_KEY_PATH;
    }

    public static String getBackupPath() {
        return BACKUP_DB_PATH;
    }
}
