"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    var desc = Object.getOwnPropertyDescriptor(m, k);
    if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
      desc = { enumerable: true, get: function() { return m[k]; } };
    }
    Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
    var ownKeys = function(o) {
        ownKeys = Object.getOwnPropertyNames || function (o) {
            var ar = [];
            for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
            return ar;
        };
        return ownKeys(o);
    };
    return function (mod) {
        if (mod && mod.__esModule) return mod;
        var result = {};
        if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
        __setModuleDefault(result, mod);
        return result;
    };
})();
Object.defineProperty(exports, "__esModule", { value: true });
const fs = __importStar(require("fs"));
const path = __importStar(require("path"));
const child_process_1 = require("child_process");
function assert(condition, message) {
    if (!condition)
        throw new Error(message);
}
function run(cmd) {
    return (0, child_process_1.execSync)(cmd, { encoding: 'utf-8', stdio: ['pipe', 'pipe', 'pipe'] }).trim();
}
const root = path.join(__dirname, '..');
const artifacts = path.join(root, '.artifacts');
const servicesDir = path.join(artifacts, 'workspaces', 'default', 'services');
const eventsDir = path.join(servicesDir, 'events');
const missionsDir = path.join(artifacts, 'missions');
fs.mkdirSync(eventsDir, { recursive: true });
fs.mkdirSync(missionsDir, { recursive: true });
const serviceId = `svc_test_${Date.now()}`;
const servicePath = path.join(servicesDir, `${serviceId}.json`);
const service = {
    serviceId,
    objective: 'Teste CLI service commands',
    status: 'active',
    riskProfile: 'medium',
    slo: {},
    kpis: {},
    budget: { hardStop: false },
    missionIds: [],
    createdAt: new Date().toISOString(),
    updatedAt: new Date().toISOString()
};
fs.writeFileSync(servicePath, JSON.stringify(service, null, 2), 'utf-8');
const statusOut = run(`node dist/index.js service status ${serviceId}`);
assert(statusOut.includes('"serviceId":'), 'service status should print service json');
run(`node dist/index.js service pause ${serviceId} --reason "teste pausa"`);
const paused = JSON.parse(fs.readFileSync(servicePath, 'utf-8'));
assert(paused.status === 'paused', 'service pause should set paused');
run(`node dist/index.js service resume ${serviceId} --reason "teste resume"`);
const resumed = JSON.parse(fs.readFileSync(servicePath, 'utf-8'));
assert(resumed.status === 'active', 'service resume should set active');
const eventsOut = run(`node dist/index.js service events ${serviceId} --limit 5`);
assert(eventsOut.includes('service_paused') || eventsOut.includes('service_resumed'), 'service events should include status transitions');
const taskId = `task_test_${Date.now()}`;
const missionPath = path.join(missionsDir, `${taskId}.json`);
fs.writeFileSync(missionPath, JSON.stringify({ taskId, status: 'completed', mode: 'task' }, null, 2), 'utf-8');
const taskOut = run(`node dist/index.js task status ${taskId}`);
assert(taskOut.includes('"taskId"'), 'task status should print task json');
console.log('TEST_CLI_SERVICE_COMMANDS_OK');
