import { NextRequest, NextResponse } from 'next/server'
import { verifyAdminAuth } from '@/lib/admin-auth'
import { mailgunService } from '@/lib/mailgun'

export const runtime = 'nodejs'

export async function POST(request: NextRequest) {
  try {
    const adminUser = await verifyAdminAuth(request)
    if (!adminUser) {
      return NextResponse.json({ error: 'Unauthorized' }, { status: 401 })
    }

    const body = await request.json()
    const { testType, email } = body

    if (testType === 'connection') {
      // Test connection to Mailgun
      const result = await mailgunService.testConnection()
      return NextResponse.json(result)
    }

    if (testType === 'email') {
      // Send test email
      if (!email) {
        return NextResponse.json({
          success: false,
          error: 'Email address is required for email test'
        }, { status: 400 })
      }

      const result = await mailgunService.sendEmail({
        to: email,
        subject: 'Biblical Guide - Email Configuration Test',
        text: `Hello,

This is a configuration test email from Biblical Guide.

Your email system has been successfully configured and is working properly. You can now receive important notifications from our platform.

Best regards,
The Biblical Guide Team

---
This is an automated message. Please do not reply to this email.
Time: ${new Date().toLocaleString()}`,
        html: `
          <!DOCTYPE html>
          <html>
          <head>
            <meta charset="utf-8">
            <meta name="viewport" content="width=device-width, initial-scale=1.0">
            <title>Biblical Guide - Email Configuration Test</title>
          </head>
          <body style="font-family: Arial, sans-serif; line-height: 1.6; color: #333; max-width: 600px; margin: 0 auto; padding: 20px;">
            <div style="background-color: #f8f9fa; padding: 20px; border-radius: 8px; margin-bottom: 20px;">
              <h2 style="color: #2c3e50; margin: 0;">Biblical Guide</h2>
              <p style="margin: 5px 0 0 0; color: #6c757d;">Email Configuration Test</p>
            </div>

            <div style="background-color: white; padding: 20px; border-radius: 8px; border: 1px solid #e9ecef;">
              <p>Hello,</p>

              <p>This is a configuration test email from Biblical Guide.</p>

              <p><strong>Your email system has been successfully configured and is working properly.</strong> You can now receive important notifications from our platform.</p>

              <div style="background-color: #d4edda; border: 1px solid #c3e6cb; border-radius: 4px; padding: 15px; margin: 20px 0;">
                <p style="margin: 0; color: #155724;"><strong>✓ Configuration successful</strong></p>
              </div>

              <p>Best regards,<br>
              The Biblical Guide Team</p>
            </div>

            <div style="margin-top: 20px; padding: 15px; background-color: #f8f9fa; border-radius: 8px; font-size: 12px; color: #6c757d;">
              <p style="margin: 0;">This is an automated message. Please do not reply to this email.</p>
              <p style="margin: 5px 0 0 0;">Time: ${new Date().toLocaleString()}</p>
            </div>
          </body>
          </html>
        `
      })

      return NextResponse.json(result)
    }

    return NextResponse.json({
      success: false,
      error: 'Invalid test type'
    }, { status: 400 })

  } catch (error) {
    console.error('Error testing Mailgun:', error)
    return NextResponse.json({
      success: false,
      error: error instanceof Error ? error.message : 'Test failed'
    }, { status: 500 })
  }
}