/**
 * Export Command - Generate Deployment Artifacts
 *
 * ARCHITECTURE:
 * - This module supports both orchestration commands AND local export generation
 * - Local exports generate deployment artifacts
 * - Orchestration routes to agentics-deployment-exporters
 *
 * SUBCOMMANDS (Local Export):
 * - terraform   Generate Terraform deployment artifacts
 * - kubernetes  Generate Kubernetes deployment manifests
 * - erp         Generate ERP integration exports
 *
 * ORCHESTRATION (External):
 * - Default command executes via agentics-deployment-exporters
 */
import * as fs from 'node:fs';
import * as path from 'node:path';
import { isNaturalLanguage } from '../types/index.js';
import { ExportersAdapter } from '../adapters/base-adapter.js';
import { createOrchestrationEngine, } from '../modules/orchestration-engine.js';
import { createArtifactHandoff } from '../modules/artifact-handoff.js';
import { createAuditTrail } from '../audit/audit-trail.js';
import { failFast, toError, CLIError } from '../errors/index.js';
import { loadEndpointConfig } from '../config/endpoints.js';
import { validateRequest, validateResponse, RequestSchemas, ResponseSchemas, } from '../contracts/validator.js';
// ============================================================================
// Export-Specific Exit Codes
// ============================================================================
export const EXPORT_EXIT_CODES = {
    SUCCESS: 0,
    INPUT_ERROR: 100,
    NOT_FOUND: 100,
    IO_ERROR: 74,
    UNEXPECTED_ERROR: 1,
};
// ============================================================================
// Storage Configuration
// ============================================================================
function getExportsDir() {
    const baseDir = process.env['AGENTICS_DATA_DIR'] ?? process.cwd();
    return path.join(baseDir, '.agentics', 'exports');
}
function ensureExportsDir() {
    const dir = getExportsDir();
    if (!fs.existsSync(dir)) {
        fs.mkdirSync(dir, { recursive: true });
    }
}
// ============================================================================
// ID Validation
// ============================================================================
function validateTargetId(id, correlationId) {
    if (!id || typeof id !== 'string' || id.trim() === '') {
        throw new CLIError({
            code: 'ECLI-EXPORT-001',
            category: 'INPUT_ERROR',
            message: 'Target ID is required',
            details: { provided: id },
            module: 'export',
            correlationId,
            recoverable: false,
            exitCode: EXPORT_EXIT_CODES.INPUT_ERROR,
        });
    }
}
// ============================================================================
// Display Formatting
// ============================================================================
export function formatExportForDisplay(result) {
    const lines = [];
    lines.push(`Export: ${result.export_type}`);
    lines.push(`Target: ${result.target_id}`);
    lines.push('');
    lines.push(`Generated ${result.artifacts.length} artifact${result.artifacts.length === 1 ? '' : 's'}:`);
    lines.push('');
    for (const artifact of result.artifacts) {
        lines.push(`  ${artifact.filename} (${artifact.type})`);
    }
    lines.push('');
    if (result.written && result.output_path) {
        lines.push(`Written to: ${result.output_path}`);
    }
    else {
        lines.push('(Artifacts generated but not written to disk)');
    }
    return lines.join('\n');
}
// ============================================================================
// Terraform Export Generation
// ============================================================================
function generateTerraformArtifacts(targetId) {
    const mainTf = `# Generated by agentics-cli
# Target: ${targetId}
# Generated at: ${new Date().toISOString()}

terraform {
  required_version = ">= 1.0"
  required_providers {
    google = {
      source  = "hashicorp/google"
      version = "~> 5.0"
    }
  }
}

provider "google" {
  # Configuration will be provided via environment
}

# Placeholder resource for target: ${targetId}
resource "google_project" "agentics_deployment" {
  name       = "agentics-${targetId}"
  project_id = "agentics-${targetId}"

  labels = {
    managed_by = "agentics-cli"
    target_id  = "${targetId}"
  }
}

# Secret Manager — store credentials securely, never in plaintext
resource "google_secret_manager_secret" "db_password" {
  secret_id = "agentics-${targetId}-db-password"
  project   = google_project.agentics_deployment.project_id

  replication {
    auto {}
  }
}

resource "google_secret_manager_secret_version" "db_password" {
  secret      = google_secret_manager_secret.db_password.id
  secret_data = var.db_password
}
`;
    const variablesTf = `# Variables for agentics deployment
# Target: ${targetId}

variable "project_id" {
  description = "The GCP project ID"
  type        = string
  default     = "agentics-${targetId}"
}

variable "region" {
  description = "The GCP region"
  type        = string
  default     = "us-central1"
}

variable "environment" {
  description = "Deployment environment"
  type        = string
  default     = "development"
}

variable "db_password" {
  description = "Database password — supply via TF_VAR_db_password or a .tfvars file excluded from VCS"
  type        = string
  sensitive   = true
}
`;
    const outputsTf = `# Outputs for agentics deployment
# Target: ${targetId}

output "project_id" {
  description = "The project ID"
  value       = google_project.agentics_deployment.project_id
}

output "deployment_timestamp" {
  description = "When this was generated"
  value       = "${new Date().toISOString()}"
}
`;
    return [
        { filename: 'main.tf', content: mainTf, type: 'terraform' },
        { filename: 'variables.tf', content: variablesTf, type: 'terraform' },
        { filename: 'outputs.tf', content: outputsTf, type: 'terraform' },
    ];
}
// ============================================================================
// Kubernetes Export Generation
// ============================================================================
function generateKubernetesArtifacts(targetId) {
    const namespace = `apiVersion: v1
kind: Namespace
metadata:
  name: agentics-${targetId}
  labels:
    managed-by: agentics-cli
    target-id: "${targetId}"
`;
    const deployment = `apiVersion: apps/v1
kind: Deployment
metadata:
  name: agentics-app
  namespace: agentics-${targetId}
  labels:
    app: agentics
    target-id: "${targetId}"
spec:
  replicas: 1
  selector:
    matchLabels:
      app: agentics
  template:
    metadata:
      labels:
        app: agentics
    spec:
      containers:
      - name: app
        image: placeholder:latest
        ports:
        - containerPort: 8080
        env:
        - name: TARGET_ID
          value: "${targetId}"
        - name: DEPLOYMENT_TIME
          value: "${new Date().toISOString()}"
        resources:
          limits:
            cpu: "500m"
            memory: "512Mi"
          requests:
            cpu: "250m"
            memory: "256Mi"
`;
    const service = `apiVersion: v1
kind: Service
metadata:
  name: agentics-service
  namespace: agentics-${targetId}
spec:
  selector:
    app: agentics
  ports:
  - port: 80
    targetPort: 8080
  type: ClusterIP
`;
    const configMap = `apiVersion: v1
kind: ConfigMap
metadata:
  name: agentics-config
  namespace: agentics-${targetId}
data:
  TARGET_ID: "${targetId}"
  GENERATED_AT: "${new Date().toISOString()}"
  MANAGED_BY: "agentics-cli"
`;
    return [
        { filename: 'namespace.yaml', content: namespace, type: 'kubernetes' },
        { filename: 'deployment.yaml', content: deployment, type: 'kubernetes' },
        { filename: 'service.yaml', content: service, type: 'kubernetes' },
        { filename: 'configmap.yaml', content: configMap, type: 'kubernetes' },
    ];
}
// ============================================================================
// ERP Export Generation
// ============================================================================
function generateErpArtifacts(targetId) {
    const integrationConfig = JSON.stringify({
        integration: {
            id: `erp-integration-${targetId}`,
            type: 'erp-connector',
            version: '1.0.0',
            generated_at: new Date().toISOString(),
            target_id: targetId,
            managed_by: 'agentics-cli',
        },
        connection: {
            endpoint: 'https://erp.example.com/api/v1',
            auth_type: 'oauth2',
            timeout_ms: 30000,
            retry_count: 3,
        },
        mappings: {
            entities: [
                { source: 'simulation_result', target: 'erp_transaction' },
                { source: 'plan_output', target: 'erp_project' },
                { source: 'deployment_status', target: 'erp_activity' },
            ],
        },
        sync: {
            enabled: false,
            interval_minutes: 15,
            batch_size: 100,
        },
    }, null, 2);
    const dataMapping = JSON.stringify({
        data_mapping: {
            version: '1.0.0',
            target_id: targetId,
            fields: [
                { source_field: 'id', target_field: 'external_id', transform: 'passthrough' },
                { source_field: 'name', target_field: 'description', transform: 'passthrough' },
                { source_field: 'status', target_field: 'state', transform: 'status_map' },
                { source_field: 'created_at', target_field: 'created_date', transform: 'iso_to_erp_date' },
                { source_field: 'metrics.total_cost', target_field: 'amount', transform: 'currency' },
            ],
            transforms: {
                status_map: {
                    completed: 'CLOSED',
                    pending: 'OPEN',
                    failed: 'ERROR',
                    draft: 'DRAFT',
                },
            },
        },
    }, null, 2);
    return [
        { filename: 'erp-integration.json', content: integrationConfig, type: 'json' },
        { filename: 'data-mapping.json', content: dataMapping, type: 'json' },
    ];
}
// ============================================================================
// Export Command Implementation
// ============================================================================
export async function executeExportCommand(input, options) {
    const correlationId = options.trace_id ?? crypto.randomUUID();
    const startTime = Date.now();
    // Initialize components
    const engine = createOrchestrationEngine(correlationId);
    const handoff = createArtifactHandoff();
    const audit = createAuditTrail();
    // Create adapter
    const exportersConfig = loadEndpointConfig('agentics-deployment-exporters');
    const exportersAdapter = new ExportersAdapter(exportersConfig, correlationId);
    // Define orchestration stages
    const stages = [
        {
            name: 'generate-export',
            adapter: exportersAdapter,
            request: {
                endpoint: '/v1/exporters',
                method: 'POST',
                buildBody: () => {
                    let requestBody;
                    if (isNaturalLanguage(input.intentRef)) {
                        // ============================================================================
                        // CONTRACT VALIDATION - Natural language format
                        // ============================================================================
                        const desc = input.intentRef.description;
                        requestBody = {
                            intent_ref: {
                                id: desc,
                                plan_id: desc,
                            },
                            format_spec: {
                                format: input.format,
                            },
                        };
                    }
                    else {
                        // ============================================================================
                        // CONTRACT VALIDATION - Structured reference format
                        // Service expects: intent_ref.id and intent_ref.plan_id
                        // ============================================================================
                        const intentRef = input.intentRef;
                        requestBody = {
                            intent_ref: {
                                id: intentRef.id,
                                plan_id: intentRef.id,
                            },
                            format_spec: {
                                format: input.format,
                            },
                        };
                    }
                    // Validate request against contract BEFORE sending
                    validateRequest(RequestSchemas.EXPORT, requestBody, correlationId);
                    return requestBody;
                },
            },
        },
    ];
    try {
        // Execute orchestration
        const result = await engine.execute(stages, options);
        if (!result.success) {
            const failedStage = result.stages.find(s => s.status === 'failed');
            throw new Error(failedStage?.error?.message ?? 'Export generation failed');
        }
        // ============================================================================
        // RESPONSE VALIDATION - execution_metadata REQUIRED
        // ============================================================================
        const exportStage = result.stages.find(s => s.name === 'generate-export');
        const validatedExport = validateResponse(ResponseSchemas.EXPORT, exportStage?.response, correlationId);
        const exportRef = handoff.extractReference(validatedExport);
        // Record audit entry
        const intentRefId = isNaturalLanguage(input.intentRef)
            ? `nl:${input.intentRef.description.slice(0, 50)}`
            : input.intentRef.id;
        audit.record({
            command: 'export',
            inputs: { intentRef: intentRefId, format: input.format, outputPath: input.outputPath },
            outputs: exportRef,
            dependencies: [
                { service: 'agentics-deployment-exporters', method: 'generate', timestamp: new Date().toISOString(), duration_ms: result.timing.total, success: true },
            ],
            duration_ms: result.timing.total,
            status: 'SUCCESS',
            trace_id: correlationId,
        });
        return {
            reference: exportRef,
            timing: Date.now() - startTime,
        };
    }
    catch (error) {
        // Record failure in audit
        const failedIntentRefId = isNaturalLanguage(input.intentRef)
            ? `nl:${input.intentRef.description.slice(0, 50)}`
            : input.intentRef.id;
        audit.record({
            command: 'export',
            inputs: { intentRef: failedIntentRefId, format: input.format },
            outputs: null,
            dependencies: [],
            duration_ms: Date.now() - startTime,
            status: 'FAILED',
            error_code: error instanceof Error ? error.name : 'UNKNOWN',
            error_message: error instanceof Error ? error.message : 'Unknown error',
            trace_id: correlationId,
        });
        // Fail fast
        failFast({
            command: 'export',
            phase: 'generation',
            upstreamError: toError(error),
            inputs: input,
            correlationId,
        });
    }
}
// ============================================================================
// Local Export Commands - Terraform
// ============================================================================
export async function executeExportTerraformCommand(input, options) {
    const correlationId = options.trace_id ?? crypto.randomUUID();
    const startTime = Date.now();
    // Validate input
    validateTargetId(input.target_id, correlationId);
    // Generate artifacts
    const artifacts = generateTerraformArtifacts(input.target_id);
    // Write to disk if output path specified
    let written = false;
    let outputPath = input.output_path;
    if (outputPath) {
        try {
            ensureExportsDir();
            const exportDir = path.join(getExportsDir(), 'terraform', input.target_id);
            if (!fs.existsSync(exportDir)) {
                fs.mkdirSync(exportDir, { recursive: true });
            }
            for (const artifact of artifacts) {
                const filePath = path.join(exportDir, artifact.filename);
                fs.writeFileSync(filePath, artifact.content, 'utf-8');
            }
            outputPath = exportDir;
            written = true;
        }
        catch (error) {
            throw new CLIError({
                code: 'ECLI-EXPORT-002',
                category: 'INTERNAL_ERROR',
                message: `Failed to write terraform artifacts: ${error instanceof Error ? error.message : 'Unknown error'}`,
                details: { output_path: outputPath, target_id: input.target_id },
                module: 'export',
                correlationId,
                recoverable: false,
                exitCode: EXPORT_EXIT_CODES.IO_ERROR,
            });
        }
    }
    return {
        export_type: 'terraform',
        target_id: input.target_id,
        artifacts,
        output_path: outputPath,
        written,
        timing: Date.now() - startTime,
    };
}
// ============================================================================
// Local Export Commands - Kubernetes
// ============================================================================
export async function executeExportKubernetesCommand(input, options) {
    const correlationId = options.trace_id ?? crypto.randomUUID();
    const startTime = Date.now();
    // Validate input
    validateTargetId(input.target_id, correlationId);
    // Generate artifacts
    const artifacts = generateKubernetesArtifacts(input.target_id);
    // Write to disk if output path specified
    let written = false;
    let outputPath = input.output_path;
    if (outputPath) {
        try {
            ensureExportsDir();
            const exportDir = path.join(getExportsDir(), 'kubernetes', input.target_id);
            if (!fs.existsSync(exportDir)) {
                fs.mkdirSync(exportDir, { recursive: true });
            }
            for (const artifact of artifacts) {
                const filePath = path.join(exportDir, artifact.filename);
                fs.writeFileSync(filePath, artifact.content, 'utf-8');
            }
            outputPath = exportDir;
            written = true;
        }
        catch (error) {
            throw new CLIError({
                code: 'ECLI-EXPORT-003',
                category: 'INTERNAL_ERROR',
                message: `Failed to write kubernetes artifacts: ${error instanceof Error ? error.message : 'Unknown error'}`,
                details: { output_path: outputPath, target_id: input.target_id },
                module: 'export',
                correlationId,
                recoverable: false,
                exitCode: EXPORT_EXIT_CODES.IO_ERROR,
            });
        }
    }
    return {
        export_type: 'kubernetes',
        target_id: input.target_id,
        artifacts,
        output_path: outputPath,
        written,
        timing: Date.now() - startTime,
    };
}
// ============================================================================
// Local Export Commands - ERP
// ============================================================================
export async function executeExportErpCommand(input, options) {
    const correlationId = options.trace_id ?? crypto.randomUUID();
    const startTime = Date.now();
    // Validate input
    validateTargetId(input.target_id, correlationId);
    // Generate artifacts
    const artifacts = generateErpArtifacts(input.target_id);
    // Write to disk if output path specified
    let written = false;
    let outputPath = input.output_path;
    if (outputPath) {
        try {
            ensureExportsDir();
            const exportDir = path.join(getExportsDir(), 'erp', input.target_id);
            if (!fs.existsSync(exportDir)) {
                fs.mkdirSync(exportDir, { recursive: true });
            }
            for (const artifact of artifacts) {
                const filePath = path.join(exportDir, artifact.filename);
                fs.writeFileSync(filePath, artifact.content, 'utf-8');
            }
            outputPath = exportDir;
            written = true;
        }
        catch (error) {
            throw new CLIError({
                code: 'ECLI-EXPORT-004',
                category: 'INTERNAL_ERROR',
                message: `Failed to write erp artifacts: ${error instanceof Error ? error.message : 'Unknown error'}`,
                details: { output_path: outputPath, target_id: input.target_id },
                module: 'export',
                correlationId,
                recoverable: false,
                exitCode: EXPORT_EXIT_CODES.IO_ERROR,
            });
        }
    }
    return {
        export_type: 'erp',
        target_id: input.target_id,
        artifacts,
        output_path: outputPath,
        written,
        timing: Date.now() - startTime,
    };
}
//# sourceMappingURL=export.js.map