/**
 * Affiliate Marketing Assets System
 * Downloadable banners, email templates, and promotional materials
 */

export interface MarketingAsset {
  id: string;
  name: string;
  description: string;
  category: 'banner' | 'email' | 'landing_page' | 'social_media' | 'video';
  format: string; // dimensions or file type
  downloadUrl: string;
  previewUrl?: string;
  createdAt: Date;
  updatedAt: Date;
}

export interface EmailTemplate {
  id: string;
  name: string;
  subject: string;
  htmlContent: string;
  textContent: string;
  previewText: string;
  variables: string[]; // e.g., {{affiliate_code}}, {{player_name}}
}

export interface BannerAsset {
  id: string;
  name: string;
  dimensions: string; // e.g., "728x90"
  format: 'png' | 'jpg' | 'gif';
  downloadUrl: string;
  previewUrl: string;
}

/**
 * Email Templates
 */
export const EMAIL_TEMPLATES: EmailTemplate[] = [
  {
    id: 'referral_invite',
    name: 'Referral Invitation',
    subject: 'Join CoinKrazy and Get 10 Free SC! 🎰',
    htmlContent: `
<!DOCTYPE html>
<html>
<head>
  <meta charset="UTF-8">
  <style>
    body { font-family: Arial, sans-serif; background-color: #f5f5f5; }
    .container { max-width: 600px; margin: 0 auto; background-color: white; padding: 20px; border-radius: 8px; }
    .header { text-align: center; padding: 20px 0; background: linear-gradient(135deg, #667eea 0%, #764ba2 100%); color: white; border-radius: 8px; }
    .cta-button { display: inline-block; background-color: #667eea; color: white; padding: 12px 30px; text-decoration: none; border-radius: 4px; margin: 20px 0; }
    .content { padding: 20px 0; }
  </style>
</head>
<body>
  <div class="container">
    <div class="header">
      <h1>Join CoinKrazy Today! 🎰</h1>
      <p>Get 10 Free SC on Your First Deposit</p>
    </div>
    
    <div class="content">
      <p>Hi {{player_name}},</p>
      
      <p>I've been having a blast playing CoinKrazy - the ultimate sweepstakes social casino! 🎉</p>
      
      <p>I wanted to share this amazing opportunity with you. Join now and get:</p>
      <ul>
        <li>✓ 10 Free SC Welcome Bonus</li>
        <li>✓ 367 Exciting Games</li>
        <li>✓ Daily Challenges & Tournaments</li>
        <li>✓ Huge Winning Potential</li>
      </ul>
      
      <center>
        <a href="https://coinkrazy.com/join?ref={{affiliate_code}}" class="cta-button">Claim Your 10 Free SC</a>
      </center>
      
      <p>Use my referral code: <strong>{{affiliate_code}}</strong></p>
      
      <p>See you on the casino floor!</p>
      <p>{{affiliate_name}}</p>
    </div>
  </div>
</body>
</html>
    `,
    textContent: `
Join CoinKrazy Today!

Hi {{player_name}},

I've been having a blast playing CoinKrazy - the ultimate sweepstakes social casino!

I wanted to share this amazing opportunity with you. Join now and get:
- 10 Free SC Welcome Bonus
- 367 Exciting Games
- Daily Challenges & Tournaments
- Huge Winning Potential

Sign up here: https://coinkrazy.com/join?ref={{affiliate_code}}

Use my referral code: {{affiliate_code}}

See you on the casino floor!
{{affiliate_name}}
    `,
    previewText: 'Get 10 Free SC on Your First Deposit',
    variables: ['player_name', 'affiliate_code', 'affiliate_name'],
  },
  {
    id: 'bonus_announcement',
    name: 'Bonus Announcement',
    subject: 'Exclusive Bonus Alert: 50% Match Bonus This Week! 💰',
    htmlContent: `
<!DOCTYPE html>
<html>
<head>
  <meta charset="UTF-8">
  <style>
    body { font-family: Arial, sans-serif; background-color: #f5f5f5; }
    .container { max-width: 600px; margin: 0 auto; background-color: white; padding: 20px; border-radius: 8px; }
    .highlight { background-color: #fff3cd; border-left: 4px solid #ffc107; padding: 15px; margin: 20px 0; }
    .cta-button { display: inline-block; background-color: #28a745; color: white; padding: 12px 30px; text-decoration: none; border-radius: 4px; margin: 20px 0; }
  </style>
</head>
<body>
  <div class="container">
    <h2>Exclusive Bonus Alert! 💰</h2>
    
    <div class="highlight">
      <h3>50% Match Bonus This Week Only!</h3>
      <p>Deposit {{bonus_amount}} and get {{bonus_amount}} FREE!</p>
    </div>
    
    <p>Hi {{player_name}},</p>
    
    <p>This week only, CoinKrazy is offering an exclusive 50% match bonus for all players!</p>
    
    <p><strong>How it works:</strong></p>
    <ul>
      <li>Deposit any amount</li>
      <li>Get 50% bonus instantly</li>
      <li>Play with double the funds</li>
      <li>Win big! 🎰</li>
    </ul>
    
    <center>
      <a href="https://coinkrazy.com/deposit?ref={{affiliate_code}}" class="cta-button">Claim Your Bonus Now</a>
    </center>
    
    <p>This offer is limited to this week, so don't miss out!</p>
  </div>
</body>
</html>
    `,
    textContent: `
Exclusive Bonus Alert!

50% Match Bonus This Week Only!
Deposit {{bonus_amount}} and get {{bonus_amount}} FREE!

Hi {{player_name}},

This week only, CoinKrazy is offering an exclusive 50% match bonus for all players!

How it works:
- Deposit any amount
- Get 50% bonus instantly
- Play with double the funds
- Win big!

Claim your bonus: https://coinkrazy.com/deposit?ref={{affiliate_code}}

This offer is limited to this week, so don't miss out!
    `,
    previewText: '50% Match Bonus This Week Only!',
    variables: ['player_name', 'bonus_amount', 'affiliate_code'],
  },
];

