#!/usr/bin/env node
/**
 * Priority 1 Influencer Outreach Email Sender
 * Issue: FRE-667
 * Send Date: April 26, 2026
 * 
 * Contacts (5 total):
 * 1. John Finn - johnfinn@business.youtube.com
 * 2. No Film School - tips@nofilmschool.com
 * 3. Script Lab - info@scriptlab.com
 * 4. ScreenCraft - info@screencraft.org
 * 5. Go Into The Story - scott@thestorydepartment.com
 */

import { Resend } from 'resend';

// Initialize Resend (free tier: 100 emails/day, 3000/month)
const resend = new Resend(process.env.RESEND_API_KEY || 're_test_key');

const emails = [
  {
    to: 'johnfinn@business.youtube.com',
    subject: 'Free lifetime Pro account - modern screenwriting tool for your channel',
    name: 'John Finn',
    template: 'john-finn'
  },
  {
    to: 'tips@nofilmschool.com',
    subject: 'Beta access: Modern screenwriting platform for NFTS community',
    name: 'No Film School',
    template: 'no-film-school'
  },
  {
    to: 'info@scriptlab.com',
    subject: 'Collaboration: Beta access + potential partnership',
    name: 'Script Lab',
    template: 'script-lab'
  },
  {
    to: 'info@screencraft.org',
    subject: 'Beta partnership: Modern screenwriting tool for ScreenCraft community',
    name: 'ScreenCraft',
    template: 'screencraft'
  },
  {
    to: 'scott@thestorydepartment.com',
    subject: 'WGA blog + modern screenwriting tools - partnership opportunity?',
    name: 'Scott Myers',
    template: 'go-into-the-story'
  }
];

