const { PrismaClient } = require('@prisma/client');

const prisma = new PrismaClient();

const sampleTemplates = [
  {
    name: "Welcome Email",
    value: "welcome_email",
    description: "Welcome email for new users",
    subject: "Welcome to our platform!",
    preheader: "Get started with your new account",
    email_content: `
      <h1>Welcome {{user_name}}!</h1>
      <p>Thank you for joining our platform. We're excited to have you aboard!</p>
      <p>Here are some things you can do to get started:</p>
      <ul>
        <li>Complete your profile</li>
        <li>Explore our features</li>
        <li>Connect with other users</li>
      </ul>
      <p>If you have any questions, feel free to reach out to our support team.</p>
      <p>Best regards,<br>The Team</p>
    `,
    category: "user_management",
    channels: ["email"],
    status: "Active",
    version: "1.0",
    is_personal: false,
    from_name: "Welcome Team",
    reply_to: "support@example.com",
    track_opens: true,
    variables: [
      { name: "user_name", description: "User's full name", required: true },
      { name: "user_email", description: "User's email address", required: true }
    ],
    is_active: true,
    created_by: "system",
    updated_by: "system"
  },
  {
    name: "Password Reset",
    value: "password_reset",
    description: "Password reset notification",
    subject: "Reset your password",
    preheader: "You requested a password reset",
    email_content: `
      <h1>Password Reset Request</h1>
      <p>Hi {{user_name}},</p>
      <p>You recently requested to reset your password. Click the button below to proceed:</p>
      <p><a href="{{reset_url}}" style="background: #007bff; color: white; padding: 10px 20px; text-decoration: none; border-radius: 5px;">Reset Password</a></p>
      <p>If you didn't request this, please ignore this email.</p>
      <p>This link will expire in 24 hours.</p>
    `,
    category: "security",
    channels: ["email"],
    status: "Active",
    version: "1.0",
    is_personal: false,
    from_name: "Security Team",
    reply_to: "security@example.com",
    track_opens: true,
    variables: [
      { name: "user_name", description: "User's full name", required: true },
      { name: "reset_url", description: "Password reset URL", required: true }
    ],
    is_active: true,
    created_by: "system",
    updated_by: "system"
  },
  {
    name: "Order Confirmation",
    value: "order_confirmation",
    description: "Order confirmation notification",
    subject: "Order Confirmed - {{order_number}}",
    preheader: "Your order has been confirmed",
    email_content: `
      <h1>Order Confirmation</h1>
      <p>Hi {{customer_name}},</p>
      <p>Your order <strong>{{order_number}}</strong> has been confirmed!</p>
      <p>Order Total: <strong>{{order_total}}</strong></p>
      <p>Estimated delivery: {{delivery_date}}</p>
      <p>You can track your order status in your account dashboard.</p>
      <p>Thank you for your business!</p>
    `,
    push_title: "Order Confirmed!",
    push_body: "Your order {{order_number}} has been confirmed and is being processed.",
    category: "orders",
    channels: ["email", "push"],
    status: "Active",
    version: "1.0",
    is_personal: false,
    from_name: "Orders Team",
    reply_to: "orders@example.com",
    track_opens: true,
    variables: [
      { name: "customer_name", description: "Customer's name", required: true },
      { name: "order_number", description: "Order number", required: true },
      { name: "order_total", description: "Order total amount", required: true },
      { name: "delivery_date", description: "Estimated delivery date", required: true }
    ],
    is_active: true,
    created_by: "system",
    updated_by: "system"
  },
  {
    name: "Marketing Newsletter",
    value: "marketing_newsletter",
    description: "Monthly marketing newsletter",
    subject: "{{newsletter_title}} - Don't miss out!",
    preheader: "Exclusive offers and updates inside",
    email_content: `
      <h1>{{newsletter_title}}</h1>
      <p>Hi {{subscriber_name}},</p>
      <p>Here are this month's highlights:</p>
      <div>{{newsletter_content}}</div>
      <p>Don't forget to follow us on social media for daily updates!</p>
      <p>Best regards,<br>Marketing Team</p>
      <p><small><a href="{{unsubscribe_url}}">Unsubscribe</a></small></p>
    `,
    category: "marketing",
    channels: ["email"],
    status: "Draft",
    version: "1.0",
    is_personal: false,
    from_name: "Marketing Team",
    reply_to: "marketing@example.com",
    track_opens: true,
    variables: [
      { name: "subscriber_name", description: "Subscriber's name", required: true },
      { name: "newsletter_title", description: "Newsletter title", required: true },
      { name: "newsletter_content", description: "Newsletter content", required: true },
      { name: "unsubscribe_url", description: "Unsubscribe URL", required: true }
    ],
    is_active: false,
    created_by: "system",
    updated_by: "system"
  }
];

async function seedTemplates() {
  try {
    console.log('Starting to seed notification templates...');
    
    // Clear existing sample data
    await prisma.notification_templates.deleteMany({
      where: {
        created_by: "system"
      }
    });
    
    console.log('Cleared existing sample templates');
    
    // Insert sample templates
    for (const template of sampleTemplates) {
      const created = await prisma.notification_templates.create({
        data: template
      });
      console.log(`Created template: ${created.name} (${created.id})`);
    }
    
    console.log(`Successfully seeded ${sampleTemplates.length} notification templates!`);
    
  } catch (error) {
    console.error('Error seeding templates:', error);
    throw error;
  } finally {
    await prisma.$disconnect();
  }
}

// Run the seeding
if (require.main === module) {
  seedTemplates()
    .then(() => {
      console.log('Seeding completed successfully!');
      process.exit(0);
    })
    .catch((error) => {
      console.error('Seeding failed:', error);
      process.exit(1);
    });
}

module.exports = seedTemplates; 