#!/usr/bin/env bun
/**
 * Test script to verify Dokploy API endpoint formats
 * Tests the three core operations needed for deployment
 */

import { createDokployClient } from './api/dokploy.js';

const TEST_PROJECT_NAME = `test-api-${Date.now()}`;
const TEST_APP_NAME = `test-app-${Date.now()}`;

console.log('═══════════════════════════════════════');
console.log('  Dokploy API Format Tests');
console.log('═══════════════════════════════════════\n');

try {
  const client = createDokployClient();

  // Test 1: project.create
  console.log('📋 Test 1: project.create');
  console.log('Request: { name, description }');
  const project = await client.createProject(
    TEST_PROJECT_NAME,
    'API format test project'
  );
  console.log('Response format:');
  console.log(JSON.stringify(project, null, 2));
  console.log(`✅ Project created: ${project.projectId}\n`);

  // Test 2: application.create
  console.log('📋 Test 2: application.create');
  console.log('Request: { name, appName, projectId, dockerImage, sourceType }');
  const application = await client.createApplication(
    TEST_APP_NAME,
    project.projectId,
    'nginx:alpine'
  );
  console.log('Response format:');
  console.log(JSON.stringify(application, null, 2));
  console.log(`✅ Application created: ${application.applicationId}\n`);

  // Test 3: application.deploy
  console.log('📋 Test 3: application.deploy');
  console.log('Request: { applicationId }');
  await client.deployApplication(application.applicationId);
  console.log('Response: void (no return value)');
  console.log(`✅ Deployment triggered\n`);

  // Cleanup
  console.log('🧹 Cleaning up test resources...');
  await client.deleteApplication(application.applicationId);
  console.log(`✅ Deleted application: ${application.applicationId}`);
  await client.deleteProject(project.projectId);
  console.log(`✅ Deleted project: ${project.projectId}`);

  console.log('\n═══════════════════════════════════════');
  console.log('  All API Format Tests Passed!');
  console.log('═══════════════════════════════════════');

} catch (error) {
  console.error('\n❌ Test failed:');
  console.error(error);
  if (error instanceof Error) {
    console.error('Error message:', error.message);
    console.error('Error stack:', error.stack);
  }
  process.exit(1);
}
