/**
 * Affiliate Support Portal System
 * Knowledge base, FAQ, and support resources
 */

export interface KnowledgeBaseArticle {
  id: string;
  title: string;
  category: string;
  content: string;
  views: number;
  helpful: number;
  notHelpful: number;
  createdAt: Date;
  updatedAt: Date;
  tags: string[];
}

export interface FAQ {
  id: string;
  question: string;
  answer: string;
  category: string;
  views: number;
  helpful: number;
  relatedArticles: string[];
}

export interface SupportTicket {
  id: number;
  affiliateId: number;
  subject: string;
  message: string;
  category: string;
  priority: 'low' | 'medium' | 'high' | 'urgent';
  status: 'open' | 'in_progress' | 'resolved' | 'closed';
  createdAt: Date;
  resolvedAt?: Date;
  response?: string;
}

/**
 * Knowledge Base Articles
 */
export const KNOWLEDGE_BASE_ARTICLES: KnowledgeBaseArticle[] = [
  {
    id: 'getting_started',
    title: 'Getting Started as a CoinKrazy Affiliate',
    category: 'Getting Started',
    content: `
# Getting Started as a CoinKrazy Affiliate

Welcome to the CoinKrazy Affiliate Program! This guide will help you get started.

## Step 1: Create Your Account
- Sign up for a CoinKrazy account
- Complete your profile
- Verify your email address

## Step 2: Apply for the Affiliate Program
- Visit the affiliate section
- Fill out the application form
- Provide your payment details
- Wait for approval (usually 24-48 hours)

## Step 3: Get Your Affiliate Code
- Once approved, you'll receive your unique affiliate code
- Use this code in all your referral links
- Share it with your audience

## Step 4: Download Marketing Materials
- Access the marketing assets library
- Download banners, email templates, and social media content
- Customize with your branding (if allowed)

## Step 5: Start Promoting
- Share your referral links on your channels
- Track your performance in the dashboard
- Optimize based on what works best

## Tips for Success
- Be authentic in your promotions
- Focus on benefits, not just features
- Build relationships with your audience
- Test different strategies
- Monitor your analytics

Good luck! 🚀
    `,
    views: 1250,
    helpful: 980,
    notHelpful: 45,
    createdAt: new Date('2026-01-01'),
    updatedAt: new Date('2026-04-01'),
    tags: ['getting-started', 'onboarding', 'basics'],
  },
  {
    id: 'earning_money',
    title: 'How to Maximize Your Earnings',
    category: 'Earning Money',
    content: `
# How to Maximize Your Earnings

## Understanding Commission Rates
- Bronze (0-49 referrals): 5% commission
- Silver (50-199 referrals): 10% commission
- Gold (200-499 referrals): 15% commission
- Platinum (500-999 referrals): 17.5% commission
- Diamond (1000+ referrals): 20% commission

## Monthly Bonuses
- Silver: 50 SC bonus
- Gold: 200 SC bonus
- Platinum: 500 SC bonus
- Diamond: 1000 SC bonus

## Strategies to Increase Earnings
1. Focus on quality over quantity
2. Target engaged audiences
3. Use multiple promotion channels
4. A/B test your messaging
5. Build an email list
6. Create valuable content
7. Engage with your community

## Tier Progression
- Track your referral count
- Monitor your progress toward the next tier
- Each tier unlock increases your earning potential
- Aim for Diamond tier for maximum earnings

## Payment Schedule
- Bronze: Monthly payouts
- Silver: Weekly payouts
- Gold: Bi-weekly payouts
- Platinum: Weekly payouts
- Diamond: Daily payouts
    `,
    views: 890,
    helpful: 750,
    notHelpful: 30,
    createdAt: new Date('2026-01-15'),
    updatedAt: new Date('2026-04-05'),
    tags: ['earnings', 'money', 'commissions', 'tiers'],
  },
  {
    id: 'marketing_strategies',
    title: 'Effective Marketing Strategies',
    category: 'Marketing',
    content: `
# Effective Marketing Strategies for CoinKrazy

## Email Marketing
- Build an email list
- Send personalized referral invites
- Share exclusive offers
- Include your affiliate code
- Track open rates and clicks

## Social Media
- Share on Facebook, Twitter, Instagram, TikTok
- Create engaging content
- Use relevant hashtags
- Engage with your followers
- Post consistently

## Content Marketing
- Write blog posts about CoinKrazy
- Create YouTube reviews
- Share gaming tips and strategies
- Provide value to your audience
- Include your referral link

## Community Engagement
- Participate in gaming forums
- Join Discord communities
- Answer questions
- Build trust
- Become a trusted resource

## Paid Advertising
- Use Google Ads
- Facebook/Instagram ads
- TikTok promotions
- Set realistic budgets
- Track ROI carefully

## Best Practices
- Be authentic
- Focus on benefits
- Build relationships
- Test strategies
- Optimize based on data
    `,
    views: 650,
    helpful: 520,
    notHelpful: 25,
    createdAt: new Date('2026-02-01'),
    updatedAt: new Date('2026-04-10'),
    tags: ['marketing', 'strategies', 'promotion'],
  },
];

/**
 * FAQ Items
 */
