import { readFileSync, writeFileSync, readdirSync, statSync } from 'fs';
import { join, relative } from 'path';

const targetFiles = [
  'lib/admin-auth.ts',
  'app/api/v1/ringtones/route.ts',
  'app/api/v1/ringtones/[id]/route.ts',
  'app/api/v1/ringtones/[id]/download/route.ts',
  'app/api/playlists/[id]/route.ts',
  'app/api/v1/ringtones/[id]/confirm/route.ts',
  'app/api/playlists/[id]/tracks/route.ts',
  'app/api/playlists/[id]/tracks/[trackId]/route.ts',
  'app/api/notifications/route.ts',
  'app/api/history/route.ts',
  'app/api/genres/[id]/route.ts',
  'app/api/artists/[id]/follow/route.ts',
  'app/api/artist/verification/route.ts',
  'app/api/artist/stats/route.ts',
  'app/api/artist/releases/route.ts',
  'app/api/tracks/[id]/like/route.ts',
  'app/api/admin/verification/route.ts',
  'app/api/artist/promotions/route.ts',
  'app/api/artist/earnings/route.ts',
  'app/api/support/route.ts',
  'app/api/artist/campaigns/route.ts',
  'app/api/support/[id]/route.ts',
  'app/api/admin/tracks/route.ts',
  'app/api/artist/audience/route.ts',
  'app/api/artist/analytics/route.ts',
  'app/api/admin/tracks/recalculate-durations/route.ts',
  'app/api/admin/tracks/bulk/route.ts',
  'app/api/promotions/route.ts',
  'app/api/admin/moderation/route.ts',
  'app/api/admin/support/route.ts',
  'app/api/admin/ringtone-api/route.ts',
  'app/api/admin/logs/route.ts',
  'app/api/admin/ringtone-api/stats/route.ts',
  'app/api/admin/support/[id]/route.ts',
  'app/api/admin/exports/pdf/route.ts',
  'app/api/admin/subscriptions/route.ts',
  'app/api/admin/ringtone-api/downloads/route.ts',
  'app/api/admin/categories/route.ts',
  'app/api/admin/ringtone-api/catalog/route.ts',
  'app/api/admin/categories/[id]/route.ts',
  'app/api/admin/ringtone-api/[id]/route.ts',
  'app/api/admin/backups/route.ts',
  'app/api/admin/revenue/route.ts',
  'app/api/admin/backups/[id]/route.ts',
  'app/api/admin/reports/route.ts',
  'app/api/admin/backups/[id]/restore/route.ts',
  'app/api/admin/settings/route.ts',
  'app/api/admin/reports/[id]/route.ts',
  'app/api/admin/api-keys/route.ts',
  'app/api/admin/promotions/route.ts',
  'app/api/admin/analytics/route.ts',
  'app/api/admin/playlists/route.ts',
  'app/api/admin/royalties/route.ts',
  'app/api/admin/notifications/route.ts',
];

let fixedCount = 0;

for (const filePath of targetFiles) {
  try {
    let content = readFileSync(filePath, 'utf-8');
    
    // Check if file has the pattern we need to fix
    if (content.includes('const sql = neon(process.env.DATABASE_URL!)')) {
      // Replace the neon import and sql creation with lib/db import
      content = content.replace(
        /import { neon } from "@neondatabase\/serverless"\n\nconst sql = neon\(process\.env\.DATABASE_URL!\)/,
        'import { sql } from "@/lib/db"'
      );
      
      // Also handle case where there might be more imports
      content = content.replace(
        /import { neon } from "@neondatabase\/serverless"\n+const sql = neon\(process\.env\.DATABASE_URL!\)/,
        'import { sql } from "@/lib/db"'
      );
      
      writeFileSync(filePath, content);
      console.log(`Fixed: ${filePath}`);
      fixedCount++;
    }
  } catch (error) {
    console.error(`Error processing ${filePath}:`, error.message);
  }
}

console.log(`\nTotal files fixed: ${fixedCount}`);
