"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.createUpCommand = createUpCommand;
const docker_compose_1 = require("../services/docker-compose");
const docker_network_1 = require("../services/docker-network");
const ssh_manager_1 = require("../services/ssh-manager");
const docker_volume_1 = require("../services/docker-volume");
const docker_checker_1 = require("../utils/docker-checker");
const env_loader_1 = require("../utils/env-loader");
const project_files_1 = require("../utils/project-files");
const logger_1 = require("../utils/logger");
const setup_1 = require("./setup");
const i18n_1 = require("../i18n");
const command_1 = require("../utils/command");
function ensureDocker() {
    if (!docker_checker_1.dockerChecker.isInstalled()) {
        throw new Error('Docker is not installed or not in PATH');
    }
    if (!docker_checker_1.dockerChecker.composePluginInstalled()) {
        throw new Error('Docker Compose plugin is not installed');
    }
}
async function loadOrSetup(options) {
    let config = env_loader_1.envLoader.load();
    if (!env_loader_1.envLoader.hasRequiredVars(config)) {
        logger_1.logger.warn('Runestone is not configured yet. Starting setup.');
        config = await (0, setup_1.runSetup)();
    }
    (0, project_files_1.ensureProjectFiles)(config);
    return config;
}
function createUpCommand() {
    return (0, command_1.createCommand)('up')
        .description((0, i18n_1.t)('commands.up.description'))
        .option('--force-recreate', (0, i18n_1.t)('options.forceRecreate.description'))
        .option('--no-deps', (0, i18n_1.t)('options.noDeps.description'))
        .action(async (options) => {
        try {
            const config = await loadOrSetup(options);
            ensureDocker();
            if (!docker_network_1.networkService.exists(config.NETWORK_NAME)) {
                logger_1.logger.info(`Creating Docker network '${config.NETWORK_NAME}'`);
                docker_network_1.networkService.createNetwork(config.NETWORK_NAME);
            }
            if (!docker_volume_1.volumeService.exists(config.SSH_VOLUME_NAME)) {
                logger_1.logger.info(`Creating Docker volume '${config.SSH_VOLUME_NAME}'`);
                docker_volume_1.volumeService.createVolume(config.SSH_VOLUME_NAME);
            }
            logger_1.logger.info('Starting Docker Compose services');
            docker_compose_1.composeService.up(config.COMPOSE_FILE_PATH, {
                wait: true,
                forceRecreate: Boolean(options.forceRecreate),
                noDeps: options.deps === false
            });
            if (process.env.RUNESTONE_SKIP_SSH_KEYS === '1') {
                logger_1.logger.warn('Skipping SSH key injection because RUNESTONE_SKIP_SSH_KEYS=1.');
            }
            else {
                logger_1.logger.info('Injecting SSH keys');
                ssh_manager_1.sshManager.addKeys(undefined, config.SSH_VOLUME_NAME);
            }
            logger_1.logger.success(`runestone is ready at https://traefik.${config.HOST_DOMAIN}`);
            logger_1.logger.info(`Mailpit: http://mailpit.${config.HOST_DOMAIN}`);
        }
        catch (error) {
            const message = error instanceof Error ? error.message : String(error);
            logger_1.logger.error(`Failed to start environment: ${message}`);
            process.exit(1);
        }
    });
}
//# sourceMappingURL=up.js.map