/**
 * Banner Assets
 */
export const BANNER_ASSETS: BannerAsset[] = [
  {
    id: 'banner_728x90',
    name: 'Leaderboard Banner',
    dimensions: '728x90',
    format: 'png',
    downloadUrl: 'https://assets.coinkrazy.com/banners/728x90.png',
    previewUrl: 'https://assets.coinkrazy.com/banners/728x90-preview.jpg',
  },
  {
    id: 'banner_300x250',
    name: 'Medium Rectangle',
    dimensions: '300x250',
    format: 'png',
    downloadUrl: 'https://assets.coinkrazy.com/banners/300x250.png',
    previewUrl: 'https://assets.coinkrazy.com/banners/300x250-preview.jpg',
  },
  {
    id: 'banner_160x600',
    name: 'Wide Skyscraper',
    dimensions: '160x600',
    format: 'png',
    downloadUrl: 'https://assets.coinkrazy.com/banners/160x600.png',
    previewUrl: 'https://assets.coinkrazy.com/banners/160x600-preview.jpg',
  },
  {
    id: 'banner_1200x628',
    name: 'Social Media Banner',
    dimensions: '1200x628',
    format: 'png',
    downloadUrl: 'https://assets.coinkrazy.com/banners/1200x628.png',
    previewUrl: 'https://assets.coinkrazy.com/banners/1200x628-preview.jpg',
  },
];

/**
 * Social Media Content Templates
 */
