#!/usr/bin/env node
import { createClient } from '@supabase/supabase-js';
import { config } from 'dotenv';

config({ path: '.env.local' });

const supabaseAdmin = createClient(
  process.env.NEXT_PUBLIC_SUPABASE_URL,
  process.env.SUPABASE_SERVICE_ROLE_KEY,
  {
    auth: { autoRefreshToken: false, persistSession: false },
  }
);

// Test downloading the newly uploaded CV
const appId = 'd515974d-4004-40f6-9791-df15c4522e7c';

console.log(`Testing download for application: ${appId}\n`);

const { data: app, error: appError } = await supabaseAdmin
  .from('job_applications')
  .select('cv_bucket, cv_path, cv_filename, applicant_name')
  .eq('id', appId)
  .single();

if (appError || !app) {
  console.error('Failed to get application:', appError);
  process.exit(1);
}

console.log('Application:', app);

const { data: cvData, error: downloadError } = await supabaseAdmin.storage
  .from(app.cv_bucket)
  .download(app.cv_path);

if (downloadError) {
  console.error('Download failed:', downloadError);
  process.exit(1);
}

console.log('\n✅ Download successful!');
console.log(`Size: ${cvData.size} bytes`);
console.log(`Type: ${cvData.type}`);
