import jwt from 'jsonwebtoken';
import { z } from "zod";
import { redirect } from 'next/navigation';
import { MontonioOrderToken } from "@/app/home/(user)/_components/cart/types";
import { loadCurrentUserAccount } from "@/app/home/(user)/_lib/server/load-user-account";
import { listProductTypes } from "@lib/data/products";
import { placeOrder, retrieveCart } from "@lib/data/cart";
import { createI18nServerInstance } from "~/lib/i18n/i18n.server";
import { createOrder } from '~/lib/services/order.service';
import { getOrderedAnalysisElementsIds, sendOrderToMedipost } from '~/lib/services/medipost.service';

const ANALYSIS_PACKAGES_TYPE_HANDLE = 'analysis-packages';
const MONTONIO_PAID_STATUS = 'PAID';

const env = () => z
  .object({
    emailSender: z
      .string({
        required_error: 'EMAIL_SENDER is required',
      })
      .min(1),
    siteUrl: z
      .string({
        required_error: 'NEXT_PUBLIC_SITE_URL is required',
      })
      .min(1),
  })
  .parse({
    emailSender: process.env.EMAIL_SENDER,
    siteUrl: process.env.NEXT_PUBLIC_SITE_URL!,
  });

const sendEmail = async ({ email, analysisPackageName, personName, partnerLocationName, language }: { email: string, analysisPackageName: string, personName: string, partnerLocationName: string, language: string }) => {
  try {
    const { renderSynlabAnalysisPackageEmail } = await import('@kit/email-templates');
    const { getMailer } = await import('@kit/mailers');

    const mailer = await getMailer();

    const { html, subject } = await renderSynlabAnalysisPackageEmail({
      analysisPackageName,
      personName,
      partnerLocationName,
      language,
    });

    await mailer
      .sendEmail({
        from: env().emailSender,
        to: email,
        subject,
        html,
      })
      .catch((error) => {
        throw new Error(`Failed to send email, message=${error}`);
      });
  } catch (error) {
    throw new Error(`Failed to send email, message=${error}`);
  }
}

const handleOrderToken = async (orderToken: string) => {
  const secretKey = process.env.MONTONIO_SECRET_KEY as string;

  const decoded = jwt.verify(orderToken, secretKey, {
    algorithms: ['HS256'],
  }) as MontonioOrderToken;
  if (decoded.paymentStatus !== MONTONIO_PAID_STATUS) {
    return null;
  }

  try {
    const [,, cartId] = decoded.merchantReferenceDisplay.split(':');
    if (!cartId) {
      throw new Error("Cart ID not found");
    }

    const cart = await retrieveCart(cartId);
    if (!cart) {
      throw new Error("Cart not found");
    }

    const medusaOrder = await placeOrder(cartId, { revalidateCacheTags: true });
    const orderedAnalysisElements = await getOrderedAnalysisElementsIds({ medusaOrder });
    await createOrder({ medusaOrder, orderedAnalysisElements });

    const { productTypes } = await listProductTypes();
    const analysisPackagesType = productTypes.find(({ metadata }) => metadata?.handle === ANALYSIS_PACKAGES_TYPE_HANDLE);
    const analysisPackageOrderItem = medusaOrder.items?.find(({ product_type_id }) => product_type_id === analysisPackagesType?.id);
    return {
      medusaOrderId: medusaOrder.id,
      email: medusaOrder.email,
      partnerLocationName: analysisPackageOrderItem?.metadata?.partner_location_name as string ?? '',
      analysisPackageName: analysisPackageOrderItem?.title ?? '',
      orderedAnalysisElements,
    };
  } catch (error) {
    console.error("Failed to place order", error);
    throw new Error(`Failed to place order, message=${error}`);
  }
}

interface PageProps {
  searchParams: {
    'order-token'?: string;
  };
}

export default async function MontonioCallbackPage({ searchParams }: PageProps) {
  const { language } = await createI18nServerInstance();
  
  try {
    const orderToken = searchParams['order-token'];
    if (!orderToken) {
      console.error("Order token is missing");
      redirect('/home/cart/montonio-callback/error');
    }

    const account = await loadCurrentUserAccount();
    if (!account) {
      console.error("Account not found in context");
      redirect('/home/cart/montonio-callback/error');
    }

    const orderResult = await handleOrderToken(orderToken);
    if (!orderResult) {
      console.error("Order result is missing");
      redirect('/home/cart/montonio-callback/error');
    }

    const { medusaOrderId, email, partnerLocationName, analysisPackageName, orderedAnalysisElements } = orderResult;
    const personName = account.name;
    
    if (email && analysisPackageName) {
      try {
        await sendEmail({ email, analysisPackageName, personName, partnerLocationName, language });
      } catch (error) {
        console.error("Failed to send email", error);
      }
    } else {
      // @TODO send email for separate analyses
      console.error("Missing email or analysisPackageName", orderResult);
    }
    
    // Send order to Medipost (no await to avoid blocking the redirect)
    sendOrderToMedipost({ medusaOrderId, orderedAnalysisElements });
    
    redirect('/home/order');
  } catch (error) {
    console.error("Failed to place order", error);
    redirect('/home/cart/montonio-callback/error');
  }
}
