import constants from './constants.js';
import path from 'node:path';

function baseDir() {
    const homeDir = process.env.HOME;
    if (constants.CLOUDRON) return homeDir;
    if (constants.TEST) return path.join(homeDir, '.cloudron_test');
    // cannot reach
}

function dashboardDir() {
    return constants.TEST ? path.resolve(import.meta.dirname, '../dashboard/dist') : path.join(baseDir(), 'box/dashboard/dist');
}

// keep these values in sync with start.sh
export default {
    baseDir,

    CLOUDRON_DEFAULT_AVATAR_FILE: path.join(import.meta.dirname, 'avatar.png'),
    INFRA_VERSION_FILE: path.join(baseDir(), 'platformdata/INFRA_VERSION'),
    CRON_SEED_FILE: path.join(baseDir(), 'platformdata/CRON_SEED'),
    TRANSLATIONS_DIR: path.join(dashboardDir(), 'translation'),
    DASHBOARD_DIR: dashboardDir(),

    PROVIDER_FILE: '/etc/cloudron/PROVIDER',

    DEFAULT_BACKUP_DIR: constants.TEST ? '/tmp/backups' : '/var/backups',
    VOLUMES_MOUNT_DIR: '/mnt/volumes',
    MANAGED_BACKUP_MOUNT_DIR: '/mnt/managedbackups',
    DOCKER_SOCKET_PATH: '/var/run/docker.sock',

    PLATFORM_DATA_DIR: path.join(baseDir(), 'platformdata'),
    PERSISTENT_DATA_DIR: path.join(baseDir(), 'platformdata/persistent'),
    APPS_DATA_DIR: path.join(baseDir(), 'appsdata'),
    SOURCE_ARCHIVES_DIR: path.join(baseDir(), 'platformdata/source-archives'),

    ACME_CHALLENGES_DIR: path.join(baseDir(), 'platformdata/acme'),
    ADDON_CONFIG_DIR: path.join(baseDir(), 'platformdata/addons'),
    MAIL_CONFIG_DIR: path.join(baseDir(), 'platformdata/addons/mail'),
    LOGROTATE_CONFIG_DIR: path.join(baseDir(), 'platformdata/logrotate.d'),
    NGINX_CONFIG_DIR: path.join(baseDir(), 'platformdata/nginx'),
    NGINX_APPCONFIG_DIR: path.join(baseDir(), 'platformdata/nginx/applications'),
    NGINX_CERT_DIR: path.join(baseDir(), 'platformdata/nginx/cert'),
    BACKUP_INFO_DIR: path.join(baseDir(), 'platformdata/backup'), // contains /{backupSite.id}/
    UPDATE_DIR: path.join(baseDir(), 'platformdata/update'),
    BOX_UPDATE_FILE: path.join(baseDir(), 'platformdata/update/boxupdate.json'),
    DISK_USAGE_EXCLUDE_FILE: path.join(baseDir(), 'platformdata/diskusage/exclude'),
    DYNDNS_INFO_FILE: path.join(baseDir(), 'platformdata/dyndns-info.json'),
    DHPARAMS_FILE: path.join(baseDir(), 'platformdata/dhparams.pem'),
    FEATURES_INFO_FILE: path.join(baseDir(), 'platformdata/features-info.json'),
    VERSION_FILE: path.join(baseDir(), 'platformdata/VERSION'),
    CIFS_CREDENTIALS_DIR: path.join(baseDir(), 'platformdata/cifs'),
    SSHFS_KEYS_DIR: path.join(baseDir(), 'platformdata/sshfs'),
    SFTP_KEYS_DIR: path.join(baseDir(), 'platformdata/sftp/ssh'),
    FIREWALL_BLOCKLIST_FILE: path.join(baseDir(), 'platformdata/firewall/blocklist.txt'),
    LDAP_ALLOWLIST_FILE: path.join(baseDir(), 'platformdata/firewall/ldap_allowlist.txt'),
    REVERSE_PROXY_REBUILD_FILE: path.join(baseDir(), 'platformdata/nginx/rebuild-needed'),
    NGINX_TRUSTED_IPS_FILE: path.join(baseDir(), 'platformdata/nginx/trusted.ips'),

    BOX_DATA_DIR: path.join(baseDir(), 'boxdata/box'),
    MAIL_DATA_DIR: path.join(baseDir(), 'boxdata/mail'),

    LOG_DIR: path.join(baseDir(), 'platformdata/logs'),
    TASKS_LOG_DIR: path.join(baseDir(), 'platformdata/logs/tasks'),
    BOX_LOG_FILE: path.join(baseDir(), 'platformdata/logs/box.log'),

    GHOST_USER_FILE: path.join(baseDir(), 'platformdata/cloudron_ghost.json'),

    // this pattern is for the cloudron logs API route to work
    BACKUP_LOG_FILE: path.join(baseDir(), 'platformdata/logs/backup/app.log'),
    UPDATER_LOG_FILE: path.join(baseDir(), 'platformdata/logs/updater/app.log'),

    SYSLOG_SOCKET_FILE: path.join(baseDir(), 'platformdata/logs/syslog.sock'),

    OIDC_STORE_DIR: path.join(baseDir(), 'platformdata/oidc'),
};
