'use server';

import { sendEmail } from '@/lib/mail/mailer';
import { env } from '@/lib/env';
import { render, ContactFormNotification, ConfirmationMessage } from '@mintel/mail';
import React from 'react';
import { getServerAppServices } from '@/lib/services/create-services.server';

export async function sendContactFormAction(formData: FormData) {
  const services = getServerAppServices();
  const logger = services.logger.child({ action: 'sendContactFormAction' });

  // Set analytics context from request headers for high-fidelity server-side tracking
  const { headers } = await import('next/headers');
  const requestHeaders = await headers();

  if ('setServerContext' in services.analytics) {
    (services.analytics as any).setServerContext({
      userAgent: requestHeaders.get('user-agent') || undefined,
      language: requestHeaders.get('accept-language')?.split(',')[0] || undefined,
      referrer: requestHeaders.get('referer') || undefined,
      ip: requestHeaders.get('x-forwarded-for')?.split(',')[0] || undefined,
    });
  }

  // Track attempt
  services.analytics.track('contact-form-attempt');

  const name = formData.get('name') as string;
  const email = formData.get('email') as string;
  const message = formData.get('message') as string;
  const productName = formData.get('productName') as string | null;

  if (!name || !email || !message) {
    logger.warn('Missing required fields in contact form', {
      name: !!name,
      email: !!email,
      message: !!message,
    });
    return { success: false, error: 'Missing required fields' };
  }

  // 1. Save to CMS
  try {
    const { getPayload } = await import('payload');
    const configPromise = (await import('@payload-config')).default;
    const payload = await getPayload({ config: configPromise });

    await payload.create({
      collection: 'form-submissions',
      data: {
        name,
        email,
        message,
        type: productName ? 'product_quote' : 'contact',
        productName: productName || undefined,
      },
      overrideAccess: true,
    });

    logger.info('Successfully saved form submission to Payload CMS', {
      type: productName ? 'product_quote' : 'contact',
      email,
    });
  } catch (error) {
    logger.error('Failed to store submission in Payload CMS', { error });
    services.errors.captureException(error, { action: 'payload_store_submission' });
  }

  // 2. Send Emails
  logger.info('Sending branded emails', { email, productName });

  const notificationSubject = productName
    ? `Product Inquiry: ${productName}`
    : 'New Contact Form Submission';
  const confirmationSubject = 'Thank you for your inquiry';
  const isTestSubmission = email === 'testing@mintel.me';

  try {
    // 2a. Send notification to Mintel/Client
    const notificationHtml = await render(
      React.createElement(ContactFormNotification, {
        name,
        email,
        message,
        productName: productName || undefined,
      }),
    );

    if (!isTestSubmission) {
      logger.info('Sending internal notification', { recipients: env.MAIL_RECIPIENTS });
      const notificationResult = await sendEmail({
        replyTo: email,
        subject: notificationSubject,
        html: notificationHtml,
      });

      if (notificationResult.success) {
        logger.info('Notification email sent successfully', {
          messageId: notificationResult.messageId,
        });
      } else {
        logger.error('Notification email DELIVERY FAILED', {
          error: notificationResult.error,
          subject: notificationSubject,
          recipients: env.MAIL_RECIPIENTS,
        });
        services.errors.captureException(
          new Error(`Notification email failed: ${notificationResult.error}`),
          { 
            action: 'sendContactFormAction_notification', 
            email, 
            recipients: env.MAIL_RECIPIENTS 
          },
        );
      }
    } else {
      logger.info('Skipping notification email for test submission', { email });
    }

    // 2b. Send confirmation to Customer (branded as KLZ Cables)
    const confirmationHtml = await render(
      React.createElement(ConfirmationMessage, {
        name,
        clientName: 'KLZ Cables',
        // brandColor: '#82ed20', // Optional: could be KLZ specific
      }),
    );

    if (!isTestSubmission) {
      logger.info('Sending customer confirmation', { to: email });
      const confirmationResult = await sendEmail({
        to: email,
        subject: confirmationSubject,
        html: confirmationHtml,
      });

      if (confirmationResult.success) {
        logger.info('Confirmation email sent successfully', {
          messageId: confirmationResult.messageId,
        });
      } else {
        logger.error('Confirmation email DELIVERY FAILED', {
          error: confirmationResult.error,
          subject: confirmationSubject,
          to: email,
        });
        services.errors.captureException(
          new Error(`Confirmation email failed: ${confirmationResult.error}`),
          { action: 'sendContactFormAction_confirmation', email },
        );
      }
    } else {
      logger.info('Skipping confirmation email for test submission', { email });
    }

    // Notify via Gotify (Internal)
    await services.notifications.notify({
      title: `📩 ${notificationSubject}`,
      message: `New message from ${name} (${email}):\n\n${message}`,
      priority: 5,
    });

    // Track success
    services.analytics.track('contact-form-success', {
      is_product_request: !!productName,
    });

    return { success: true };
  } catch (error) {
    const errorMsg = error instanceof Error ? error.message : String(error);
    logger.error('Failed to send branded emails', {
      error: errorMsg,
      stack: error instanceof Error ? error.stack : undefined,
    });

    services.errors.captureException(error, { action: 'sendContactFormAction', email });

    await services.notifications.notify({
      title: '🚨 Contact Form Error',
      message: `Failed to send emails for ${name} (${email}). Error: ${errorMsg}`,
      priority: 8,
    });

    return { success: false, error: errorMsg };
  }
}
