// Test script for Application API
// Run with: node test-application-api.js

const API_BASE = 'http://217.196.51.230:4000/api';

async function testApplicationAPI() {
  try {
    // First, create a job to get a jobId
    console.log('1. Creating a test job...');
    const jobResponse = await fetch(`${API_BASE}/jobs`, {
      method: 'POST',
      headers: { 'Content-Type': 'application/json' },
      body: JSON.stringify({
        title: 'Software Engineer',
        department: 'Engineering',
        location: 'Remote',
        employmentType: 'Full-time',
        description: 'Test job for application testing',
        responsibilities: ['Write code', 'Review PRs'],
        requirements: ['3+ years experience'],
        applicationUrl: 'https://example.com/apply',
        tags: 'test',
        status: 'Published'
      })
    });
    
    const jobData = await jobResponse.json();
    if (!jobData.success) {
      throw new Error('Failed to create job');
    }
    const jobId = jobData.data._id || jobData.data.id;
    console.log('✓ Job created with ID:', jobId);

    // Now create an application
    console.log('\n2. Creating an application...');
    const appResponse = await fetch(`${API_BASE}/applications`, {
      method: 'POST',
      headers: { 'Content-Type': 'application/json' },
      body: JSON.stringify({
        fullName: 'John Doe',
        email: 'john.doe@example.com',
        phone: '+1 (555) 123-4567',
        jobId: jobId,
        role: 'Software Engineer',
        location: 'Remote',
        resumeUrl: 'https://example.com/resume.pdf',
        note: 'Interested in this position',
        status: 'New'
      })
    });

    const appData = await appResponse.json();
    if (!appData.success) {
      throw new Error('Failed to create application: ' + JSON.stringify(appData));
    }
    console.log('✓ Application created:', appData.data);

    // List all applications
    console.log('\n3. Listing all applications...');
    const listResponse = await fetch(`${API_BASE}/applications`);
    const listData = await listResponse.json();
    console.log('✓ Applications:', listData.data.length);

    console.log('\n✅ All tests passed!');
  } catch (error) {
    console.error('❌ Test failed:', error.message);
  }
}

testApplicationAPI();

