<?php

namespace App\Services;

use App\Helpers\AmountInWords;
use App\Helpers\CompanyMedia;
use App\Helpers\CompanySettings;
use App\Helpers\PurchaseBillDocumentSettings;
use App\Helpers\GstHelper;
use App\Models\Company;
use App\Models\PurchaseBill;
use App\Models\TaxRate;

class PurchaseBillDocumentPresenter
{
    /**
     * @return array<string, mixed>
     */
    public function displayData(PurchaseBill $purchaseBill): array
    {
        $purchaseBill->load(['items', 'vendor']);
        $company = Company::findOrFail($purchaseBill->company_id);
        $taxRates = TaxRate::where('company_id', $company->id)->active()->selectable()->get();

        $settings = CompanySettings::forCompany($company->id);
        $docSettings = PurchaseBillDocumentSettings::forCompany($company->id);
        $gstContext = GstHelper::documentViewContext($company, $purchaseBill);
        $party = $purchaseBill->vendor;
        $shipToLines = $company->fullAddressLines();

        return array_merge([
            'purchaseBill' => $purchaseBill,
            'party' => $party,
            'shipToLines' => $shipToLines,
            'company' => $company,
            'settings' => $settings,
            'docSettings' => $docSettings,
            'taxBreakdown' => $gstContext['gstMode']
                ? []
                : $this->buildTaxBreakdown($purchaseBill, $taxRates),
            'amountInWords' => AmountInWords::rupees((float) $purchaseBill->grand_total),
            'documentCopies' => [
                'original' => 'Original Copy',
                'duplicate' => 'Duplicate Copy',
            ],
            'purchaseBillTitle' => PurchaseBillDocumentSettings::title($company->id),
            'logoSrc' => CompanyMedia::imageSource($company->logo_path),
            'signatureSrc' => CompanyMedia::imageSource($company->signature_image_path),
        ], $gstContext);
    }

    /**
     * @return list<array{name: string, rate: float, amount: float}>
     */
    private function buildTaxBreakdown(PurchaseBill $purchaseBill, $taxRates): array
    {
        $groups = [];

        foreach ($purchaseBill->items as $line) {
            if ((float) $line->tax_amount <= 0) {
                continue;
            }

            $rate = (float) $line->tax_rate;
            $key = number_format($rate, 2, '.', '');

            if (! isset($groups[$key])) {
                $tax = $taxRates->first(fn ($t) => abs((float) $t->rate - $rate) < 0.001);
                $groups[$key] = [
                    'name' => $tax?->name ?? 'Tax',
                    'rate' => $rate,
                    'amount' => 0,
                ];
            }

            $groups[$key]['amount'] += (float) $line->tax_amount;
        }

        usort($groups, fn ($a, $b) => $a['rate'] <=> $b['rate']);

        return array_values($groups);
    }
}
