const fetch = require('node-fetch');

const SERVER_URL = 'http://localhost:7896';

async function testGitDB() {
  console.log('🧪 Testing GitDB Server...\n');

  try {
    // Test 1: Health Check
    console.log('1. Testing Health Check...');
    const health = await fetch(`${SERVER_URL}/health`);
    const healthData = await health.json();
    console.log('✅ Health Check:', healthData);
    console.log('');

    // Test 2: Create Collection
    console.log('2. Creating Collection...');
    const createCollection = await fetch(`${SERVER_URL}/collections`, {
      method: 'POST',
      headers: { 'Content-Type': 'application/json' },
      body: JSON.stringify({ name: 'users' })
    });
    const collectionData = await createCollection.json();
    console.log('✅ Collection Created:', collectionData);
    console.log('');

    // Test 3: Insert Document
    console.log('3. Inserting Document...');
    const insertDoc = await fetch(`${SERVER_URL}/insert`, {
      method: 'POST',
      headers: { 'Content-Type': 'application/json' },
      body: JSON.stringify({
        collection: 'users',
        document: {
          name: 'John Doe',
          email: 'john@example.com',
          age: 30,
          city: 'New York'
        }
      })
    });
    const insertData = await insertDoc.json();
    console.log('✅ Document Inserted:', insertData);
    const docId = insertData.id;
    console.log('');

    // Test 4: Find Document by ID
    console.log('4. Finding Document by ID...');
    const findDoc = await fetch(`${SERVER_URL}/find/${docId}?collection=users`);
    const findData = await findDoc.json();
    console.log('✅ Document Found:', findData);
    console.log('');

    // Test 5: Find Document by Query
    console.log('5. Finding Document by Query...');
    const findOneDoc = await fetch(`${SERVER_URL}/findone`, {
      method: 'POST',
      headers: { 'Content-Type': 'application/json' },
      body: JSON.stringify({
        collection: 'users',
        query: { name: 'John Doe' }
      })
    });
    const findOneData = await findOneDoc.json();
    console.log('✅ Document Found by Query:', findOneData);
    console.log('');

    // Test 6: Update Document
    console.log('6. Updating Document...');
    const updateDoc = await fetch(`${SERVER_URL}/update/${docId}`, {
      method: 'PUT',
      headers: { 'Content-Type': 'application/json' },
      body: JSON.stringify({
        collection: 'users',
        updates: { age: 31, city: 'Los Angeles' }
      })
    });
    const updateData = await updateDoc.json();
    console.log('✅ Document Updated:', updateData);
    console.log('');

    // Test 7: Count Documents
    console.log('7. Counting Documents...');
    const countDocs = await fetch(`${SERVER_URL}/count`, {
      method: 'POST',
      headers: { 'Content-Type': 'application/json' },
      body: JSON.stringify({ collection: 'users' })
    });
    const countData = await countDocs.json();
    console.log('✅ Document Count:', countData);
    console.log('');

    // Test 8: Show All Documents
    console.log('8. Showing All Documents...');
    const showDocs = await fetch(`${SERVER_URL}/show-docs`, {
      method: 'POST',
      headers: { 'Content-Type': 'application/json' },
      body: JSON.stringify({ collection: 'users' })
    });
    const showData = await showDocs.json();
    console.log('✅ All Documents:', showData);
    console.log('');

    // Test 9: Get Distinct Values
    console.log('9. Getting Distinct Cities...');
    const distinctCities = await fetch(`${SERVER_URL}/distinct`, {
      method: 'POST',
      headers: { 'Content-Type': 'application/json' },
      body: JSON.stringify({
        collection: 'users',
        field: 'city'
      })
    });
    const distinctData = await distinctCities.json();
    console.log('✅ Distinct Cities:', distinctData);
    console.log('');

    // Test 10: List Collections
    console.log('10. Listing Collections...');
    const listCollections = await fetch(`${SERVER_URL}/collections`);
    const collectionsData = await listCollections.json();
    console.log('✅ Collections:', collectionsData);
    console.log('');

    console.log('🎉 All tests completed successfully!');
    console.log('GitDB is working perfectly! 🚀');

  } catch (error) {
    console.error('❌ Test failed:', error.message);
  }
}

// Run the test
testGitDB(); 