/**
 * InvoiceSection Component
 *
 * Displays invoice information for international shipments in the preview modal.
 */
import { __ } from '@wordpress/i18n';

import styles from '../styles.module.scss';

export const InvoiceSection = ({ invoice }) => {
	if (!invoice || !invoice.items || invoice.items.length === 0) {
		return null;
	}

	const renderField = (label, value) => {
		if (!value && value !== 0) return null;

		return (
			<div className={styles['shipment-preview-modal__field']}>
				<span className={styles['shipment-preview-modal__field-label']}>{label}:</span>
				<span className={styles['shipment-preview-modal__field-value']}>{value}</span>
			</div>
		);
	};

	return (
		<div className={styles['shipment-preview-modal__section']}>
			<h3 className={styles['shipment-preview-modal__section-title']}>
				{__('Invoice (International)', 'nova-post-for-woocommerce')}
			</h3>
			<div className={styles['shipment-preview-modal__section-content']}>
				{renderField(__('Incoterm', 'nova-post-for-woocommerce'), invoice.incoterm)}
				{renderField(__('Export Reason', 'nova-post-for-woocommerce'), invoice.exportReason)}
				{renderField(__('Currency', 'nova-post-for-woocommerce'), invoice.currencyCode)}
				{renderField(__('Customer Number', 'nova-post-for-woocommerce'), invoice.customerNumber)}

				<div className={styles['shipment-preview-modal__invoice-items']}>
					<h4 className={styles['shipment-preview-modal__subsection-title']}>
						{__('Items', 'nova-post-for-woocommerce')}
					</h4>
					{invoice.items.map((item, index) => (
						<div key={index} className={styles['shipment-preview-modal__invoice-item']}>
							<h5 className={styles['shipment-preview-modal__invoice-item-title']}>
								{item.name || item.nameEng} (x{item.amount})
							</h5>
							{renderField(__('HS Code', 'nova-post-for-woocommerce'), item.hsCode)}
							{renderField(__('Weight (g)', 'nova-post-for-woocommerce'), item.actualWeight)}
							{renderField(__('Unit Cost', 'nova-post-for-woocommerce'), `${item.cost} ${item.currency}`)}
							{renderField(__('Cost', 'nova-post-for-woocommerce') + ' USD', `$${item.costUsd}`)}
							{renderField(__('Cost', 'nova-post-for-woocommerce') + ' EUR', `€${item.costEur}`)}
						</div>
					))}
				</div>

						<div className={styles['shipment-preview-modal__invoice-totals']}>
							<h4 className={styles['shipment-preview-modal__subsection-title']}>
								{__('Totals', 'nova-post-for-woocommerce')}
							</h4>
					{renderField(__('Total Cost', 'nova-post-for-woocommerce'), `${invoice.totalCost} ${invoice.currencyCode}`)}
					{renderField(__('Total', 'nova-post-for-woocommerce') + ' USD', `$${invoice.totalCostUsd}`)}
					{renderField(__('Total', 'nova-post-for-woocommerce') + ' EUR', `€${invoice.totalCostEur}`)}
				</div>
			</div>
		</div>
	);
};
