'use client';

import { View, StyleSheet } from '@react-pdf/renderer';
import type { PdfColumn } from './types';
import { PdfThead } from './PdfThead';
import { PdfTbody } from './PdfTbody';
import { PdfTfoot } from './PdfTfoot';

const styles = StyleSheet.create({
  table: {
    borderWidth: 1,
    borderColor: '#000000',
    marginBottom: 15,
  },
});

interface PdfTableProps<TData = Record<string, unknown>> {
  columns: PdfColumn<TData>[];
  data: TData[];
  showFooter?: boolean;
  footerLabel?: string;
  firstRow?: {
    valueKey: string;
    value: number;
    align?: 'right';
    color?: string;
  };
}

export const PdfTable = <TData = Record<string, unknown>,>({
  columns,
  data,
  showFooter = false,
  footerLabel = 'Total',
  firstRow,
}: PdfTableProps<TData>) => {
  // Check if any column has footer defined
  const hasFooter =
    showFooter || columns.some((col) => col.footer !== undefined);

  return (
    <View style={styles.table}>
      <PdfThead columns={columns} data={data} />
      <PdfTbody columns={columns} data={data} firstRow={firstRow} />
      {hasFooter && data.length > 0 && (
        <PdfTfoot columns={columns} data={data} label={footerLabel} />
      )}
    </View>
  );
};

export type { PdfColumn };
