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

    // Get request body
    const body = await request.json();
    const { reason } = body;

    // Reject the application
    await territoryManagerAPI.reviewApplication(id, 'reject', reason);

    // Send rejection email to applicant
    await sendRejectionEmail(id, reason);

    // Log the rejection action

    return new Response(JSON.stringify({ 
      success: true, 
      message: 'Application rejected 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 sendRejectionEmail(applicationId: string, reason?: 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: 'Territory Manager Application Update - Black Canyon Tickets',
      html: `
        <h2>Thank you for your interest in the Territory Manager program</h2>
        <p>Hi ${application.full_name},</p>
        <p>Thank you for taking the time to apply for the Black Canyon Tickets Territory Manager program. After careful review, we have decided not to move forward with your application at this time.</p>
        
        ${reason ? `
        <h3>Feedback</h3>
        <p>${reason}</p>
        ` : ''}
        
        <h3>What's Next?</h3>
        <p>While we won't be moving forward with your application at this time, we encourage you to:</p>
        <ul>
          <li>Gain more experience in event management or sales</li>
          <li>Build connections in your local event community</li>
          <li>Consider reapplying in the future when you have more relevant experience</li>
        </ul>
        
        <p>We appreciate your interest in Black Canyon Tickets and wish you the best in your future endeavors.</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) {

  }
}