// Email templates from /marketing/beta-outreach-priority-1.md
const templates = {
  'john-finn': `
<p>Hi John,</p>

<p>I've been following your channel for years - your Final Draft tutorials are legendary in the screenwriting community. The way you break down screenplay format is exactly what new writers need.</p>

<p>I'm reaching out from <strong>Scripter</strong>, a new screenwriting platform launching soon. We're building a modern alternative to Final Draft with:</p>

<ul>
<li><strong>Real-time collaboration</strong> (like Google Docs for screenplays)</li>
<li><strong>AI writing assistant</strong> (optional, writer-controlled)</li>
<li><strong>Cloud-native</strong> (works on any device, no install needed)</li>
<li><strong>Affordable pricing</strong> (Pro at $9.99/month vs Final Draft's $200 one-time)</li>
</ul>

<p><strong>The Ask:</strong><br>
I'd love to give you <strong>free lifetime Pro access</strong> in exchange for:</p>
<ol>
<li>Honest feedback on bugs, UX, features</li>
<li>Optional: A video review if you genuinely like it (no pressure!)</li>
</ol>

<p>We're limiting our beta to 500 writers, and I think your audience would love to see a modern alternative covered on your channel.</p>

<p><strong>Next Steps:</strong><br>
Interested in a quick 15-min demo? Here's my Calendly: <a href="https://calendly.com/scripter-cmo">Calendly Link</a></p>

<p>Or just reply to this email and I'll get you set up with beta access immediately.</p>

<p>Thanks for all the amazing content you create for the screenwriting community!</p>

<p>Best,<br>
CMO, Scripter</p>

<p><strong>P.S.</strong> Happy to provide an exclusive discount code for your viewers if/when we launch!</p>
  `,
  'no-film-school': `
<p>Hi NFTS Team,</p>

<p>Love what you're doing with No Film School - it's the go-to resource for indie filmmakers and screenwriters.</p>

<p>I'm reaching out from <strong>Scripter</strong>, a new screenwriting platform built for how writers actually work in 2026:</p>

<p><strong>Key Features:</strong></p>
<ul>
<li>Real-time collaboration (multiple writers in the same script)</li>
<li>AI-assisted outlining and dialogue suggestions</li>
<li>Cloud-native, works on any device</li>
<li>Free tier + Pro at $9.99/month (vs Final Draft's $200)</li>
</ul>

<p><strong>The Opportunity:</strong><br>
We're launching our beta program (500 users max) and would love to have the NFTS community represented. We can offer:</p>

<ol>
<li><strong>Free lifetime Pro accounts</strong> for your team</li>
<li><strong>Exclusive discount code</strong> for your readers/viewers</li>
<li><strong>Guest post opportunity</strong>: "How AI and collaboration tools are changing screenwriting" (no pitch, pure value)</li>
</ol>

<p>We're not asking for coverage - just honest feedback from people who actually know filmmaking.</p>

<p>Interested in early access?</p>

<p>Best,<br>
CMO, Scripter</p>

<p><strong>P.S.</strong> We're launching on Product Hunt May 7 - happy to coordinate if you're interested in featuring us!</p>
  `,
  'script-lab': `
<p>Hi Script Lab Team,</p>

<p>I've been following Script Lab for years - your screenplay analysis videos and software reviews are incredibly valuable to the screenwriting community.</p>

<p>I'm reaching out from <strong>Scripter</strong>, a new screenwriting platform launching soon. Given that you've reviewed Final Draft, WriterDuet, and other tools, I thought you might be interested in what we're building.</p>

<p><strong>What Makes Scripter Different:</strong></p>
<ul>
<li><strong>Real-time collaboration</strong> (Final Draft wishes it had this)</li>
<li><strong>AI writing assistant</strong> (writer-controlled, optional)</li>
<li><strong>Cloud-native</strong> (no install, works anywhere)</li>
<li><strong>Modern pricing</strong> (Free tier + $9.99/month Pro)</li>
</ul>

<p><strong>Partnership Opportunity:</strong><br>
We're launching our beta program and would love to partner with Script Lab:</p>

<ol>
<li><strong>Free lifetime Pro access</strong> for your team</li>
<li><strong>Exclusive early review opportunity</strong> (embargoed access if you want)</li>
<li><strong>Affiliate program</strong> (we can discuss revenue share)</li>
<li><strong>Guest content exchange</strong> (we'll write for your blog, you guest post on ours)</li>
</ol>

<p>We're limiting beta to 500 users, and I'd love to have Script Lab as one of our founding partners.</p>

<p>Interested in chatting?</p>

<p>Best,<br>
CMO, Scripter</p>
  `,
  'screencraft': `
<p>Hi ScreenCraft Team,</p>

<p>Huge fan of what you're doing with ScreenCraft - the competitions, resources, and blog are incredibly valuable for working screenwriters.</p>

<p>I'm reaching out from <strong>Scripter</strong>, a new screenwriting platform launching in May 2026. We're building a modern alternative to Final Draft with real-time collaboration and AI assistance.</p>

<p><strong>Why I'm Reaching Out:</strong><br>
Your community is exactly who we're building for - serious writers who want professional tools without the $200 price tag.</p>

<p><strong>Partnership Ideas:</strong></p>
<ol>
<li><strong>Beta access for ScreenCraft community</strong> - Free Pro accounts for competition winners/finalists</li>
<li><strong>Educational discount</strong> - Special pricing for your readers</li>
<li><strong>Co-hosted webinar</strong> - "The Future of Screenwriting Tools" (no pitch, pure education)</li>
<li><strong>Sponsored content</strong> - We'll write educational posts for your blog</li>
</ol>

<p><strong>What We're Asking:</strong></p>
<ul>
<li>Honest feedback from your team on our beta</li>
<li>Willingness to explore partnership opportunities</li>
<li>Optional: Mention in your newsletter if you think it's valuable for your readers</li>
</ul>

<p>We're not asking for free coverage - we want to provide genuine value to your community.</p>

<p>Interested in exploring this?</p>

<p>Best,<br>
CMO, Scripter</p>
  `,
  'go-into-the-story': `
<p>Hi Scott,</p>

<p>I've been reading Go Into The Story since the beginning - it's the gold standard for screenwriting education. Your posts on story structure have taught me more than any book.</p>

<p>I'm reaching out from <strong>Scripter</strong>, a new screenwriting platform launching soon. Given that you write about the craft (not just tools), I wanted to get your perspective on what we're building.</p>

<p><strong>The Vision:</strong><br>
We believe screenwriting tools should:</p>
<ol>
<li><strong>Get out of the way</strong> and let you write</li>
<li><strong>Enable collaboration</strong> (writing is often a team sport)</li>
<li><strong>Use AI thoughtfully</strong> (assist, don't replace)</li>
<li><strong>Be accessible</strong> (free tier, affordable Pro)</li>
</ol>

<p><strong>The Ask:</strong><br>
I'd love to offer you <strong>free lifetime Pro access</strong> for your own writing, no strings attached. If you find it valuable and want to mention it to your readers, that's great - but no pressure at all.</p>

<p>We're also happy to:</p>
<ul>
<li>Write a guest post on "How Technology is Changing Screenwriting"</li>
<li>Sponsor a Screenwriting Soirée or event</li>
<li>Provide beta access for WGA members</li>
</ul>

<p>Would you be open to a quick call to discuss?</p>

<p>Best,<br>
CMO, Scripter</p>

<p><strong>P.S.</strong> I know you get pitched constantly - this isn't a pitch for coverage. Just offering a tool that might help your writing.</p>
  `
};

