'use client';

import { Page, View, Document, StyleSheet, Font } from '@react-pdf/renderer';
import {
  DailyMarketingReport,
  SalesSummary,
} from '@/types/api/report/marketing';
import {
  formatCurrency,
  formatDate,
  formatNumber,
  formatTitleCase,
} from '@/lib/helper';
import { PdfTable, PdfColumn } from '@/components/helper/pdf/table';
import { PdfParamBadge } from '@/components/helper/pdf/badge/PdfParamBadge';
import { PdfStatusBadge } from '@/components/helper/pdf/badge/PdfStatusBadge';
import { PdfTypography } from '@/components/helper/pdf/typography/PdfTypography';
import { PdfPageNumber } from '@/components/helper/pdf/layout/PdfPageNumber';

Font.register({
  family: 'Helvetica',
  src: 'helvetica',
});

const pdfStyles = StyleSheet.create({
  page: {
    fontSize: 10,
    fontFamily: 'Helvetica',
    padding: 20,
    backgroundColor: '#FFFFFF',
  },
  titleSection: {
    marginBottom: 10,
  },
  parameterContainer: {
    flexDirection: 'row',
    flexWrap: 'wrap',
    marginBottom: 8,
  },
});

interface DailyMarketingReportPDFProps {
  data?: DailyMarketingReport;
  total?: SalesSummary;
}

