/**
 * Seed Workflow Templates
 *
 * Creates the default "Marketing Launch Workflow" template if it doesn't already exist.
 * Uses upsert by slug to prevent duplicates on re-run.
 * Called on server startup.
 */

import { WorkflowTemplate } from '../models/WorkflowTemplate';
import type { IWorkflowTemplate } from '../models/WorkflowTemplate';

const DEFAULT_TEMPLATE = {
  name: 'Marketing Launch Workflow',
  slug: 'marketing-launch-workflow',
  description: 'Complete these essential setup actions to launch your marketing campaign.',
  status: 'active' as const,
  displayOrder: 0,
  icon: 'Rocket',
  category: 'marketing',
  version: 1,
  steps: [
    {
      stepKey: 'create-book',
      title: 'Book Creation',
      description: 'Create and configure your book to establish thought leadership and generate leads.',
      stepOrder: 1,
      icon: 'BookOpen',
      ctaLabel: 'Start',
      route: '/books',
      completionRule: 'single_action' as const,
      helpText: 'Create at least one book to mark this step complete.',
      actions: [
        {
          actionKey: 'create-book',
          label: 'Create Book',
          module: 'books',
          route: '/books',
          completionEvent: 'book_created',
          completionKey: 'books',
          actionOrder: 1,
        },
      ],
    },
    {
      stepKey: 'create-landing-page',
      title: 'Landing Page Creation',
      description: 'Create a landing page for your book campaign to capture leads and drive conversions.',
      stepOrder: 2,
      icon: 'PanelTop',
      ctaLabel: 'Start',
      route: '/landing-pages',
      completionRule: 'single_action' as const,
      helpText: 'Create at least one landing page to mark this step complete.',
      actions: [
        {
          actionKey: 'create-landing-page',
          label: 'Create Landing Page',
          module: 'landing-pages',
          route: '/landing-pages',
          completionEvent: 'landing_page_created',
          completionKey: 'landingPages',
          actionOrder: 1,
        },
      ],
    },
    {
      stepKey: 'create-ad-campaign',
      title: 'Ads Creation',
      description: 'Create an advertising campaign using Meta Ads or Google Ads to reach your target audience.',
      stepOrder: 3,
      icon: 'Megaphone',
      ctaLabel: 'Start',
      route: '/ads',
      completionRule: 'any_of_actions' as const,
      helpText: 'Create at least one ad campaign (Meta or Google) to mark this step complete.',
      actions: [
        {
          actionKey: 'create-meta-ad',
          label: 'Create Meta Ad',
          module: 'ads',
          route: '/ads',
          completionEvent: 'meta_ad_created',
          completionKey: 'adCampaigns',
          actionOrder: 1,
        },
        {
          actionKey: 'create-google-ad',
          label: 'Create Google Ad',
          module: 'ads',
          route: '/ads?platform=google',
          completionEvent: 'google_ad_created',
          completionKey: 'adCampaigns',
          actionOrder: 2,
        },
      ],
    },
    {
      stepKey: 'create-newsletter',
      title: 'Newsletter Creation',
      description: 'Create a newsletter campaign to nurture and engage your audience.',
      stepOrder: 4,
      icon: 'MailPlus',
      ctaLabel: 'Start',
      route: '/newsletter-content-os',
      completionRule: 'single_action' as const,
      helpText: 'Create at least one newsletter campaign to mark this step complete.',
      actions: [
        {
          actionKey: 'create-newsletter',
          label: 'Create Newsletter',
          module: 'newsletter',
          route: '/newsletter-content-os',
          completionEvent: 'newsletter_created',
          completionKey: 'newsletterCampaigns',
          actionOrder: 1,
        },
      ],
    },
    {
      stepKey: 'create-whatsapp-campaign',
      title: 'WhatsApp Nurturing',
      description: 'Create a WhatsApp nurturing campaign to engage leads directly.',
      stepOrder: 5,
      icon: 'MessageSquareQuote',
      ctaLabel: 'Start',
      route: '/whatsapp-nurturing',
      completionRule: 'single_action' as const,
      helpText: 'Create at least one WhatsApp campaign to mark this step complete.',
      actions: [
        {
          actionKey: 'create-whatsapp-campaign',
          label: 'Create WhatsApp Campaign',
          module: 'whatsapp-nurturing',
          route: '/whatsapp-nurturing',
          completionEvent: 'whatsapp_campaign_created',
          completionKey: 'whatsappCampaigns',
          actionOrder: 1,
        },
      ],
    },
  ],
};

export async function seedWorkflowTemplates(): Promise<void> {
  try {
    const existing = await WorkflowTemplate.findOne({ slug: DEFAULT_TEMPLATE.slug }).lean();
    if (existing) {
      console.log('[Seed] Default workflow template already exists, skipping seed.');
      return;
    }

    await WorkflowTemplate.create(DEFAULT_TEMPLATE as Partial<IWorkflowTemplate>);
    console.log('[Seed] Default workflow template created successfully.');
  } catch (error: any) {
    console.error('[Seed] Failed to seed workflow templates:', error.message);
  }
}