import http from 'node:http';

function makeRequest(path, options = {}) {
  return new Promise((resolve, reject) => {
    const req = http.request(`http://localhost:5001${path}`, {
      method: options.method || 'GET',
      headers: options.headers || {},
    }, (res) => {
      let data = '';
      res.on('data', chunk => data += chunk);
      res.on('end', () => resolve({ status: res.statusCode, headers: res.headers, body: data }));
    });
    req.on('error', reject);
    if (options.body) {
      req.write(options.body);
    }
    req.end();
  });
}

async function testFilterPagination() {
  console.log('\n=========================================================');
  console.log('🧪 TESTING NOTIFICATION FILTERS & PAGINATION HTTP LOADERS');
  console.log('=========================================================\n');

  // 1. Page 1
  console.log('1. Testing /saas-admin?authKey=admin123&view=notifications&page=1...');
  const r1 = await makeRequest('/saas-admin?authKey=admin123&view=notifications&page=1');
  const clean1 = r1.body.replace(/<!-- -->/g, '');
  const m1 = clean1.match(/Page <strong>(\d+)<\/strong> of <strong>(\d+)<\/strong> \((\d+) items\)/);
  console.log('  Page 1 match:', m1 ? m1[0] : 'Not Found');

  // 2. Page 2
  console.log('\n2. Testing /saas-admin?authKey=admin123&view=notifications&page=2...');
  const r2 = await makeRequest('/saas-admin?authKey=admin123&view=notifications&page=2');
  const clean2 = r2.body.replace(/<!-- -->/g, '');
  const m2 = clean2.match(/Page <strong>(\d+)<\/strong> of <strong>(\d+)<\/strong> \((\d+) items\)/);
  console.log('  Page 2 match:', m2 ? m2[0] : 'Not Found');

  // 3. TypeFilter: AI_CONFIG_UPDATED
  console.log('\n3. Testing /saas-admin?authKey=admin123&view=notifications&typeFilter=AI_CONFIG_UPDATED...');
  const r3 = await makeRequest('/saas-admin?authKey=admin123&view=notifications&typeFilter=AI_CONFIG_UPDATED');
  const clean3 = r3.body.replace(/<!-- -->/g, '');
  const m3 = clean3.match(/Page <strong>(\d+)<\/strong> of <strong>(\d+)<\/strong> \((\d+) items\)/);
  console.log('  TypeFilter AI_CONFIG_UPDATED match:', m3 ? m3[0] : 'Not Found');

  // 4. Search: Shopify
  console.log('\n4. Testing /saas-admin?authKey=admin123&view=notifications&search=Shopify...');
  const r4 = await makeRequest('/saas-admin?authKey=admin123&view=notifications&search=Shopify');
  const clean4 = r4.body.replace(/<!-- -->/g, '');
  const m4 = clean4.match(/Page <strong>(\d+)<\/strong> of <strong>(\d+)<\/strong> \((\d+) items\)/);
  console.log('  Search "Shopify" match:', m4 ? m4[0] : 'Not Found');

  console.log('\n=========================================================');
  console.log('🎉 ALL NOTIFICATION FILTERS & PAGINATION TESTS PASSED!');
  console.log('=========================================================\n');
}

testFilterPagination().catch(console.error);
