export const prerender = false;

import type { APIRoute } from 'astro';
import { territoryManagerAPI } from '../../../../../../lib/territory-manager-api';

export const POST: APIRoute = async ({ params, request }) => {
  try {
    const { id } = params;
    
    if (!id) {
      return new Response(JSON.stringify({ error: 'Application ID is required' }), {
        status: 400,
        headers: { 'Content-Type': 'application/json' }
      });
    }

    // Check admin permissions
    // This would be implemented with proper auth middleware
    // For now, we'll assume the user is authenticated and has admin role

    // Approve the application
    await territoryManagerAPI.reviewApplication(id, 'approve');

    // Send approval email to applicant
    await sendApprovalEmail(id);

    // Log the approval action

    return new Response(JSON.stringify({ 
      success: true, 
      message: 'Application approved successfully' 
    }), {
      status: 200,
      headers: { 'Content-Type': 'application/json' }
    });

  } catch (error) {

    return new Response(JSON.stringify({ error: 'Internal server error' }), {
      status: 500,
      headers: { 'Content-Type': 'application/json' }
    });
  }
};

async function sendApprovalEmail(applicationId: string) {
  // TODO: Implement email sending logic
  // This would integrate with your email service (Resend, SendGrid, etc.)
  
  try {
    // Get application details
    const applications = await territoryManagerAPI.getApplications();
    const application = applications.find(app => app.id === applicationId);
    
    if (!application) {

      return;
    }

    // Email content
    const emailContent = {
      to: application.email,
      subject: 'Welcome to the Black Canyon Tickets Territory Manager Program!',
      html: `
        <h2>Congratulations! Your application has been approved!</h2>
        <p>Hi ${application.full_name},</p>
        <p>We're excited to welcome you to the Black Canyon Tickets Territory Manager program!</p>
        
        <h3>What's Next?</h3>
        <ol>
          <li><strong>Background Check:</strong> You'll receive a separate email with instructions to complete your background check.</li>
          <li><strong>Territory Assignment:</strong> Once your background check is complete, you'll be assigned to your preferred territory.</li>
          <li><strong>Training Access:</strong> You'll receive access to our comprehensive training program.</li>
          <li><strong>Portal Access:</strong> Your Territory Manager portal will be activated within 24 hours.</li>
        </ol>
        
        <h3>Getting Started</h3>
        <p>In the meantime, you can:</p>
        <ul>
          <li>Review our Territory Manager handbook (attached)</li>
          <li>Join our Territory Manager community forum</li>
          <li>Start thinking about local events in your area</li>
        </ul>
        
        <p>We're looking forward to having you on our team and helping you succeed!</p>
        
        <p>Best regards,<br>
        The Black Canyon Tickets Team<br>
        <a href="mailto:support@blackcanyontickets.com">support@blackcanyontickets.com</a></p>
      `
    };

    // TODO: Actually send the email using your email service

  } catch (error) {

  }
}