import React from 'react';

import { Document, Font, Page, StyleSheet, Text, View } from '@react-pdf/renderer';

import { removeHtmlTags } from '@/utils/creatorHelpers';

Font.register({
  family: 'Roboto',
  fonts: [
    {
      src: '/fonts/roboto/Roboto-Regular.ttf',
      fontWeight: 'normal',
    },
    {
      src: '/fonts/roboto/Roboto-Bold.ttf',
      fontWeight: 'bold',
    },
  ],
});

const styles = StyleSheet.create({
  page: {
    padding: 35,
    fontSize: 11,
    fontFamily: 'Roboto',
    color: '#1f2937',
  },
  header: {
    marginBottom: 18,
    paddingBottom: 12,
    borderBottomWidth: 2,
    borderBottomColor: '#1f2937',
    flexDirection: 'row',
    justifyContent: 'space-between',
    alignItems: 'flex-end',
  },
  headerLeft: {
    flex: 1,
  },
  headerTitle: {
    fontSize: 22,
    fontWeight: 'bold',
    marginBottom: 4,
    color: '#111827',
  },
  headerSubtitle: {
    fontSize: 12,
    color: '#111827',
  },
  headerRight: {
    fontSize: 10,
    color: '#9ca3af',
    textAlign: 'right',
  },
  section: {
    marginBottom: 16,
    paddingBottom: 12,
    borderBottomWidth: 0.5,
    borderBottomColor: '#e5e7eb',
  },
  sectionTitle: {
    fontSize: 13,
    fontWeight: 'bold',
    marginBottom: 12,
    color: '#111827',
  },
  sectionSubtitle: {
    fontSize: 12,
    color: '#111827',
    marginBottom: 24,
  },
  paragraph: {
    lineHeight: 1.35,
    marginBottom: 6,
  },
});

type CreatorTextSection = {
  title?: string;
  subtitle?: string | null;
  content: string;
};

const htmlToParagraphs = (html: string): string[] => {
  if (!html) return [];

  const withBreaks = html
    .replace(/<br\s*\/?>/gi, '\n')
    .replace(/<\/p>/gi, '\n\n')
    .replace(/<\/(div|h[1-6]|li)>/gi, '\n')
    .replace(/<li[^>]*>/gi, '• ')
    .replace(/<\/?p[^>]*>/gi, '');

  const cleaned = removeHtmlTags(withBreaks);
  return cleaned
    .split(/\n{2,}/)
    .map((block) => block.replace(/\n+/g, ' ').trim())
    .filter(Boolean);
};

interface CreatorTextPDFDocumentProps {
  creatorName: string;
  sections: CreatorTextSection[];
}

const CreatorTextPDFDocument: React.FC<CreatorTextPDFDocumentProps> = ({
  creatorName,
  sections,
}) => (
  <Document>
    <Page size="A4" style={styles.page}>
      <View style={styles.header}>
        <View style={styles.headerLeft}>
          <Text style={styles.headerTitle}>{creatorName}</Text>
          <Text style={styles.headerSubtitle}>azopus.hu</Text>
        </View>
        <Text style={styles.headerRight}>{new Date().toISOString().slice(0, 10)}</Text>
      </View>

      {sections.map((section, index) => {
        const paragraphs = htmlToParagraphs(section.content);
        return (
          <View key={`${section.title || 'section'}-${index}`} style={styles.section}>
            {section.title ? <Text style={styles.sectionTitle}>{section.title}</Text> : null}
            {section.subtitle ? (
              <Text style={styles.sectionSubtitle}>{section.subtitle}</Text>
            ) : null}
            {paragraphs.map((paragraph, index) => (
              <Text key={`${section.title}-${index}`} style={styles.paragraph}>
                {paragraph}
              </Text>
            ))}
          </View>
        );
      })}
    </Page>
  </Document>
);

export default CreatorTextPDFDocument;
