import { S3Client, PutObjectCommand } from '@aws-sdk/client-s3';

const client = new S3Client({
  region: 'auto',
  endpoint: `https://${process.env.CLOUDFLARE_ACCOUNT_ID}.r2.cloudflarestorage.com`,
  credentials: {
    accessKeyId: process.env.CLOUDFLARE_R2_ACCESS_KEY_ID!,
    secretAccessKey: process.env.CLOUDFLARE_R2_SECRET_ACCESS_KEY!
  }
});

async function testUpload() {
  const testContent = Buffer.from('Test file created at ' + new Date().toISOString());
  const path = 'neoad/clients/annonceurs/mathis-worldwide-12345678901235/ads/test-file.txt';
  
  console.log('Uploading test file to:', path);
  
  try {
    await client.send(new PutObjectCommand({
      Bucket: process.env.CLOUDFLARE_R2_BUCKET_NAME!,
      Key: path,
      Body: testContent,
      ContentType: 'text/plain'
    }));
    
    const publicUrl = `${process.env.CLOUDFLARE_R2_PUBLIC_URL}/${path}`;
    console.log('Upload successful!');
    console.log('Public URL:', publicUrl);
  } catch (error) {
    console.error('Upload failed:', error);
  }
}

testUpload();
