import { NextResponse } from 'next/server';
import { WelcomeTemplate } from '@/lib/email/templates/Transactional';
import { JobApprovedTemplate, JobRejectedTemplate } from '@/lib/email/templates/JobRelated';
import { InterviewInvitationTemplate } from '@/lib/email/templates/Interview';
import { NewMessageTemplate } from '@/lib/email/templates/Messaging';

// This is a development-only route to preview email templates in the browser.
// It should be restricted or removed in a strict production environment.
export async function GET(request: Request) {
  // Add a simple security check to only allow in development mode or with a secret key
  if (process.env.NODE_ENV === 'production' && request.headers.get('x-preview-secret') !== process.env.EMAIL_PREVIEW_SECRET) {
    return new NextResponse('Unauthorized in production without secret', { status: 401 });
  }

  const { searchParams } = new URL(request.url);
  const type = searchParams.get('type') || 'welcome';

  let html = '';

  try {
    switch (type) {
      case 'welcome':
        html = WelcomeTemplate({
          name: 'Sarah Candidate',
          loginUrl: 'http://localhost:3000/login'
        });
        break;

      case 'job-approved':
        html = JobApprovedTemplate({
          employerName: 'Acme Corp Hiring Team',
          jobTitle: 'Senior Frontend Node Engineer',
          companyName: 'Acme Corp',
          jobId: '12345-abcde'
        });
        break;

      case 'job-rejected':
        html = JobRejectedTemplate({
          employerName: 'Acme Corp Hiring Team',
          jobTitle: 'Senior Frontend Node Engineer',
          companyName: 'Acme Corp',
          jobId: '12345-abcde',
          reason: 'Salary expectation range is required per local state laws.',
          details: 'Please update the minimum and maximum salary fields in the job posting form.'
        });
        break;

      case 'interview':
        html = InterviewInvitationTemplate({
          candidateName: 'Sarah Candidate',
          companyName: 'Tech Innovators Inc.',
          positionTitle: 'Full Stack React Developer',
          interviewDate: 'Thursday, October 24th',
          interviewTime: '2:00 PM EST',
          interviewFormat: 'Video Call (Google Meet)',
          interviewerName: 'Jane Doe, Lead Engineer',
          joinUrl: 'http://localhost:3000/dashboard/interviews/detail/789',
          interviewLink: 'https://meet.google.com/abc-defg-hij'
        });
        break;

      case 'message':
        html = NewMessageTemplate({
          recipientName: 'Sarah Candidate',
          senderName: 'John Recruiter',
          companyName: 'Stripe',
          jobTitle: 'Backend Engineer',
          messagePreview: 'Hi Sarah, your background in distributed systems is exactly what we are looking for. Do you have time tomorrow for a quick intro call?',
          applicationId: 'app-999'
        });
        break;

      default:
        return new NextResponse(`
          <html>
            <body style="padding: 40px; font-family: sans-serif;">
              <h2>Email Template Preview</h2>
              <p>Select a template to preview:</p>
              <ul>
                <li><a href="?type=welcome">Welcome Email</a></li>
                <li><a href="?type=job-approved">Job Approved Email</a></li>
                <li><a href="?type=job-rejected">Job Rejected Email</a></li>
                <li><a href="?type=interview">Interview Invitation Email</a></li>
                <li><a href="?type=message">New Message Email</a></li>
              </ul>
            </body>
          </html>
        `, { headers: { 'Content-Type': 'text/html' } });
    }

    return new NextResponse(html, {
      headers: {
        'Content-Type': 'text/html; charset=utf-8',
        'Cache-Control': 'no-store'
      }
    });

  } catch (error: any) {
    return new NextResponse(`Error rendering template: ${error.message}`, { status: 500 });
  }
}
