/**
 * Script to find deprecated imports in the codebase
 * Run with: node scripts/find-deprecated-imports.js
 */

const fs = require('fs');
const path = require('path');
const { execSync } = require('child_process');

// Deprecated patterns to search for
const DEPRECATED_PATTERNS = [
  "from '@/lib/client-utils'",
  "from '@/lib/client-service'",
  "from '@/lib/data-service'",
  "from '@/lib/server-service'",
  "from '@/lib/productData'",
  "from '@/lib/shippingHelper'",
  "from '@/lib/storeHelper'",
  "from '@/lib/stats-service'",
];

function findDeprecatedImports() {
  console.log('Searching for deprecated imports...\n');
  
  DEPRECATED_PATTERNS.forEach(pattern => {
    try {
      const result = execSync(`grep -r "${pattern}" --include="*.ts" --include="*.tsx" --include="*.js" --include="*.jsx" .`, { encoding: 'utf8' });
      
      if (result) {
        console.log(`\n== Found deprecated imports: ${pattern} ==`);
        console.log(result);
        console.log(`\n== Suggested replacement: ==`);
        suggestReplacement(pattern);
      }
    } catch (error) {
      if (error.status !== 1) {
        console.error(`Error searching for ${pattern}:`, error.message);
      }
    }
  });
}

function suggestReplacement(pattern) {
  const match = pattern.match(/from '([^']+)'/);
  if (!match) return;
  
  const oldPath = match[1];
  let replacement = '';
  
  switch (oldPath) {
    case '@/lib/client-utils':
    case '@/lib/client-service':
    case '@/lib/data-service':
      replacement = "import { clientFetch, fetchClient } from '@/lib/api';";
      break;
    case '@/lib/server-service':
      replacement = "import { fetchServer } from '@/lib/api'; // Only use in Server Components";
      break;
    case '@/lib/productData':
      replacement = "import { getProducts, createProduct, updateProduct, deleteProduct } from '@/lib/api';";
      break;
    case '@/lib/shippingHelper':
      replacement = "import { getShippingOptions, createShippingOption, updateShippingOption, deleteShippingOption } from '@/lib/api';";
      break;
    case '@/lib/storeHelper':
      replacement = "import { clientFetch } from '@/lib/api';";
      break;
    case '@/lib/stats-service':
      replacement = "import { getPlatformStats } from '@/lib/api';";
      break;
    default:
      replacement = "import from '@/lib/api';";
  }
  
  console.log(replacement);
  console.log("\nReplace the specific imports with the corresponding ones from @/lib/api");
}

// Run the search
findDeprecatedImports(); 