import { useCallback, useState } from 'react';
import { notifications } from '@mantine/notifications';
import { useTranslation } from 'react-i18next';
import { extractErrorMessage } from '@ema-platform/api';
import { authStorage } from '@ema-platform/auth';
import { API_BASE_URL, pageOptionsFor } from '../config/designer';

interface PreviewArgs {
  hbsSource: string;
  licenseTypeId: string | null;
  landscape: boolean;
  pageWidth?: string;
  pageHeight?: string;
}

/**
 * Renders the editor's current contents, not the saved row, so unsaved edits
 * are what you see. Opened as a blob into `PdfPreviewModal` rather than a new
 * tab, so the designer never loses their place.
 */
export function useTemplatePreview() {
  const { t } = useTranslation();
  const [previewUrl, setPreviewUrl] = useState<string | null>(null);

  const open = useCallback(
    async ({ hbsSource, licenseTypeId, landscape, pageWidth, pageHeight }: PreviewArgs) => {
      try {
        // The preview returns a PDF stream, not JSON, so it bypasses RTK Query
        // and calls the API directly — which means spelling out the base URL and
        // the bearer token that the shared baseQuery would normally attach.
        const token = authStorage.getToken();
        const response = await fetch(`${API_BASE_URL}/license-templates/preview`, {
          method: 'POST',
          headers: {
            'Content-Type': 'application/json',
            ...(token ? { Authorization: `Bearer ${token}` } : {}),
          },
          body: JSON.stringify({
            hbsSource,
            licenseTypeId,
            pageOptions: pageOptionsFor(
              landscape,
              pageWidth && pageHeight ? { width: pageWidth, height: pageHeight } : undefined,
            ),
          }),
        });
        if (!response.ok) throw new Error(await response.text());
        setPreviewUrl(URL.createObjectURL(await response.blob()));
      } catch (err) {
        notifications.show({
          color: 'red',
          title: t('designer.previewFailed', 'Could not render the preview'),
          message: extractErrorMessage(err),
        });
      }
    },
    [t],
  );

  const close = useCallback(() => {
    setPreviewUrl((current) => {
      if (current) URL.revokeObjectURL(current);
      return null;
    });
  }, []);

  return { previewUrl, open, close };
}
