<?php

namespace App\Exports;

use Illuminate\Support\Collection;
use Maatwebsite\Excel\Concerns\FromCollection;
use Maatwebsite\Excel\Concerns\WithHeadings;
use Maatwebsite\Excel\Concerns\WithTitle;

class GeneralLedgerExport implements FromCollection, WithHeadings, WithTitle
{
    /**
     * @param  array<string, mixed>  $ledger
     */
    public function __construct(
        private readonly array $ledger,
    ) {}

    public function collection(): Collection
    {
        return $this->ledger['entries']->map(fn (array $entry) => [
            $entry['date'],
            $entry['reference_number'],
            $entry['description'],
            $entry['debit_amount'] !== '0.00' ? $entry['debit_amount'] : '',
            $entry['credit_amount'] !== '0.00' ? $entry['credit_amount'] : '',
            $entry['running_balance'],
        ]);
    }

    public function headings(): array
    {
        return ['Date', 'Reference', 'Description', 'Debit', 'Credit', 'Balance'];
    }

    public function title(): string
    {
        return $this->ledger['account']->code ?? 'General Ledger';
    }
}