export const SOCIAL_MEDIA_TEMPLATES = {
  instagram: [
    {
      id: 'instagram_post_1',
      caption: 'Just hit the jackpot on CoinKrazy! 🎰💰 Join me and get 10 free SC with code {{affiliate_code}} #CoinKrazy #SocialCasino #WinBig',
      hashtags: ['#CoinKrazy', '#SocialCasino', '#Gaming', '#WinBig', '#Jackpot'],
    },
    {
      id: 'instagram_post_2',
      caption: 'CoinKrazy has 367 games and I\'m obsessed! 🎮✨ Try it free with my code {{affiliate_code}} #CoinKrazy #GamingLife',
      hashtags: ['#CoinKrazy', '#GamingLife', '#SocialCasino', '#Gaming'],
    },
  ],
  twitter: [
    {
      id: 'twitter_post_1',
      content: 'Just won big on CoinKrazy! 🎰 Join me and get 10 free SC. Use code {{affiliate_code}} #CoinKrazy #SocialCasino',
    },
    {
      id: 'twitter_post_2',
      content: '367 games, daily tournaments, huge prizes 💰 CoinKrazy is the ultimate social casino! Join with code {{affiliate_code}} #Gaming',
    },
  ],
  facebook: [
    {
      id: 'facebook_post_1',
      content: 'Have you tried CoinKrazy yet? It\'s the most fun I\'ve had gaming! 🎰 Get 10 free SC when you join with my code {{affiliate_code}}',
    },
  ],
  tiktok: [
    {
      id: 'tiktok_video_1',
      description: 'Quick TikTok video showing exciting gameplay moments from CoinKrazy',
      tips: [
        'Show exciting wins',
        'Use trending sounds',
        'Keep it under 60 seconds',
        'Include your referral code in text overlay',
      ],
    },
  ],
};

/**
 * Get email template by ID
 */
export function getEmailTemplate(id: string): EmailTemplate | undefined {
  return EMAIL_TEMPLATES.find(template => template.id === id);
}

/**
 * Get all email templates
 */
export function getAllEmailTemplates(): EmailTemplate[] {
  return EMAIL_TEMPLATES;
}

/**
 * Get banner asset by ID
 */
export function getBannerAsset(id: string): BannerAsset | undefined {
  return BANNER_ASSETS.find(banner => banner.id === id);
}

/**
 * Get all banner assets
 */
export function getAllBannerAssets(): BannerAsset[] {
  return BANNER_ASSETS;
}

/**
 * Render email template with variables
 */
export function renderEmailTemplate(template: EmailTemplate, variables: Record<string, string>): {
  subject: string;
  htmlContent: string;
  textContent: string;
} {
  let subject = template.subject;
  let htmlContent = template.htmlContent;
  let textContent = template.textContent;

  Object.entries(variables).forEach(([key, value]) => {
    const placeholder = `{{${key}}}`;
    subject = subject.replace(new RegExp(placeholder, 'g'), value);
    htmlContent = htmlContent.replace(new RegExp(placeholder, 'g'), value);
    textContent = textContent.replace(new RegExp(placeholder, 'g'), value);
  });

  return { subject, htmlContent, textContent };
}

/**
 * Get marketing assets by category
 */
export function getAssetsByCategory(category: 'banner' | 'email' | 'social_media'): MarketingAsset[] {
  // This would fetch from database in production
  if (category === 'banner') {
    return BANNER_ASSETS.map(banner => ({
      id: banner.id,
      name: banner.name,
      description: `${banner.dimensions} banner for web promotions`,
      category: 'banner',
      format: banner.dimensions,
      downloadUrl: banner.downloadUrl,
      previewUrl: banner.previewUrl,
      createdAt: new Date(),
      updatedAt: new Date(),
    }));
  }

  if (category === 'email') {
    return EMAIL_TEMPLATES.map(template => ({
      id: template.id,
      name: template.name,
      description: template.subject,
      category: 'email',
      format: 'html',
      downloadUrl: `https://coinkrazy.com/email-templates/${template.id}`,
      createdAt: new Date(),
      updatedAt: new Date(),
    }));
  }

  return [];
}