export const FAQ_ITEMS: FAQ[] = [
  {
    id: 'faq_1',
    question: 'How do I get my affiliate code?',
    answer: 'Once your application is approved, you\'ll receive your unique affiliate code via email. You can also find it in your affiliate dashboard under "My Account".',
    category: 'Getting Started',
    views: 2100,
    helpful: 1900,
    relatedArticles: ['getting_started'],
  },
  {
    id: 'faq_2',
    question: 'When do I get paid?',
    answer: 'Payment frequency depends on your tier: Bronze (Monthly), Silver (Weekly), Gold (Bi-weekly), Platinum (Weekly), Diamond (Daily). Minimum payout is 100 SC.',
    category: 'Payments',
    views: 1800,
    helpful: 1650,
    relatedArticles: ['earning_money'],
  },
  {
    id: 'faq_3',
    question: 'What is the minimum payout amount?',
    answer: 'The minimum payout amount is 100 SC. You must also be KYC verified to request a payout.',
    category: 'Payments',
    views: 1200,
    helpful: 1050,
    relatedArticles: ['earning_money'],
  },
  {
    id: 'faq_4',
    question: 'Can I change my payment method?',
    answer: 'Yes, you can update your payment method in your affiliate dashboard under "Settings". You can choose between Bank Transfer, PayPal, Cryptocurrency, or Store Credit.',
    category: 'Payments',
    views: 890,
    helpful: 780,
    relatedArticles: [],
  },
  {
    id: 'faq_5',
    question: 'How do I increase my commission rate?',
    answer: 'Your commission rate increases as you reach higher tiers based on your referral count. Each tier unlock gives you a higher commission percentage and additional benefits.',
    category: 'Earnings',
    views: 1450,
    helpful: 1300,
    relatedArticles: ['earning_money'],
  },
  {
    id: 'faq_6',
    question: 'What marketing materials are available?',
    answer: 'We provide banners (multiple sizes), email templates, social media content, landing page templates, and video assets. All materials are available for download in your affiliate dashboard.',
    category: 'Marketing',
    views: 980,
    helpful: 850,
    relatedArticles: ['marketing_strategies'],
  },
];

/**
 * Support Categories
 */
export const SUPPORT_CATEGORIES = [
  { id: 'technical', name: 'Technical Issues', description: 'Dashboard, links, tracking problems' },
  { id: 'payment', name: 'Payment & Payouts', description: 'Payout requests, payment methods' },
  { id: 'marketing', name: 'Marketing Help', description: 'Assets, strategies, promotion' },
  { id: 'account', name: 'Account Management', description: 'Profile, settings, verification' },
  { id: 'other', name: 'Other', description: 'General questions and inquiries' },
];

/**
 * Get knowledge base article by ID
 */
export function getKnowledgeBaseArticle(id: string): KnowledgeBaseArticle | undefined {
  return KNOWLEDGE_BASE_ARTICLES.find(article => article.id === id);
}

/**
 * Get all knowledge base articles
 */
export function getAllKnowledgeBaseArticles(): KnowledgeBaseArticle[] {
  return KNOWLEDGE_BASE_ARTICLES;
}

/**
 * Search knowledge base
 */
export function searchKnowledgeBase(query: string): KnowledgeBaseArticle[] {
  const lowerQuery = query.toLowerCase();
  return KNOWLEDGE_BASE_ARTICLES.filter(
    article =>
      article.title.toLowerCase().includes(lowerQuery) ||
      article.content.toLowerCase().includes(lowerQuery) ||
      article.tags.some(tag => tag.toLowerCase().includes(lowerQuery))
  );
}

/**
 * Get FAQ by ID
 */
export function getFAQItem(id: string): FAQ | undefined {
  return FAQ_ITEMS.find(item => item.id === id);
}

/**
 * Get all FAQ items
 */
export function getAllFAQItems(): FAQ[] {
  return FAQ_ITEMS;
}

/**
 * Get FAQ by category
 */
export function getFAQByCategory(category: string): FAQ[] {
  return FAQ_ITEMS.filter(item => item.category === category);
}

/**
 * Search FAQ
 */
export function searchFAQ(query: string): FAQ[] {
  const lowerQuery = query.toLowerCase();
  return FAQ_ITEMS.filter(
    item =>
      item.question.toLowerCase().includes(lowerQuery) ||
      item.answer.toLowerCase().includes(lowerQuery)
  );
}

/**
 * Create support ticket
 */
export function createSupportTicket(
  affiliateId: number,
  subject: string,
  message: string,
  category: string
): Partial<SupportTicket> {
  return {
    affiliateId,
    subject,
    message,
    category,
    priority: 'medium',
    status: 'open',
    createdAt: new Date(),
  };
}

/**
 * Get support category name
 */
export function getSupportCategoryName(categoryId: string): string {
  const category = SUPPORT_CATEGORIES.find(c => c.id === categoryId);
  return category?.name || categoryId;
}

/**
 * Mark FAQ as helpful
 */
export function markFAQAsHelpful(faqId: string, helpful: boolean): void {
  const faq = FAQ_ITEMS.find(item => item.id === faqId);
  if (faq) {
    if (helpful) {
      faq.helpful++;
    } else {
      faq.notHelpful++;
    }
  }
}

/**
 * Generate support ticket response email
 */
export function generateSupportTicketResponseEmail(
  affiliateName: string,
  ticketId: number,
  response: string
): { subject: string; htmlContent: string } {
  return {
    subject: `Support Ticket #${ticketId} Response`,
    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; border-bottom: 2px solid #667eea; }
    .content { padding: 20px 0; }
    .response { background-color: #f0f4ff; border-left: 4px solid #667eea; padding: 15px; margin: 20px 0; }
  </style>
</head>
<body>
  <div class="container">
    <div class="header">
      <h2>Support Ticket Response</h2>
      <p>Ticket #${ticketId}</p>
    </div>
    
    <div class="content">
      <p>Hi ${affiliateName},</p>
      
      <p>Thank you for contacting CoinKrazy support. Here's the response to your ticket:</p>
      
      <div class="response">
        ${response}
      </div>
      
      <p>If you have any follow-up questions, please reply to this email or visit your support dashboard.</p>
      
      <p>Best regards,<br>CoinKrazy Support Team</p>
    </div>
  </div>
</body>
</html>
    `,
  };
}