const getTableColumns = (
  summary?: SalesSummary
): PdfColumn<DailyMarketingReport[number]>[] => [
  {
    key: 'no',
    header: 'No',
    flex: 0.5,
    align: 'center',
    cell: ({ index }) => index + 1,
  },
  {
    key: 'so_date',
    header: 'Tanggal Sales Order',
    flex: 1.3,
    align: 'center',
    cell: ({ row }) =>
      row.so_date ? formatDate(row.so_date, 'DD MMM YY') : '-',
  },
  {
    key: 'do_date',
    header: 'Tanggal Delivery Order',
    flex: 1.3,
    align: 'center',
    cell: ({ row }) =>
      row.realization_date
        ? formatDate(row.realization_date, 'DD MMM YY')
        : '-',
  },
  {
    key: 'aging',
    header: 'Aging (Hari)',
    flex: 0.7,
    align: 'center',
    cell: ({ row }) => row.aging_days ?? '-',
  },
  {
    key: 'warehouse',
    header: 'Gudang Fisik',
    flex: 1.2,
    align: 'left',
    cell: ({ row }) => row.warehouse?.name ?? '-',
  },
  {
    key: 'customer',
    header: 'Pelanggan',
    flex: 1.5,
    align: 'left',
    cell: ({ row }) => row.customer?.name ?? '-',
  },
  {
    key: 'sales',
    header: 'Sales',
    flex: 1,
    align: 'left',
    cell: ({ row }) => row.sales?.name ?? '-',
  },
  {
    key: 'product',
    header: 'Produk',
    flex: 1.3,
    align: 'left',
    cell: ({ row }) => row.product?.name ?? '-',
  },
  {
    key: 'do_number',
    header: 'Nomor DO',
    flex: 1.2,
    align: 'left',
    cell: ({ row }) => row.do_number ?? '-',
  },
  {
    key: 'vehicle',
    header: 'Nomor Polisi',
    flex: 1,
    align: 'left',
    cell: ({ row }) => row.vehicle_number ?? '-',
  },
  {
    key: 'marketing_type',
    header: 'Tipe Marketing',
    flex: 1,
    align: 'center',
    cell: ({ row }) =>
      row.marketing_type ? (
        <View style={{ alignItems: 'center' }}>
          <PdfStatusBadge
            style={{
              backgroundColor:
                row.marketing_type.toLowerCase() === 'ayam'
                  ? '#FEF3C7'
                  : row.marketing_type.toLowerCase() === 'trading'
                    ? '#DBEAFE'
                    : row.marketing_type.toLowerCase() === 'telur'
                      ? '#D1FAE5'
                      : '#F5F5F5',
              color:
                row.marketing_type.toLowerCase() === 'ayam'
                  ? '#92400E'
                  : row.marketing_type.toLowerCase() === 'trading'
                    ? '#1E40AF'
                    : row.marketing_type.toLowerCase() === 'telur'
                      ? '#065F46'
                      : '#333333',
              borderColor:
                row.marketing_type.toLowerCase() === 'ayam'
                  ? '#FBBF24'
                  : row.marketing_type.toLowerCase() === 'trading'
                    ? '#60A5FA'
                    : row.marketing_type.toLowerCase() === 'telur'
                      ? '#34D399'
                      : '#E5E7EB',
            }}
          >
            {formatTitleCase(row.marketing_type)}
          </PdfStatusBadge>
        </View>
      ) : (
        '-'
      ),
  },
  {
    key: 'qty',
    header: 'Quantity',
    flex: 0.7,
    align: 'right',
    cell: ({ row }) => formatNumber(row.qty ?? 0),
    footer: summary ? formatNumber(summary.total_qty ?? 0) : '',
    footerAlign: 'right',
  },
  {
    key: 'avg_weight',
    header: 'Rata-Rata (Kg)',
    flex: 0.8,
    align: 'right',
    cell: ({ row }) => formatNumber(row.average_weight_kg ?? 0),
    footer: summary ? formatNumber(summary.total_weight_kg ?? 0) : '',
    footerAlign: 'right',
  },
  {
    key: 'total_weight',
    header: 'Total Berat (Kg)',
    flex: 0.9,
    align: 'right',
    cell: ({ row }) => formatNumber(row.total_weight_kg ?? 0),
    footer: summary ? formatNumber(summary.total_weight_kg ?? 0) : '',
    footerAlign: 'right',
  },
  {
    key: 'sales_price',
    header: 'Harga Jual (Rp)',
    flex: 0.9,
    align: 'right',
    cell: ({ row }) => formatCurrency(row.sales_price_per_kg ?? 0),
    footer: '',
  },
  {
    key: 'hpp_price',
    header: 'HPP (Rp)',
    flex: 1.3,
    align: 'right',
    cell: ({ row }) => formatCurrency(row.hpp_price_per_kg ?? 0),
    footer: summary ? formatCurrency(summary.total_hpp_price_per_kg ?? 0) : '',
    footerAlign: 'right',
  },
  {
    key: 'sales_amount',
    header: 'Total Jual (Rp)',
    flex: 1,
    align: 'right',
    cell: ({ row }) => formatCurrency(row.sales_amount ?? 0),
    footer: summary ? formatCurrency(summary.total_sales_amount ?? 0) : '',
    footerAlign: 'right',
  },
  {
    key: 'hpp_amount',
    header: 'Total HPP (Rp)',
    flex: 1.3,
    align: 'right',
    cell: ({ row }) => formatCurrency(row.hpp_amount ?? 0),
    footer: summary ? formatCurrency(summary.total_hpp_amount ?? 0) : '',
    footerAlign: 'right',
  },
];

const DailyMarketingReportPDF = ({
  data,
  total,
}: DailyMarketingReportPDFProps) => {
  const rows = data || [];
  const summary = total;

  return (
    <Document>
      <Page size='A3' orientation='landscape' style={pdfStyles.page}>
        {/* Title and Parameters */}
        <View style={pdfStyles.titleSection}>
          <PdfTypography size='h1' variant='primary'>
            Laporan &gt; Penjualan Harian
          </PdfTypography>
          <View style={pdfStyles.parameterContainer}>
            <PdfParamBadge>
              Tanggal: {formatDate(Date.now(), 'DD MMMM YYYY')}
            </PdfParamBadge>
            <PdfParamBadge>
              Dicetak: {formatDate(new Date(), 'DD MMM YYYY HH:mm')}
            </PdfParamBadge>
          </View>
        </View>

        {/* Table */}
        <PdfTable
          columns={getTableColumns(summary)}
          data={rows}
          showFooter={!!summary}
          footerLabel='TOTAL'
        />

        <PdfPageNumber />
      </Page>
    </Document>
  );
};

export default DailyMarketingReportPDF;