async function sendEmails() {
  console.log('🚀 Starting Priority 1 influencer outreach...\n');
  
  const results = [];
  
  for (const email of emails) {
    try {
      console.log(`📧 Sending to ${email.name} (${email.to})...`);
      
      const data = await resend.emails.send({
        from: 'Scripter CMO <cmo@scripter.app>',
        to: [email.to],
        subject: email.subject,
        html: `
<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <style>
    body { font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif; line-height: 1.6; color: #1a336b; max-width: 600px; margin: 0 auto; padding: 20px; }
    a { color: #518ac8; }
    ul, ol { padding-left: 20px; }
  </style>
</head>
<body>
  ${templates[email.template]}
</body>
</html>
        `,
        headers: {
          'X-Priority': '1',
          'X-Campaign': 'FRE-667-Priority1-Outreach'
        }
      });
      
      results.push({
        contact: email.name,
        email: email.to,
        status: 'sent',
        id: data.id,
        timestamp: new Date().toISOString()
      });
      
      console.log(`✅ Sent successfully (ID: ${data.id})\n`);
      
      // Rate limiting: wait 2 seconds between emails
      await new Promise(resolve => setTimeout(resolve, 2000));
      
    } catch (error) {
      console.error(`❌ Failed to send to ${email.name}:`, error.message);
      results.push({
        contact: email.name,
        email: email.to,
        status: 'failed',
        error: error.message,
        timestamp: new Date().toISOString()
      });
    }
  }
  
  // Print summary
  console.log('\n' + '='.repeat(60));
  console.log('📊 SEND SUMMARY');
  console.log('='.repeat(60));
  
  const sent = results.filter(r => r.status === 'sent').length;
  const failed = results.filter(r => r.status === 'failed').length;
  
  console.log(`\n✅ Sent: ${sent}/${emails.length}`);
  console.log(`❌ Failed: ${failed}/${emails.length}`);
  console.log('\nDetailed Results:');
  
  results.forEach(r => {
    console.log(`  ${r.status === 'sent' ? '✅' : '❌'} ${r.contact} (${r.email})`);
    if (r.id) console.log(`     ID: ${r.id}`);
    if (r.error) console.log(`     Error: ${r.error}`);
  });
  
  console.log('\n' + '='.repeat(60));
  console.log('📅 Follow-up Schedule:');
  console.log('  • Follow-up #1: April 29 (Day 3)');
  console.log('  • Follow-up #2: May 3 (Day 7)');
  console.log('  • Follow-up #3: May 10 (Day 14 - break up)');
  console.log('='.repeat(60));
  
  return results;
}

// Run if executed directly
if (process.argv[1]?.includes('send-priority-1-outreach.js')) {
  sendEmails().catch(console.error);
}

export { sendEmails };
