<?php

namespace Myvetshop\Module\Clinique\Accounting\Export\Exporter\PostProcess;

use Myvetshop\Module\Clinique\Accounting\Export\Model\ExportLine;

/**
 * Correct the VAT information for 0.01ct difference to balance the export
 */
class VatRoundCorrectorPostProcessor implements ExportPostProcessorInterface
{
    public function postProcess(array $lines): array
    {
        $orderTotals = \array_reduce(
            $lines,
            function (array $totals, ExportLine $line): array {
                switch ($line->getAccount()) {
                    case '4110001':
                        $totals['paid'] += \max($line->getDebit(), $line->getCredit());
                        break;

                    case '7097':
                        $totals['discounts'] += \max($line->getDebit(), $line->getCredit());
                        break;

                    case '445711':
                    case '4457113':
                    case '4457111':
                        $totals['vat'] += \max($line->getDebit(), $line->getCredit());
                        break;

                    case '7080001':
                        $totals['shipping'] += \max($line->getDebit(), $line->getCredit());
                        break;

                    case '70711':
                    case '7071101':
                    case '7071102':
                    case '70713':
                    case '7071301':
                    case '7071302':
                        $totals['products'] = \max($line->getDebit(), $line->getCredit());
                        break;

                    default:
                        // Ignored
                }

                return $totals;
            },
            [
                'paid' => 0,
                'discounts' => 0,
                'vat' => 0,
                'products' => 0,
                'shipping' => 0,
            ]
        );

        $totalPaid = \round($orderTotals['paid'], 2);
        $totalVat = \round($orderTotals['vat'], 2);
        $totalProducts = \round($orderTotals['products'], 2);
        $totalDiscounts = \round($orderTotals['discounts'], 2);
        $totalShipping = \round($orderTotals['shipping'], 2);

        $difference = \round($totalPaid + $totalDiscounts - $totalVat - $totalProducts - $totalShipping, 2);

        // Patch the VAT amount to balance the export
        while (\abs($difference) > 0 && \abs($difference) <= 0.02) {
            // Find the first VAT line to apply the patch
            foreach ($lines as $i => $line) {
                switch ($line->getAccount()) {
                    case '445711':
                    case '4457113':
                    case '4457111':
                        $lines[$i] = new ExportLine(
                            $line->getDate(),
                            $line->getJournal(),
                            $line->getAccount(),
                            $line->getEntitled(),
                            $line->getDocNumber(),
                            $line->getDebit() > 0 ? \round($line->getDebit() + $difference, 2) : $line->getDebit(),
                            $line->getCredit() > 0 ? \round($line->getCredit() + $difference, 2) : $line->getCredit(),
                        );
                        $difference = 0;
                        break;

                    default:
                        // Ignored
                }
            }
        }

        return $lines;
    }
}
